pre merge
This commit is contained in:
parent
af244a039d
commit
c769fa6386
|
@ -1,7 +1,9 @@
|
||||||
package com.datadoghq.trace.impl;
|
package com.datadoghq.trace.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public class SpanContext implements io.opentracing.SpanContext {
|
public class SpanContext implements io.opentracing.SpanContext {
|
||||||
|
|
||||||
|
@ -34,7 +36,8 @@ public class SpanContext implements io.opentracing.SpanContext {
|
||||||
this.traceId = traceId;
|
this.traceId = traceId;
|
||||||
this.spanId = spanId;
|
this.spanId = spanId;
|
||||||
this.parentId = parentId;
|
this.parentId = parentId;
|
||||||
this.baggageItems = baggageItems;
|
Optional<Map<String, String>> baggage = Optional.ofNullable(baggageItems);
|
||||||
|
this.baggageItems = baggage.orElse(Collections.emptyMap());
|
||||||
this.errorFlag = errorFlag;
|
this.errorFlag = errorFlag;
|
||||||
this.metrics = metrics;
|
this.metrics = metrics;
|
||||||
|
|
||||||
|
@ -44,7 +47,7 @@ public class SpanContext implements io.opentracing.SpanContext {
|
||||||
|
|
||||||
|
|
||||||
public Iterable<Map.Entry<String, String>> baggageItems() {
|
public Iterable<Map.Entry<String, String>> baggageItems() {
|
||||||
return null;
|
return this.baggageItems.entrySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getTraceId() {
|
public long getTraceId() {
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
package com.datadoghq.trace.impl;
|
package com.datadoghq.trace.impl;
|
||||||
|
|
||||||
|
import io.opentracing.References;
|
||||||
import io.opentracing.Span;
|
import io.opentracing.Span;
|
||||||
import io.opentracing.SpanContext;
|
import io.opentracing.SpanContext;
|
||||||
import io.opentracing.propagation.Format;
|
import io.opentracing.propagation.Format;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
|
|
||||||
public class Tracer implements io.opentracing.Tracer {
|
public class Tracer implements io.opentracing.Tracer {
|
||||||
|
@ -27,27 +25,33 @@ public class Tracer implements io.opentracing.Tracer {
|
||||||
class SpanBuilder implements io.opentracing.Tracer.SpanBuilder {
|
class SpanBuilder implements io.opentracing.Tracer.SpanBuilder {
|
||||||
|
|
||||||
private final String operationName;
|
private final String operationName;
|
||||||
private Map<String, Object> tags = new HashMap();
|
private HashMap tags = new HashMap();
|
||||||
private Long timestamp;
|
private Long timestamp;
|
||||||
private Optional<SpanContext> parent = Optional.empty();
|
private SpanContext parent;
|
||||||
|
|
||||||
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) {
|
||||||
this.parent = Optional.ofNullable(spanContext);
|
this.parent = spanContext;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public io.opentracing.Tracer.SpanBuilder asChildOf(Span span) {
|
public io.opentracing.Tracer.SpanBuilder asChildOf(Span span) {
|
||||||
return null;
|
return asChildOf(span.context());
|
||||||
}
|
}
|
||||||
|
|
||||||
public io.opentracing.Tracer.SpanBuilder addReference(String s, SpanContext spanContext) {
|
public io.opentracing.Tracer.SpanBuilder addReference(String referenceType, SpanContext spanContext) {
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (References.CHILD_OF.equals(referenceType) || References.FOLLOWS_FROM.equals(referenceType)) {
|
||||||
|
// @todo: implements the notion of referenceType, currently only link a span to a parent one
|
||||||
|
return asChildOf(spanContext);
|
||||||
|
} else {
|
||||||
|
// do nothing
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public io.opentracing.Tracer.SpanBuilder withTag(String tag, Number number) {
|
public io.opentracing.Tracer.SpanBuilder withTag(String tag, Number number) {
|
||||||
return withTag(tag, (Object) number);
|
return withTag(tag, (Object) number);
|
||||||
|
@ -66,7 +70,6 @@ public class Tracer implements io.opentracing.Tracer {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public io.opentracing.Tracer.SpanBuilder withStartTimestamp(long timestamp) {
|
public io.opentracing.Tracer.SpanBuilder withStartTimestamp(long timestamp) {
|
||||||
this.timestamp = timestamp;
|
this.timestamp = timestamp;
|
||||||
return this;
|
return this;
|
||||||
|
@ -87,11 +90,11 @@ public class Tracer implements io.opentracing.Tracer {
|
||||||
|
|
||||||
private SpanContext buildTheSpanContext() {
|
private SpanContext buildTheSpanContext() {
|
||||||
|
|
||||||
SpanContext context = null;
|
SpanContext context;
|
||||||
|
|
||||||
long generatedId = generateNewId();
|
long generatedId = generateNewId();
|
||||||
if (parent.isPresent()) {
|
if (parent != null) {
|
||||||
com.datadoghq.trace.impl.SpanContext p = (com.datadoghq.trace.impl.SpanContext) parent.get();
|
com.datadoghq.trace.impl.SpanContext p = (com.datadoghq.trace.impl.SpanContext) parent;
|
||||||
context = new com.datadoghq.trace.impl.SpanContext(
|
context = new com.datadoghq.trace.impl.SpanContext(
|
||||||
p.getTraceId(),
|
p.getTraceId(),
|
||||||
generatedId,
|
generatedId,
|
||||||
|
@ -121,7 +124,10 @@ public class Tracer implements io.opentracing.Tracer {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Iterable<Map.Entry<String, String>> baggageItems() {
|
public Iterable<Map.Entry<String, String>> baggageItems() {
|
||||||
return null;
|
if (parent == null) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
return parent.baggageItems();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.datadoghq.trace.impl;
|
package com.datadoghq.trace.impl;
|
||||||
|
|
||||||
|
import io.opentracing.References;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -100,21 +101,87 @@ public class SpanBuilderTest {
|
||||||
final long expectedParentId = spanId;
|
final long expectedParentId = spanId;
|
||||||
|
|
||||||
SpanContext mockedContext = mock(SpanContext.class);
|
SpanContext mockedContext = mock(SpanContext.class);
|
||||||
|
Span mockedSpan = mock(Span.class);
|
||||||
|
|
||||||
|
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
|
Span span = (Span) tracer
|
||||||
.buildSpan(expectedName)
|
.buildSpan(expectedName)
|
||||||
.asChildOf(mockedContext)
|
.asChildOf(mockedSpan)
|
||||||
.start();
|
.start();
|
||||||
|
|
||||||
SpanContext actualContext = (SpanContext) span.context();
|
SpanContext actualContext = (SpanContext) span.context();
|
||||||
|
|
||||||
assertThat(actualContext.getParentId()).isEqualTo(expectedParentId);
|
assertThat(actualContext.getParentId()).isEqualTo(expectedParentId);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldLinkViaReferenceType() {
|
||||||
|
|
||||||
|
|
||||||
|
final long spanId = 223L;
|
||||||
|
final long expectedParentId = spanId;
|
||||||
|
|
||||||
|
SpanContext mockedContext = mock(SpanContext.class);
|
||||||
|
when(mockedContext.getSpanId()).thenReturn(spanId);
|
||||||
|
|
||||||
|
final String expectedName = "fakeName";
|
||||||
|
|
||||||
|
|
||||||
|
// case 1, using a CHILD_OF ref
|
||||||
|
Span span = (Span) tracer
|
||||||
|
.buildSpan(expectedName)
|
||||||
|
.addReference(References.CHILD_OF, mockedContext)
|
||||||
|
.start();
|
||||||
|
|
||||||
|
SpanContext actualContext = (SpanContext) span.context();
|
||||||
|
assertThat(actualContext.getParentId()).isEqualTo(expectedParentId);
|
||||||
|
|
||||||
|
|
||||||
|
// case 2, using a FOLLOW_FROM ref
|
||||||
|
span = (Span) tracer
|
||||||
|
.buildSpan(expectedName)
|
||||||
|
.addReference(References.FOLLOWS_FROM, mockedContext)
|
||||||
|
.start();
|
||||||
|
|
||||||
|
actualContext = (SpanContext) span.context();
|
||||||
|
assertThat(actualContext.getParentId()).isEqualTo(expectedParentId);
|
||||||
|
|
||||||
|
// case 2, using a WFT ref, should not be linked to the previous
|
||||||
|
span = (Span) tracer
|
||||||
|
.buildSpan(expectedName)
|
||||||
|
.addReference("WTF", mockedContext)
|
||||||
|
.start();
|
||||||
|
|
||||||
|
actualContext = (SpanContext) span.context();
|
||||||
|
assertThat(actualContext.getParentId()).isEqualTo(0L);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldInheritOfBaggage() {
|
||||||
|
|
||||||
|
final String expectedName = "fakeName";
|
||||||
|
final String expectedBaggageItemKey = "fakeKey";
|
||||||
|
final String expectedBaggageItemValue = "fakeValue";
|
||||||
|
|
||||||
|
Span parent = (Span) tracer
|
||||||
|
.buildSpan(expectedName)
|
||||||
|
.start();
|
||||||
|
|
||||||
|
assertThat(parent.getOperationName()).isEqualTo(expectedName);
|
||||||
|
assertThat(parent.context().baggageItems()).isEmpty();
|
||||||
|
|
||||||
|
Span span = (Span) tracer.buildSpan(expectedName).start();
|
||||||
|
|
||||||
|
assertThat(span.getOperationName()).isEqualTo(expectedName);
|
||||||
|
assertThat(span.getBaggageItem(expectedBaggageItemKey)).isEqualTo(expectedBaggageItemValue);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,8 +1,42 @@
|
||||||
package com.datadoghq.trace.impl;
|
package com.datadoghq.trace.impl;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
|
||||||
public class SpanTest {
|
public class SpanTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldAddBaggageItem(){
|
||||||
|
|
||||||
|
|
||||||
|
Tracer mockedTracer = mock(Tracer.class);
|
||||||
|
SpanContext mockedContext = mock(SpanContext.class);
|
||||||
|
|
||||||
|
final String expectedBaggageItemKey = "fakeKey";
|
||||||
|
final String expectedBaggageItemValue = "fakeValue";
|
||||||
|
|
||||||
|
|
||||||
|
Span span = new Span(
|
||||||
|
mockedTracer,
|
||||||
|
"fakeName",
|
||||||
|
null,
|
||||||
|
Optional.empty(),
|
||||||
|
mockedContext
|
||||||
|
);
|
||||||
|
|
||||||
|
assertThat(span.context().baggageItems()).isEmpty();
|
||||||
|
|
||||||
|
span.setBaggageItem(expectedBaggageItemKey, expectedBaggageItemValue);
|
||||||
|
|
||||||
|
assertThat(span.getBaggageItem(expectedBaggageItemKey)).isEqualTo(expectedBaggageItemValue);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue