And, ... Voilà
This commit is contained in:
parent
69ea8573b9
commit
299cc57fc1
|
|
@ -1,10 +1,10 @@
|
|||
package com.datadoghq.trace;
|
||||
|
||||
|
||||
import com.datadoghq.trace.impl.Span;
|
||||
import com.datadoghq.trace.impl.DDSpan;
|
||||
|
||||
public interface Sampler {
|
||||
|
||||
public boolean sample(Span span);
|
||||
public boolean sample(DDSpan span);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,21 +6,21 @@ import java.util.Map;
|
|||
import java.util.Optional;
|
||||
|
||||
|
||||
public class Span implements io.opentracing.Span {
|
||||
public class DDSpan implements io.opentracing.Span {
|
||||
|
||||
private final Tracer tracer;
|
||||
private final String operationName;
|
||||
private Map<String, Object> tags;
|
||||
private long startTime;
|
||||
private long durationMilliseconds;
|
||||
private final com.datadoghq.trace.impl.SpanContext context;
|
||||
private final DDSpanContext context;
|
||||
|
||||
Span(
|
||||
DDSpan(
|
||||
Tracer tracer,
|
||||
String operationName,
|
||||
Map<String, Object> tags,
|
||||
Optional<Long> timestamp,
|
||||
com.datadoghq.trace.impl.SpanContext context) {
|
||||
DDSpanContext context) {
|
||||
this.tracer = tracer;
|
||||
this.operationName = operationName;
|
||||
this.tags = tags;
|
||||
|
|
@ -104,7 +104,7 @@ public class Span implements io.opentracing.Span {
|
|||
return startTime;
|
||||
}
|
||||
|
||||
public com.datadoghq.trace.impl.SpanContext getContext(){
|
||||
public DDSpanContext getContext(){
|
||||
return context;
|
||||
}
|
||||
|
||||
|
|
@ -5,7 +5,7 @@ import java.util.Collections;
|
|||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class SpanContext implements io.opentracing.SpanContext {
|
||||
public class DDSpanContext implements io.opentracing.SpanContext {
|
||||
|
||||
// Public span attributes
|
||||
private final String serviceName;
|
||||
|
|
@ -20,7 +20,7 @@ public class SpanContext implements io.opentracing.SpanContext {
|
|||
// Sampler attributes
|
||||
private boolean sampled;
|
||||
|
||||
public SpanContext(
|
||||
public DDSpanContext(
|
||||
long traceId,
|
||||
long spanId,
|
||||
long parentId,
|
||||
|
|
@ -17,7 +17,7 @@ public class DDSpanSerializer implements SpanSerializer {
|
|||
}
|
||||
|
||||
public io.opentracing.Span deserialize(String str) throws JsonParseException, JsonMappingException, IOException {
|
||||
return objectMapper.readValue(str, Span.class);
|
||||
return objectMapper.readValue(str, DDSpan.class);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception{
|
||||
|
|
|
|||
|
|
@ -78,9 +78,10 @@ public class Tracer implements io.opentracing.Tracer {
|
|||
public Span start() {
|
||||
|
||||
// build the context
|
||||
SpanContext context = buildTheSpanContext();
|
||||
DDSpanContext context = buildTheSpanContext();
|
||||
|
||||
return new com.datadoghq.trace.impl.Span(
|
||||
|
||||
return new DDSpan(
|
||||
Tracer.this,
|
||||
this.operationName,
|
||||
this.tags,
|
||||
|
|
@ -88,14 +89,14 @@ public class Tracer implements io.opentracing.Tracer {
|
|||
context);
|
||||
}
|
||||
|
||||
private SpanContext buildTheSpanContext() {
|
||||
private DDSpanContext buildTheSpanContext() {
|
||||
|
||||
SpanContext context;
|
||||
DDSpanContext context;
|
||||
|
||||
long generatedId = generateNewId();
|
||||
if (parent != null) {
|
||||
com.datadoghq.trace.impl.SpanContext p = (com.datadoghq.trace.impl.SpanContext) parent;
|
||||
context = new com.datadoghq.trace.impl.SpanContext(
|
||||
DDSpanContext p = (DDSpanContext) parent;
|
||||
context = new DDSpanContext(
|
||||
p.getTraceId(),
|
||||
generatedId,
|
||||
p.getSpanId(),
|
||||
|
|
@ -107,7 +108,7 @@ public class Tracer implements io.opentracing.Tracer {
|
|||
null,
|
||||
true);
|
||||
} else {
|
||||
context = new com.datadoghq.trace.impl.SpanContext(
|
||||
context = new DDSpanContext(
|
||||
generatedId,
|
||||
generatedId,
|
||||
0L,
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class SpanBuilderTest {
|
||||
public class DDSpanBuilderTest {
|
||||
|
||||
Tracer tracer;
|
||||
|
||||
|
|
@ -31,7 +31,7 @@ public class SpanBuilderTest {
|
|||
public void shouldBuilSimpleSpan() {
|
||||
|
||||
final String expectedName = "fakeName";
|
||||
Span span = (Span) tracer.buildSpan(expectedName).start();
|
||||
DDSpan span = (DDSpan) tracer.buildSpan(expectedName).start();
|
||||
assertThat(span.getOperationName()).isEqualTo(expectedName);
|
||||
}
|
||||
|
||||
|
|
@ -47,7 +47,7 @@ public class SpanBuilderTest {
|
|||
}
|
||||
};
|
||||
|
||||
Span span = (Span) tracer
|
||||
DDSpan span = (DDSpan) tracer
|
||||
.buildSpan(expectedName)
|
||||
.withTag("1", (Boolean) tags.get("1"))
|
||||
.withTag("2", (String) tags.get("2"))
|
||||
|
|
@ -59,7 +59,7 @@ public class SpanBuilderTest {
|
|||
|
||||
// with no tag provided
|
||||
|
||||
span = (Span) tracer
|
||||
span = (DDSpan) tracer
|
||||
.buildSpan(expectedName)
|
||||
.start();
|
||||
|
||||
|
|
@ -75,7 +75,7 @@ public class SpanBuilderTest {
|
|||
final long expectedTimestamp = 487517802L * 1000;
|
||||
final String expectedName = "fakeName";
|
||||
|
||||
Span span = (Span) tracer
|
||||
DDSpan span = (DDSpan) tracer
|
||||
.buildSpan(expectedName)
|
||||
.withStartTimestamp(expectedTimestamp)
|
||||
.start();
|
||||
|
|
@ -84,7 +84,7 @@ public class SpanBuilderTest {
|
|||
|
||||
// auto-timestamp in nanoseconds
|
||||
long tick = System.currentTimeMillis();
|
||||
span = (Span) tracer
|
||||
span = (DDSpan) tracer
|
||||
.buildSpan(expectedName)
|
||||
.start();
|
||||
|
||||
|
|
@ -100,20 +100,20 @@ public class SpanBuilderTest {
|
|||
final long spanId = 1L;
|
||||
final long expectedParentId = spanId;
|
||||
|
||||
SpanContext mockedContext = mock(SpanContext.class);
|
||||
Span mockedSpan = mock(Span.class);
|
||||
DDSpanContext mockedContext = mock(DDSpanContext.class);
|
||||
DDSpan mockedSpan = mock(DDSpan.class);
|
||||
|
||||
when(mockedSpan.context()).thenReturn(mockedContext);
|
||||
when(mockedContext.getSpanId()).thenReturn(spanId);
|
||||
|
||||
final String expectedName = "fakeName";
|
||||
|
||||
Span span = (Span) tracer
|
||||
DDSpan span = (DDSpan) tracer
|
||||
.buildSpan(expectedName)
|
||||
.asChildOf(mockedSpan)
|
||||
.start();
|
||||
|
||||
SpanContext actualContext = (SpanContext) span.context();
|
||||
DDSpanContext actualContext = (DDSpanContext) span.context();
|
||||
|
||||
assertThat(actualContext.getParentId()).isEqualTo(expectedParentId);
|
||||
|
||||
|
|
@ -127,38 +127,38 @@ public class SpanBuilderTest {
|
|||
final long spanId = 223L;
|
||||
final long expectedParentId = spanId;
|
||||
|
||||
SpanContext mockedContext = mock(SpanContext.class);
|
||||
DDSpanContext mockedContext = mock(DDSpanContext.class);
|
||||
when(mockedContext.getSpanId()).thenReturn(spanId);
|
||||
|
||||
final String expectedName = "fakeName";
|
||||
|
||||
|
||||
// case 1, using a CHILD_OF ref
|
||||
Span span = (Span) tracer
|
||||
DDSpan span = (DDSpan) tracer
|
||||
.buildSpan(expectedName)
|
||||
.addReference(References.CHILD_OF, mockedContext)
|
||||
.start();
|
||||
|
||||
SpanContext actualContext = (SpanContext) span.context();
|
||||
DDSpanContext actualContext = (DDSpanContext) span.context();
|
||||
assertThat(actualContext.getParentId()).isEqualTo(expectedParentId);
|
||||
|
||||
|
||||
// case 2, using a FOLLOW_FROM ref
|
||||
span = (Span) tracer
|
||||
span = (DDSpan) tracer
|
||||
.buildSpan(expectedName)
|
||||
.addReference(References.FOLLOWS_FROM, mockedContext)
|
||||
.start();
|
||||
|
||||
actualContext = (SpanContext) span.context();
|
||||
actualContext = (DDSpanContext) span.context();
|
||||
assertThat(actualContext.getParentId()).isEqualTo(expectedParentId);
|
||||
|
||||
// case 2, using a WFT ref, should not be linked to the previous
|
||||
span = (Span) tracer
|
||||
span = (DDSpan) tracer
|
||||
.buildSpan(expectedName)
|
||||
.addReference("WTF", mockedContext)
|
||||
.start();
|
||||
|
||||
actualContext = (SpanContext) span.context();
|
||||
actualContext = (DDSpanContext) span.context();
|
||||
assertThat(actualContext.getParentId()).isEqualTo(0L);
|
||||
|
||||
}
|
||||
|
|
@ -170,14 +170,14 @@ public class SpanBuilderTest {
|
|||
final String expectedBaggageItemKey = "fakeKey";
|
||||
final String expectedBaggageItemValue = "fakeValue";
|
||||
|
||||
Span parent = (Span) tracer
|
||||
DDSpan parent = (DDSpan) tracer
|
||||
.buildSpan(expectedName)
|
||||
.start();
|
||||
|
||||
assertThat(parent.getOperationName()).isEqualTo(expectedName);
|
||||
assertThat(parent.context().baggageItems()).isEmpty();
|
||||
|
||||
Span span = (Span) tracer.buildSpan(expectedName).start();
|
||||
DDSpan span = (DDSpan) tracer.buildSpan(expectedName).start();
|
||||
|
||||
assertThat(span.getOperationName()).isEqualTo(expectedName);
|
||||
assertThat(span.getBaggageItem(expectedBaggageItemKey)).isEqualTo(expectedBaggageItemValue);
|
||||
|
|
@ -5,25 +5,24 @@ import org.junit.Test;
|
|||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
|
||||
public class SpanTest {
|
||||
public class DDSpanTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldAddBaggageItem(){
|
||||
public void shouldAddBaggageItem() {
|
||||
|
||||
|
||||
Tracer mockedTracer = mock(Tracer.class);
|
||||
SpanContext mockedContext = mock(SpanContext.class);
|
||||
DDSpanContext mockedContext = mock(DDSpanContext.class);
|
||||
|
||||
final String expectedBaggageItemKey = "fakeKey";
|
||||
final String expectedBaggageItemValue = "fakeValue";
|
||||
|
||||
|
||||
Span span = new Span(
|
||||
DDSpan span = new DDSpan(
|
||||
mockedTracer,
|
||||
"fakeName",
|
||||
null,
|
||||
Loading…
Reference in New Issue