And, ... Voilà

This commit is contained in:
Guillaume Polaert 2017-04-26 10:55:33 +02:00
parent 69ea8573b9
commit 299cc57fc1
7 changed files with 41 additions and 41 deletions

View File

@ -1,10 +1,10 @@
package com.datadoghq.trace; package com.datadoghq.trace;
import com.datadoghq.trace.impl.Span; import com.datadoghq.trace.impl.DDSpan;
public interface Sampler { public interface Sampler {
public boolean sample(Span span); public boolean sample(DDSpan span);
} }

View File

@ -6,21 +6,21 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
public class Span implements io.opentracing.Span { public class DDSpan implements io.opentracing.Span {
private final Tracer tracer; private final Tracer tracer;
private final String operationName; private final String operationName;
private Map<String, Object> tags; private Map<String, Object> tags;
private long startTime; private long startTime;
private long durationMilliseconds; private long durationMilliseconds;
private final com.datadoghq.trace.impl.SpanContext context; private final DDSpanContext context;
Span( DDSpan(
Tracer tracer, Tracer tracer,
String operationName, String operationName,
Map<String, Object> tags, Map<String, Object> tags,
Optional<Long> timestamp, Optional<Long> timestamp,
com.datadoghq.trace.impl.SpanContext context) { DDSpanContext context) {
this.tracer = tracer; this.tracer = tracer;
this.operationName = operationName; this.operationName = operationName;
this.tags = tags; this.tags = tags;
@ -104,7 +104,7 @@ public class Span implements io.opentracing.Span {
return startTime; return startTime;
} }
public com.datadoghq.trace.impl.SpanContext getContext(){ public DDSpanContext getContext(){
return context; return context;
} }

View File

@ -5,7 +5,7 @@ import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
public class SpanContext implements io.opentracing.SpanContext { public class DDSpanContext implements io.opentracing.SpanContext {
// Public span attributes // Public span attributes
private final String serviceName; private final String serviceName;
@ -20,7 +20,7 @@ public class SpanContext implements io.opentracing.SpanContext {
// Sampler attributes // Sampler attributes
private boolean sampled; private boolean sampled;
public SpanContext( public DDSpanContext(
long traceId, long traceId,
long spanId, long spanId,
long parentId, long parentId,

View File

@ -17,7 +17,7 @@ public class DDSpanSerializer implements SpanSerializer {
} }
public io.opentracing.Span deserialize(String str) throws JsonParseException, JsonMappingException, IOException { 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{ public static void main(String[] args) throws Exception{

View File

@ -78,9 +78,10 @@ public class Tracer implements io.opentracing.Tracer {
public Span start() { public Span start() {
// build the context // build the context
SpanContext context = buildTheSpanContext(); DDSpanContext context = buildTheSpanContext();
return new com.datadoghq.trace.impl.Span(
return new DDSpan(
Tracer.this, Tracer.this,
this.operationName, this.operationName,
this.tags, this.tags,
@ -88,14 +89,14 @@ public class Tracer implements io.opentracing.Tracer {
context); context);
} }
private SpanContext buildTheSpanContext() { private DDSpanContext buildTheSpanContext() {
SpanContext context; DDSpanContext context;
long generatedId = generateNewId(); long generatedId = generateNewId();
if (parent != null) { if (parent != null) {
com.datadoghq.trace.impl.SpanContext p = (com.datadoghq.trace.impl.SpanContext) parent; DDSpanContext p = (DDSpanContext) parent;
context = new com.datadoghq.trace.impl.SpanContext( context = new DDSpanContext(
p.getTraceId(), p.getTraceId(),
generatedId, generatedId,
p.getSpanId(), p.getSpanId(),
@ -107,7 +108,7 @@ public class Tracer implements io.opentracing.Tracer {
null, null,
true); true);
} else { } else {
context = new com.datadoghq.trace.impl.SpanContext( context = new DDSpanContext(
generatedId, generatedId,
generatedId, generatedId,
0L, 0L,

View File

@ -12,7 +12,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
public class SpanBuilderTest { public class DDSpanBuilderTest {
Tracer tracer; Tracer tracer;
@ -31,7 +31,7 @@ public class SpanBuilderTest {
public void shouldBuilSimpleSpan() { public void shouldBuilSimpleSpan() {
final String expectedName = "fakeName"; final String expectedName = "fakeName";
Span span = (Span) tracer.buildSpan(expectedName).start(); DDSpan span = (DDSpan) tracer.buildSpan(expectedName).start();
assertThat(span.getOperationName()).isEqualTo(expectedName); assertThat(span.getOperationName()).isEqualTo(expectedName);
} }
@ -47,7 +47,7 @@ public class SpanBuilderTest {
} }
}; };
Span span = (Span) tracer DDSpan span = (DDSpan) tracer
.buildSpan(expectedName) .buildSpan(expectedName)
.withTag("1", (Boolean) tags.get("1")) .withTag("1", (Boolean) tags.get("1"))
.withTag("2", (String) tags.get("2")) .withTag("2", (String) tags.get("2"))
@ -59,7 +59,7 @@ public class SpanBuilderTest {
// with no tag provided // with no tag provided
span = (Span) tracer span = (DDSpan) tracer
.buildSpan(expectedName) .buildSpan(expectedName)
.start(); .start();
@ -75,7 +75,7 @@ public class SpanBuilderTest {
final long expectedTimestamp = 487517802L * 1000; final long expectedTimestamp = 487517802L * 1000;
final String expectedName = "fakeName"; final String expectedName = "fakeName";
Span span = (Span) tracer DDSpan span = (DDSpan) tracer
.buildSpan(expectedName) .buildSpan(expectedName)
.withStartTimestamp(expectedTimestamp) .withStartTimestamp(expectedTimestamp)
.start(); .start();
@ -84,7 +84,7 @@ public class SpanBuilderTest {
// auto-timestamp in nanoseconds // auto-timestamp in nanoseconds
long tick = System.currentTimeMillis(); long tick = System.currentTimeMillis();
span = (Span) tracer span = (DDSpan) tracer
.buildSpan(expectedName) .buildSpan(expectedName)
.start(); .start();
@ -100,20 +100,20 @@ public class SpanBuilderTest {
final long spanId = 1L; final long spanId = 1L;
final long expectedParentId = spanId; final long expectedParentId = spanId;
SpanContext mockedContext = mock(SpanContext.class); DDSpanContext mockedContext = mock(DDSpanContext.class);
Span mockedSpan = mock(Span.class); DDSpan mockedSpan = mock(DDSpan.class);
when(mockedSpan.context()).thenReturn(mockedContext); when(mockedSpan.context()).thenReturn(mockedContext);
when(mockedContext.getSpanId()).thenReturn(spanId); when(mockedContext.getSpanId()).thenReturn(spanId);
final String expectedName = "fakeName"; final String expectedName = "fakeName";
Span span = (Span) tracer DDSpan span = (DDSpan) tracer
.buildSpan(expectedName) .buildSpan(expectedName)
.asChildOf(mockedSpan) .asChildOf(mockedSpan)
.start(); .start();
SpanContext actualContext = (SpanContext) span.context(); DDSpanContext actualContext = (DDSpanContext) span.context();
assertThat(actualContext.getParentId()).isEqualTo(expectedParentId); assertThat(actualContext.getParentId()).isEqualTo(expectedParentId);
@ -127,38 +127,38 @@ public class SpanBuilderTest {
final long spanId = 223L; final long spanId = 223L;
final long expectedParentId = spanId; final long expectedParentId = spanId;
SpanContext mockedContext = mock(SpanContext.class); DDSpanContext mockedContext = mock(DDSpanContext.class);
when(mockedContext.getSpanId()).thenReturn(spanId); when(mockedContext.getSpanId()).thenReturn(spanId);
final String expectedName = "fakeName"; final String expectedName = "fakeName";
// case 1, using a CHILD_OF ref // case 1, using a CHILD_OF ref
Span span = (Span) tracer DDSpan span = (DDSpan) tracer
.buildSpan(expectedName) .buildSpan(expectedName)
.addReference(References.CHILD_OF, mockedContext) .addReference(References.CHILD_OF, mockedContext)
.start(); .start();
SpanContext actualContext = (SpanContext) span.context(); DDSpanContext actualContext = (DDSpanContext) span.context();
assertThat(actualContext.getParentId()).isEqualTo(expectedParentId); assertThat(actualContext.getParentId()).isEqualTo(expectedParentId);
// case 2, using a FOLLOW_FROM ref // case 2, using a FOLLOW_FROM ref
span = (Span) tracer span = (DDSpan) tracer
.buildSpan(expectedName) .buildSpan(expectedName)
.addReference(References.FOLLOWS_FROM, mockedContext) .addReference(References.FOLLOWS_FROM, mockedContext)
.start(); .start();
actualContext = (SpanContext) span.context(); actualContext = (DDSpanContext) span.context();
assertThat(actualContext.getParentId()).isEqualTo(expectedParentId); assertThat(actualContext.getParentId()).isEqualTo(expectedParentId);
// case 2, using a WFT ref, should not be linked to the previous // case 2, using a WFT ref, should not be linked to the previous
span = (Span) tracer span = (DDSpan) tracer
.buildSpan(expectedName) .buildSpan(expectedName)
.addReference("WTF", mockedContext) .addReference("WTF", mockedContext)
.start(); .start();
actualContext = (SpanContext) span.context(); actualContext = (DDSpanContext) span.context();
assertThat(actualContext.getParentId()).isEqualTo(0L); assertThat(actualContext.getParentId()).isEqualTo(0L);
} }
@ -170,14 +170,14 @@ public class SpanBuilderTest {
final String expectedBaggageItemKey = "fakeKey"; final String expectedBaggageItemKey = "fakeKey";
final String expectedBaggageItemValue = "fakeValue"; final String expectedBaggageItemValue = "fakeValue";
Span parent = (Span) tracer DDSpan parent = (DDSpan) tracer
.buildSpan(expectedName) .buildSpan(expectedName)
.start(); .start();
assertThat(parent.getOperationName()).isEqualTo(expectedName); assertThat(parent.getOperationName()).isEqualTo(expectedName);
assertThat(parent.context().baggageItems()).isEmpty(); 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.getOperationName()).isEqualTo(expectedName);
assertThat(span.getBaggageItem(expectedBaggageItemKey)).isEqualTo(expectedBaggageItemValue); assertThat(span.getBaggageItem(expectedBaggageItemKey)).isEqualTo(expectedBaggageItemValue);

View File

@ -5,25 +5,24 @@ import org.junit.Test;
import java.util.Optional; import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
public class SpanTest { public class DDSpanTest {
@Test @Test
public void shouldAddBaggageItem(){ public void shouldAddBaggageItem() {
Tracer mockedTracer = mock(Tracer.class); Tracer mockedTracer = mock(Tracer.class);
SpanContext mockedContext = mock(SpanContext.class); DDSpanContext mockedContext = mock(DDSpanContext.class);
final String expectedBaggageItemKey = "fakeKey"; final String expectedBaggageItemKey = "fakeKey";
final String expectedBaggageItemValue = "fakeValue"; final String expectedBaggageItemValue = "fakeValue";
Span span = new Span( DDSpan span = new DDSpan(
mockedTracer, mockedTracer,
"fakeName", "fakeName",
null, null,