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;
}
public io.opentracing.Span setBaggageItem(String s, String s1) {
return null;
public io.opentracing.Span setBaggageItem(String key, String value) {
this.context.setBaggageItem(key, value);
return this;
}
public String getBaggageItem(String s) {
return null;
public String getBaggageItem(String key) {
return this.context.getBaggageItem(key);
}
public io.opentracing.Span setOperationName(String s) {
@ -107,5 +108,8 @@ public class DDSpan implements io.opentracing.Span {
public DDSpanContext getContext(){
return context;
}
public DDSpanContext DDContext() {
return this.context;
}
}

View File

@ -1,7 +1,10 @@
package com.datadoghq.trace.impl;
import io.opentracing.Span;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
@ -20,6 +23,19 @@ public class DDSpanContext implements io.opentracing.SpanContext {
// Sampler attributes
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(
long traceId,
long spanId,
@ -30,14 +46,15 @@ public class DDSpanContext implements io.opentracing.SpanContext {
boolean errorFlag,
Map<String, Object> metrics,
String spanType,
boolean sampled) {
this.serviceName = serviceName;
this.resourceName = resourceName;
this.traceId = traceId;
boolean sampled) { this.traceId = traceId;
this.spanId = spanId;
this.parentId = parentId;
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.metrics = metrics;
@ -73,7 +90,7 @@ public class DDSpanContext implements io.opentracing.SpanContext {
}
public boolean isErrorFlag() {
return errorFlag;
return this.errorFlag;
}
public Map<String, Object> getMetrics() {
@ -88,4 +105,15 @@ public class DDSpanContext implements io.opentracing.SpanContext {
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.SpanContext;
import io.opentracing.propagation.Format;
import io.opentracing.tag.Tags;
import java.util.*;
@ -25,7 +26,7 @@ public class Tracer implements io.opentracing.Tracer {
class SpanBuilder implements io.opentracing.Tracer.SpanBuilder {
private final String operationName;
private Map<String, Object> tags = new HashMap();
private Map<String, Object> tags = new HashMap<>();
private Long timestamp;
private SpanContext parent;
@ -94,30 +95,31 @@ public class Tracer implements io.opentracing.Tracer {
DDSpanContext context;
long generatedId = generateNewId();
if (parent != null) {
DDSpanContext p = (DDSpanContext) parent;
if (this.parent != null) {
DDSpanContext p = (DDSpanContext) this.parent;
context = new DDSpanContext(
p.getTraceId(),
generatedId,
p.getSpanId(),
p.getServiceName(),
(String) this.tags.getOrDefault(DDTags.RESOURCE.getKey(), ""),
p.getBaggageItems(),
this.tags.containsKey(Tags.ERROR.getKey()),
null,
null,
null,
false,
null,
null,
true);
(String) this.tags.getOrDefault(Tags.SPAN_KIND.getKey(), ""),
true
);
} else {
context = new DDSpanContext(
generatedId,
generatedId,
0L,
(String) this.tags.getOrDefault(DDTags.SERVICE.getKey(), ""),
(String) this.tags.getOrDefault(DDTags.RESOURCE.getKey(), ""),
null,
this.tags.containsKey(Tags.ERROR.getKey()),
null,
null,
false,
null,
null,
(String) this.tags.getOrDefault(Tags.SPAN_KIND.getKey(), ""),
true);
}

View File

@ -164,23 +164,35 @@ public class DDSpanBuilderTest {
}
@Test
public void shouldInheritOfBaggage() {
public void shouldInheritOfTheDDParentAttributes() {
final String expectedName = "fakeName";
final String expectedServiceName = "fakeServiceName";
final String expectedResourceName = "fakeResourceName";
final String expectedBaggageItemKey = "fakeKey";
final String expectedBaggageItemValue = "fakeValue";
Map<String, String> baggage = new HashMap<String, String>() {{
put("service", expectedServiceName);
}};
DDSpan parent = (DDSpan) tracer
.buildSpan(expectedName)
.withTag(DDTags.SERVICE.getKey(), expectedServiceName)
.withTag(DDTags.RESOURCE.getKey(), expectedResourceName)
.start();
assertThat(parent.getOperationName()).isEqualTo(expectedName);
assertThat(parent.context().baggageItems()).isEmpty();
parent.setBaggageItem(expectedBaggageItemKey, expectedBaggageItemValue);
DDSpan span = (DDSpan) tracer.buildSpan(expectedName).start();
DDSpan span = (DDSpan) tracer
.buildSpan(expectedName)
.asChildOf(parent)
.start();
assertThat(span.getOperationName()).isEqualTo(expectedName);
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);
DDSpanContext mockedContext = mock(DDSpanContext.class);
DDSpanContext context = new DDSpanContext();
final String expectedBaggageItemKey = "fakeKey";
final String expectedBaggageItemValue = "fakeValue";
@ -27,7 +28,7 @@ public class DDSpanTest {
"fakeName",
null,
Optional.empty(),
mockedContext
context
);
assertThat(span.context().baggageItems()).isEmpty();