Add tests for OTTraceCorrelation
This commit is contained in:
parent
519f15bcc6
commit
d3aa982082
|
@ -1,17 +1,29 @@
|
|||
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();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
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()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue