sync w/
MODIFY
pom.xml
pom.xml
<version>3.6.2</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>2.7.22</version>
+ </dependency>
</dependencies>
<build>

MODIFY
src/main/java/com/datadoghq/trace/impl/Tracer.java
src/main/java/com/datadoghq/trace/impl/Tracer.java
@@ -7,6 +7,7 @@ import io.opentracing.propagation.Format;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
+import java.util.UUID;
public class Tracer implements io.opentracing.Tracer {
@@ -28,14 +29,15 @@ public class Tracer implements io.opentracing.Tracer {
private final String operationName;
private Map<String, Object> tags = new HashMap();
private Long timestamp;
- private Optional<SpanContext> parent;
+ private Optional<SpanContext> parent = Optional.empty();
public SpanBuilder(String operationName) {
this.operationName = operationName;
}
public io.opentracing.Tracer.SpanBuilder asChildOf(SpanContext spanContext) {
- return null;
+ this.parent = Optional.ofNullable(spanContext);
+ return this;
}
public io.opentracing.Tracer.SpanBuilder asChildOf(Span span) {
@@ -73,36 +75,57 @@ public class Tracer implements io.opentracing.Tracer {
public Span start() {
// build the context
- SpanContext context = buildNewSpanContext();
+ SpanContext context = buildTheSpanContext();
return new com.datadoghq.trace.impl.Span(
+ Tracer.this,
this.operationName,
this.tags,
Optional.ofNullable(this.timestamp),
context);
}
- private SpanContext buildNewSpanContext() {
-
- Optional<Object> parentContext = Optional.ofNullable(this.parent);
- SpanContext context = new com.datadoghq.trace.impl.SpanContext(
-
- );
-
-
-
- if (this.parent == null) {
- long traceId = 123L;
-
+ private SpanContext buildTheSpanContext() {
+
+ SpanContext context = null;
+
+ long generatedId = generateNewId();
+ if (parent.isPresent()) {
+ com.datadoghq.trace.impl.SpanContext p = (com.datadoghq.trace.impl.SpanContext) parent.get();
+ context = new com.datadoghq.trace.impl.SpanContext(
+ p.getTraceId(),
+ generatedId,
+ p.getSpanId(),
+ null,
+ null,
+ null,
+ false,
+ null,
+ null,
+ true);
} else {
-
+ context = new com.datadoghq.trace.impl.SpanContext(
+ generatedId,
+ generatedId,
+ 0L,
+ null,
+ null,
+ null,
+ false,
+ null,
+ null,
+ true);
}
- return null;
+ return context;
}
public Iterable<Map.Entry<String, String>> baggageItems() {
return null;
}
}
+
+ long generateNewId() {
+ return UUID.randomUUID().getMostSignificantBits();
+ }
}

MODIFY
src/test/java/com/datadoghq/trace/impl/SpanBuilderTest.java
src/test/java/com/datadoghq/trace/impl/SpanBuilderTest.java
package com.datadoghq.trace.impl;
-import org.assertj.core.data.MapEntry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import java.util.Collections;
-import java.util.Date;
import java.util.HashMap;
import java.util.Map;
-import static org.assertj.core.api.Assertions.*;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
public class SpanBuilderTest {
@@ -70,9 +69,9 @@ public class SpanBuilderTest {
}
@Test
- public void shouldBuilSpanTimestampInNano() {
+ public void shouldBuildSpanTimestampInMilli() {
- final long expectedTimestamp = 487517802L * 1000 * 1000;
+ final long expectedTimestamp = 487517802L * 1000;
final String expectedName = "fakeName";
Span span = (Span) tracer
@@ -83,15 +82,39 @@ public class SpanBuilderTest {
assertThat(span.getStartTime()).isEqualTo(expectedTimestamp);
// auto-timestamp in nanoseconds
- long tick = System.nanoTime();
+ long tick = System.currentTimeMillis();
span = (Span) tracer
.buildSpan(expectedName)
.start();
// between now and now + 100ms
- assertThat(span.getStartTime()).isBetween(tick, tick * 1000 * 100);
+ assertThat(span.getStartTime()).isBetween(tick, tick + 100);
}
+ @Test
+ public void shouldLinkToParentSpan() {
+
+ final long spanId = 1L;
+ final long expectedParentId = spanId;
+
+ SpanContext mockedContext = mock(SpanContext.class);
+
+ when(mockedContext.getSpanId()).thenReturn(spanId);
+
+ final String expectedName = "fakeName";
+
+ Span span = (Span) tracer
+ .buildSpan(expectedName)
+ .asChildOf(mockedContext)
+ .start();
+
+ SpanContext actualContext = (SpanContext) span.context();
+
+ assertThat(actualContext.getParentId()).isEqualTo(expectedParentId);
+
+
+ }
+
}
\ No newline at end of file

MODIFY
src/test/java/com/datadoghq/trace/impl/TracerTest.java
src/test/java/com/datadoghq/trace/impl/TracerTest.java
@@ -3,17 +3,21 @@ package com.datadoghq.trace.impl;
import org.junit.Test;
-class TracerTest {
- @Test
- void buildSpan() {
- }
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class TracerTest {
- @Test
- void inject() {
- }
@Test
- void extract() {
+ public void testGenerateNewId() {
+
+ Tracer tracer = new Tracer();
+ long id1 = tracer.generateNewId();
+ long id2 = tracer.generateNewId();
+
+ assertThat(id1).isNotNull();
+ assertThat(id1).isNotZero();
+ assertThat(id1).isNotEqualTo(id2);
}
}
\ No newline at end of file

sync w/ @renaud.boutet