chaining props and context

This commit is contained in:
Guillaume Polaert 2017-04-26 12:38:19 +02:00
parent 299cc57fc1
commit 8b66116aeb
6 changed files with 85 additions and 30 deletions

View File

@ -72,12 +72,13 @@ public class DDSpan implements io.opentracing.Span {
return null; return null;
} }
public io.opentracing.Span setBaggageItem(String s, String s1) { public io.opentracing.Span setBaggageItem(String key, String value) {
return null; this.context.setBaggageItem(key, value);
return this;
} }
public String getBaggageItem(String s) { public String getBaggageItem(String key) {
return null; return this.context.getBaggageItem(key);
} }
public io.opentracing.Span setOperationName(String s) { public io.opentracing.Span setOperationName(String s) {
@ -108,4 +109,7 @@ public class DDSpan implements io.opentracing.Span {
return context; return context;
} }
public DDSpanContext DDContext() {
return this.context;
}
} }

View File

@ -1,7 +1,10 @@
package com.datadoghq.trace.impl; package com.datadoghq.trace.impl;
import io.opentracing.Span;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@ -20,6 +23,19 @@ public class DDSpanContext implements io.opentracing.SpanContext {
// Sampler attributes // Sampler attributes
private boolean sampled; private boolean sampled;
// Testing purpose, @todo better mock
DDSpanContext() {
serviceName = null;
resourceName = null;
traceId = 0;
spanId = 0;
parentId = 0;
baggageItems = new HashMap<>();
errorFlag = false;
metrics = null;
spanType = null;
}
public DDSpanContext( public DDSpanContext(
long traceId, long traceId,
long spanId, long spanId,
@ -30,14 +46,15 @@ public class DDSpanContext implements io.opentracing.SpanContext {
boolean errorFlag, boolean errorFlag,
Map<String, Object> metrics, Map<String, Object> metrics,
String spanType, String spanType,
boolean sampled) { boolean sampled) { this.traceId = traceId;
this.serviceName = serviceName;
this.resourceName = resourceName;
this.traceId = traceId;
this.spanId = spanId; this.spanId = spanId;
this.parentId = parentId; this.parentId = parentId;
Optional<Map<String, String>> baggage = Optional.ofNullable(baggageItems); Optional<Map<String, String>> baggage = Optional.ofNullable(baggageItems);
this.baggageItems = baggage.orElse(Collections.emptyMap());
this.serviceName = serviceName;
this.resourceName = resourceName;
this.baggageItems = baggage.orElse(new HashMap<>());
this.errorFlag = errorFlag; this.errorFlag = errorFlag;
this.metrics = metrics; this.metrics = metrics;
@ -73,7 +90,7 @@ public class DDSpanContext implements io.opentracing.SpanContext {
} }
public boolean isErrorFlag() { public boolean isErrorFlag() {
return errorFlag; return this.errorFlag;
} }
public Map<String, Object> getMetrics() { public Map<String, Object> getMetrics() {
@ -88,4 +105,15 @@ public class DDSpanContext implements io.opentracing.SpanContext {
return sampled; return sampled;
} }
public void setBaggageItem(String key, String value) {
this.baggageItems.put(key, value);
}
public String getBaggageItem(String key) {
return this.baggageItems.get(key);
}
public Map<String, String> getBaggageItems() {
return baggageItems;
}
} }

View File

@ -0,0 +1,8 @@
package com.datadoghq.trace.impl;
import io.opentracing.tag.StringTag;
public class DDTags {
public static final StringTag RESOURCE = new StringTag("resource");
public static final StringTag SERVICE = new StringTag("service");
}

View File

@ -4,6 +4,7 @@ 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 io.opentracing.tag.Tags;
import java.util.*; import java.util.*;
@ -25,7 +26,7 @@ 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 Map<String, Object> tags = new HashMap<>();
private Long timestamp; private Long timestamp;
private SpanContext parent; private SpanContext parent;
@ -94,30 +95,31 @@ public class Tracer implements io.opentracing.Tracer {
DDSpanContext context; DDSpanContext context;
long generatedId = generateNewId(); long generatedId = generateNewId();
if (parent != null) { if (this.parent != null) {
DDSpanContext p = (DDSpanContext) parent; DDSpanContext p = (DDSpanContext) this.parent;
context = new DDSpanContext( context = new DDSpanContext(
p.getTraceId(), p.getTraceId(),
generatedId, generatedId,
p.getSpanId(), p.getSpanId(),
p.getServiceName(),
(String) this.tags.getOrDefault(DDTags.RESOURCE.getKey(), ""),
p.getBaggageItems(),
this.tags.containsKey(Tags.ERROR.getKey()),
null, null,
null, (String) this.tags.getOrDefault(Tags.SPAN_KIND.getKey(), ""),
null, true
false, );
null,
null,
true);
} else { } else {
context = new DDSpanContext( context = new DDSpanContext(
generatedId, generatedId,
generatedId, generatedId,
0L, 0L,
(String) this.tags.getOrDefault(DDTags.SERVICE.getKey(), ""),
(String) this.tags.getOrDefault(DDTags.RESOURCE.getKey(), ""),
null, null,
this.tags.containsKey(Tags.ERROR.getKey()),
null, null,
null, (String) this.tags.getOrDefault(Tags.SPAN_KIND.getKey(), ""),
false,
null,
null,
true); true);
} }

View File

@ -164,23 +164,35 @@ public class DDSpanBuilderTest {
} }
@Test @Test
public void shouldInheritOfBaggage() { public void shouldInheritOfTheDDParentAttributes() {
final String expectedName = "fakeName"; final String expectedName = "fakeName";
final String expectedServiceName = "fakeServiceName";
final String expectedResourceName = "fakeResourceName";
final String expectedBaggageItemKey = "fakeKey"; final String expectedBaggageItemKey = "fakeKey";
final String expectedBaggageItemValue = "fakeValue"; final String expectedBaggageItemValue = "fakeValue";
Map<String, String> baggage = new HashMap<String, String>() {{
put("service", expectedServiceName);
}};
DDSpan parent = (DDSpan) tracer DDSpan parent = (DDSpan) tracer
.buildSpan(expectedName) .buildSpan(expectedName)
.withTag(DDTags.SERVICE.getKey(), expectedServiceName)
.withTag(DDTags.RESOURCE.getKey(), expectedResourceName)
.start(); .start();
assertThat(parent.getOperationName()).isEqualTo(expectedName); parent.setBaggageItem(expectedBaggageItemKey, expectedBaggageItemValue);
assertThat(parent.context().baggageItems()).isEmpty();
DDSpan span = (DDSpan) tracer.buildSpan(expectedName).start(); DDSpan span = (DDSpan) tracer
.buildSpan(expectedName)
.asChildOf(parent)
.start();
assertThat(span.getOperationName()).isEqualTo(expectedName); assertThat(span.getOperationName()).isEqualTo(expectedName);
assertThat(span.getBaggageItem(expectedBaggageItemKey)).isEqualTo(expectedBaggageItemValue); assertThat(span.getBaggageItem(expectedBaggageItemKey)).isEqualTo(expectedBaggageItemValue);
assertThat(span.DDContext().getServiceName()).isEqualTo(expectedServiceName);
assertThat(span.DDContext().getResourceName()).isNotEqualTo(expectedResourceName);
} }

View File

@ -16,7 +16,8 @@ public class DDSpanTest {
Tracer mockedTracer = mock(Tracer.class); Tracer mockedTracer = mock(Tracer.class);
DDSpanContext mockedContext = mock(DDSpanContext.class); DDSpanContext context = new DDSpanContext();
final String expectedBaggageItemKey = "fakeKey"; final String expectedBaggageItemKey = "fakeKey";
final String expectedBaggageItemValue = "fakeValue"; final String expectedBaggageItemValue = "fakeValue";
@ -27,7 +28,7 @@ public class DDSpanTest {
"fakeName", "fakeName",
null, null,
Optional.empty(), Optional.empty(),
mockedContext context
); );
assertThat(span.context().baggageItems()).isEmpty(); assertThat(span.context().baggageItems()).isEmpty();