Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Introduce Clock interface for time measurements #50

Merged
merged 3 commits into from
Sep 12, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ ext.jacksonVersion = '2.7.4'

ext.junitVersion = '4.12'
ext.mockitoVersion = '2.0.2-beta'
ext.powermockVersion = '1.6.4'

allprojects {
apply plugin: 'idea' // intellij support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.uber.jaeger.filters.jaxrs2;
package com.uber.jaeger.context;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oibe these tests weren't in the proper tests dir, so not sure if they were even exercised previously (intelliJ didn't understand them as tests)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted.


import com.uber.jaeger.Tracer;
import com.uber.jaeger.reporters.InMemoryReporter;
import com.uber.jaeger.samplers.ConstSampler;
import io.opentracing.Span;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -35,14 +32,12 @@
import static org.mockito.Mockito.when;

public class CallableTest {
Tracer tracer;
TraceContext traceContext;
Span span;

@Before
public void setUp() {
tracer = new Tracer.Builder("raza", new InMemoryReporter(), new ConstSampler(true)).build();
span = tracer.buildSpan("firefly").start();
span = mock(Span.class);
traceContext = mock(TraceContext.class);
when(traceContext.getCurrentSpan()).thenReturn(span);
when(traceContext.pop()).thenReturn(span);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.uber.jaeger.filters.jaxrs2;
package com.uber.jaeger.context;

import com.uber.jaeger.Tracer;
import com.uber.jaeger.reporters.InMemoryReporter;
import com.uber.jaeger.samplers.ConstSampler;
import io.opentracing.Span;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -35,14 +32,12 @@
import static org.mockito.Mockito.when;

public class RunnableTest {
Tracer tracer;
TraceContext traceContext;
Span span;

@Before
public void setUp() {
tracer = new Tracer.Builder("death-star", new InMemoryReporter(), new ConstSampler(true)).build();
span = tracer.buildSpan("destroyer").start();
span = mock(Span.class);
traceContext = mock(TraceContext.class);
when(traceContext.getCurrentSpan()).thenReturn(span);
when(traceContext.pop()).thenReturn(span);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,17 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.uber.jaeger.filters.jaxrs2;
package com.uber.jaeger.context;

import io.opentracing.Span;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

import com.uber.jaeger.Tracer;
import com.uber.jaeger.reporters.InMemoryReporter;
import com.uber.jaeger.samplers.ConstSampler;
import io.opentracing.Span;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
Expand All @@ -41,22 +38,22 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.mockito.Mockito.when;

public class TracedExecutorServiceTest {

TimeUnit TIME_UNIT = TimeUnit.MILLISECONDS;
TracedExecutorService tracedExecutorService;
ExecutorService wrappedExecutorService;
Tracer tracer = new Tracer.Builder("test-executor-service", new InMemoryReporter(), new ConstSampler(true)).build();
//Tracer tracer = new Tracer.Builder("test-executor-service", new InMemoryReporter(), new ConstSampler(true)).build();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing this

Span span;
TraceContext traceContext;
List<java.util.concurrent.Callable<Span>> callableList;

@Before
public void setUp() {
wrappedExecutorService = mock(ExecutorService.class);
span = tracer.buildSpan("span-op").start();
span = mock(Span.class);
traceContext = mock(TraceContext.class);
when(traceContext.pop()).thenReturn(span);
when(traceContext.getCurrentSpan()).thenReturn(span);
Expand Down
4 changes: 0 additions & 4 deletions jaeger-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ dependencies {

testCompile group: 'junit', name: 'junit', version: junitVersion
testCompile group: 'org.mockito', name: 'mockito-all', version: mockitoVersion
// TODO use only one mocking framework
testCompile group: 'org.powermock', name: 'powermock-api-mockito', version: powermockVersion
testCompile group: 'org.powermock', name: 'powermock-core', version: powermockVersion
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: powermockVersion
}

compileThrift {
Expand Down
62 changes: 44 additions & 18 deletions jaeger-core/src/main/java/com/uber/jaeger/Span.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,42 @@
package com.uber.jaeger;

import com.twitter.zipkin.thriftjava.Endpoint;
import com.uber.jaeger.utils.Utils;
import io.opentracing.tag.Tags;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Span implements io.opentracing.Span {
private final Tracer tracer;
private final long start; // time in microseconds
private final String operationName;
private final long startTimeMicroseconds;
private final long startTimeNanoseconds;
private final boolean computeDurationViaNanoseconds;
private long durationMicroseconds; // span durationMicroseconds
private String operationName;
private SpanContext context;
private Endpoint peer;
private long duration; // time in microseconds
private Map<String,Object> tags;
private Map<String, Object> tags;
private List<LogData> logs;
private String localComponent;
private boolean isClient;
private boolean isRPC;

Span(Tracer tracer, String operationName, SpanContext context, long start, Map<String, Object> tags) {
Span(Tracer tracer,
String operationName,
SpanContext context,
long startTimeMicroseconds,
long startTimeNanoseconds,
boolean computeDurationViaNanoseconds, Map<String, Object> tags
) {
this.tracer = tracer;
this.operationName = operationName;
this.context = context;
this.start = start;
this.startTimeMicroseconds = startTimeMicroseconds;
this.startTimeNanoseconds = startTimeNanoseconds;
this.computeDurationViaNanoseconds = computeDurationViaNanoseconds;
this.tags = tags;
}

Expand All @@ -56,12 +66,12 @@ public String getLocalComponent() {
}

public long getStart() {
return start;
return startTimeMicroseconds;
}

public long getDuration() {
synchronized (this) {
return duration;
return durationMicroseconds;
}
}

Expand All @@ -87,23 +97,30 @@ private Endpoint getOrMakePeer() {

public Map<String, Object> getTags() {
synchronized (this) {
// TODO wasteful allocation and copying
return new HashMap<>(tags);
return Collections.unmodifiableMap(tags);
}
}

// Will be in the Span interface as of opentracing-java 0.15
public Span setOperationName(String operationName) {
synchronized (this) {
this.operationName = operationName;
}
return this;
}

public String getOperationName() {
return operationName;
synchronized (this) {
return operationName;
}
}

public List<LogData> getLogs() {
synchronized (this) {
if (logs == null) {
return null;
}

// TODO wasteful allocation and copying
return new ArrayList<>(logs);
return Collections.unmodifiableList(logs);
}
}

Expand Down Expand Up @@ -143,13 +160,22 @@ public io.opentracing.SpanContext context() {

@Override
public void finish() {
finish(Utils.getMicroseconds());
if (computeDurationViaNanoseconds) {
long nanoDuration = startTimeNanoseconds - tracer.clock().currentTimeNanos();
finishWithDuration(nanoDuration / 1000);
} else {
finish(tracer.clock().currentTimeMicros());
}
}

@Override
public void finish(long finishMicros) {
finishWithDuration(finishMicros - startTimeMicroseconds);
}

private void finishWithDuration(long durationMicros) {
synchronized(this) {
this.duration = finishMicros - start;
this.durationMicroseconds = durationMicros;
}

if (context.isSampled()) {
Expand Down Expand Up @@ -238,7 +264,7 @@ private Span setTagAsObject(String key, Object value) {

@Override
public Span log(String message, /* @Nullable */ Object payload) {
return log(Utils.getMicroseconds(), message, payload);
return log(tracer.clock().currentTimeMicros(), message, payload);
}

@Override
Expand Down
Loading