Add tests for OTTraceCorrelation
MODIFY
dd-trace-ot/src/main/java/datadog/opentracing/OTTraceCorrelation.java
dd-trace-ot/src/main/java/datadog/opentracing/OTTraceCorrelation.java
package datadog.opentracing;
+import com.google.common.annotations.VisibleForTesting;
import datadog.trace.api.CorrelationIdentifier;
import io.opentracing.Span;
+import io.opentracing.Tracer;
import io.opentracing.util.GlobalTracer;
public class OTTraceCorrelation implements CorrelationIdentifier.Provider {
public static final OTTraceCorrelation INSTANCE = new OTTraceCorrelation();
- private OTTraceCorrelation() {}
+ private final Tracer tracer;
+
+ private OTTraceCorrelation() {
+ // GlobalTracer.get() is guaranteed to return a constant so we can keep reference to it
+ this(GlobalTracer.get());
+ }
+
+ @VisibleForTesting
+ OTTraceCorrelation(Tracer tracer) {
+ this.tracer = tracer;
+ }
@Override
public long getTraceId() {
- final Span activeSpan = GlobalTracer.get().activeSpan();
+ final Span activeSpan = tracer.activeSpan();
if (activeSpan instanceof DDSpan) {
return ((DDSpan) activeSpan).getTraceId();
}
@@ -20,7 +32,7 @@ public class OTTraceCorrelation implements CorrelationIdentifier.Provider {
@Override
public long getSpanId() {
- final Span activeSpan = GlobalTracer.get().activeSpan();
+ final Span activeSpan = tracer.activeSpan();
if (activeSpan instanceof DDSpan) {
return ((DDSpan) activeSpan).getSpanId();
}

ADD
dd-trace-ot/src/test/groovy/datadog/opentracing/OTTraceCorrelationTest.groovy
+package datadog.opentracing
+
+import datadog.trace.common.writer.ListWriter
+import spock.lang.Shared
+import spock.lang.Specification
+
+class OTTraceCorrelationTest extends Specification {
+
+ static final WRITER = new ListWriter()
+
+ @Shared
+ DDTracer tracer = new DDTracer(WRITER)
+ @Shared
+ OTTraceCorrelation traceCorrelation = new OTTraceCorrelation(tracer)
+
+ def scope = tracer.buildSpan("test").startActive(true)
+
+ def cleanup() {
+ scope.close()
+ }
+
+ def "get trace id without trace"() {
+ setup:
+ scope.close()
+
+ expect:
+ 0 == traceCorrelation.getTraceId()
+ }
+
+ def "get trace id with trace"() {
+ expect:
+ ((DDSpan) scope.span()).traceId == traceCorrelation.getTraceId()
+ }
+
+ def "get span id without span"() {
+ setup:
+ scope.close()
+
+ expect:
+ 0 == traceCorrelation.getSpanId()
+ }
+
+ def "get span id with trace"() {
+ expect:
+ ((DDSpan) scope.span()).spanId == traceCorrelation.getSpanId()
+ }
+}

Add tests for OTTraceCorrelation