sync w/ @renaud.boutet
This commit is contained in:
parent
7f0a2d643e
commit
a8535a0365
5
pom.xml
5
pom.xml
|
@ -28,6 +28,11 @@
|
||||||
<version>3.6.2</version>
|
<version>3.6.2</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<version>2.7.22</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -7,6 +7,7 @@ import io.opentracing.propagation.Format;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
|
||||||
public class Tracer implements io.opentracing.Tracer {
|
public class Tracer implements io.opentracing.Tracer {
|
||||||
|
@ -28,14 +29,15 @@ public class Tracer implements io.opentracing.Tracer {
|
||||||
private final String operationName;
|
private final String operationName;
|
||||||
private Map<String, Object> tags = new HashMap();
|
private Map<String, Object> tags = new HashMap();
|
||||||
private Long timestamp;
|
private Long timestamp;
|
||||||
private Optional<SpanContext> parent;
|
private Optional<SpanContext> parent = Optional.empty();
|
||||||
|
|
||||||
public SpanBuilder(String operationName) {
|
public SpanBuilder(String operationName) {
|
||||||
this.operationName = operationName;
|
this.operationName = operationName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public io.opentracing.Tracer.SpanBuilder asChildOf(SpanContext spanContext) {
|
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) {
|
public io.opentracing.Tracer.SpanBuilder asChildOf(Span span) {
|
||||||
|
@ -73,36 +75,57 @@ public class Tracer implements io.opentracing.Tracer {
|
||||||
public Span start() {
|
public Span start() {
|
||||||
|
|
||||||
// build the context
|
// build the context
|
||||||
SpanContext context = buildNewSpanContext();
|
SpanContext context = buildTheSpanContext();
|
||||||
|
|
||||||
return new com.datadoghq.trace.impl.Span(
|
return new com.datadoghq.trace.impl.Span(
|
||||||
|
Tracer.this,
|
||||||
this.operationName,
|
this.operationName,
|
||||||
this.tags,
|
this.tags,
|
||||||
Optional.ofNullable(this.timestamp),
|
Optional.ofNullable(this.timestamp),
|
||||||
context);
|
context);
|
||||||
}
|
}
|
||||||
|
|
||||||
private SpanContext buildNewSpanContext() {
|
private SpanContext buildTheSpanContext() {
|
||||||
|
|
||||||
Optional<Object> parentContext = Optional.ofNullable(this.parent);
|
SpanContext context = null;
|
||||||
SpanContext context = new com.datadoghq.trace.impl.SpanContext(
|
|
||||||
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (this.parent == null) {
|
|
||||||
long traceId = 123L;
|
|
||||||
|
|
||||||
|
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 {
|
} 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() {
|
public Iterable<Map.Entry<String, String>> baggageItems() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long generateNewId() {
|
||||||
|
return UUID.randomUUID().getMostSignificantBits();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,15 @@
|
||||||
package com.datadoghq.trace.impl;
|
package com.datadoghq.trace.impl;
|
||||||
|
|
||||||
import org.assertj.core.data.MapEntry;
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
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 {
|
public class SpanBuilderTest {
|
||||||
|
|
||||||
|
@ -70,9 +69,9 @@ public class SpanBuilderTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldBuilSpanTimestampInNano() {
|
public void shouldBuildSpanTimestampInMilli() {
|
||||||
|
|
||||||
final long expectedTimestamp = 487517802L * 1000 * 1000;
|
final long expectedTimestamp = 487517802L * 1000;
|
||||||
final String expectedName = "fakeName";
|
final String expectedName = "fakeName";
|
||||||
|
|
||||||
Span span = (Span) tracer
|
Span span = (Span) tracer
|
||||||
|
@ -83,15 +82,39 @@ public class SpanBuilderTest {
|
||||||
assertThat(span.getStartTime()).isEqualTo(expectedTimestamp);
|
assertThat(span.getStartTime()).isEqualTo(expectedTimestamp);
|
||||||
|
|
||||||
// auto-timestamp in nanoseconds
|
// auto-timestamp in nanoseconds
|
||||||
long tick = System.nanoTime();
|
long tick = System.currentTimeMillis();
|
||||||
span = (Span) tracer
|
span = (Span) tracer
|
||||||
.buildSpan(expectedName)
|
.buildSpan(expectedName)
|
||||||
.start();
|
.start();
|
||||||
|
|
||||||
// between now and now + 100ms
|
// 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);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -3,17 +3,21 @@ package com.datadoghq.trace.impl;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
class TracerTest {
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
@Test
|
|
||||||
void buildSpan() {
|
public class TracerTest {
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void inject() {
|
public void testGenerateNewId() {
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
Tracer tracer = new Tracer();
|
||||||
void extract() {
|
long id1 = tracer.generateNewId();
|
||||||
|
long id2 = tracer.generateNewId();
|
||||||
|
|
||||||
|
assertThat(id1).isNotNull();
|
||||||
|
assertThat(id1).isNotZero();
|
||||||
|
assertThat(id1).isNotEqualTo(id2);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue