trying to fix inherits

This commit is contained in:
Guillaume Polaert 2017-04-26 15:14:50 +02:00
parent df1c528c6d
commit 1955dcd65c
6 changed files with 80 additions and 36 deletions

View File

@ -1,30 +1,32 @@
package com.datadoghq.trace;
import com.datadoghq.trace.impl.DDTags;
import com.datadoghq.trace.impl.Tracer;
import com.datadoghq.trace.writer.impl.DDAgentWriter;
import io.opentracing.Span;
import io.opentracing.Tracer;
public class Example {
public static void main(String[] args) {
Tracer tracer = new com.datadoghq.trace.impl.Tracer();
Tracer tracer = new Tracer();
Writer writer = new DDAgentWriter();
Span parent = tracer
.buildSpan("hello-world")
.withTag(DDTags.SERVICE.getKey(), "service-name")
.withServiceName("service-name")
.start();
parent.setBaggageItem("a-baggage", "value");
parent.finish();
Span child = tracer
Tracer.SpanBuilder builder = (Tracer.SpanBuilder) tracer
.buildSpan("hello-world")
.asChildOf(parent)
.asChildOf(parent);
Span child = builder
.withServiceName("service-name")
.start();
child.finish();

View File

@ -9,7 +9,7 @@ import java.util.Optional;
public class DDSpan implements io.opentracing.Span {
private final Tracer tracer;
private final String operationName;
private String operationName;
private Map<String, Object> tags;
private long startTime;
private long durationNano;
@ -19,12 +19,12 @@ public class DDSpan implements io.opentracing.Span {
Tracer tracer,
String operationName,
Map<String, Object> tags,
Optional<Long> timestamp,
Long timestamp,
DDSpanContext context) {
this.tracer = tracer;
this.operationName = operationName;
this.tags = tags;
this.startTime = timestamp.orElse(System.nanoTime());
this.startTime = Optional.ofNullable(timestamp).orElse(System.nanoTime());
this.context = context;
}
@ -86,8 +86,9 @@ public class DDSpan implements io.opentracing.Span {
return this.context.getBaggageItem(key);
}
public io.opentracing.Span setOperationName(String s) {
return null;
public io.opentracing.Span setOperationName(String operationName) {
this.operationName = operationName;
return this;
}
public io.opentracing.Span log(String s, Object o) {

View File

@ -1,8 +0,0 @@
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

@ -15,6 +15,7 @@ public class Tracer implements io.opentracing.Tracer {
return new SpanBuilder(operationName);
}
public <C> void inject(SpanContext spanContext, Format<C> format, C c) {
}
@ -23,12 +24,16 @@ public class Tracer implements io.opentracing.Tracer {
return null;
}
class SpanBuilder implements io.opentracing.Tracer.SpanBuilder {
public class SpanBuilder implements io.opentracing.Tracer.SpanBuilder {
private final String operationName;
private Map<String, Object> tags = new HashMap<>();
private Long timestamp;
private SpanContext parent;
private String serviceName;
private String resourceName;
private boolean errorFlag;
private String spanType;
public SpanBuilder(String operationName) {
this.operationName = operationName;
@ -76,6 +81,29 @@ public class Tracer implements io.opentracing.Tracer {
return this;
}
public Tracer.SpanBuilder withServiceName(String serviceName) {
this.serviceName = serviceName;
return this;
}
public Tracer.SpanBuilder withResourceName(String resourceName) {
this.resourceName = resourceName;
return this;
}
public Tracer.SpanBuilder withErrorFlag() {
this.errorFlag = true;
return this;
}
public Tracer.SpanBuilder withSpanType(String spanType) {
this.spanType = spanType;
return this;
}
public Span start() {
// build the context
@ -86,7 +114,7 @@ public class Tracer implements io.opentracing.Tracer {
Tracer.this,
this.operationName,
this.tags,
Optional.ofNullable(this.timestamp),
this.timestamp,
context);
}
@ -101,12 +129,12 @@ public class Tracer implements io.opentracing.Tracer {
p.getTraceId(),
generatedId,
p.getSpanId(),
p.getServiceName(),
(String) this.tags.getOrDefault(DDTags.RESOURCE.getKey(), ""),
Optional.ofNullable(p.getServiceName()).orElse(this.serviceName),
Optional.ofNullable(this.resourceName).orElse(this.operationName),
p.getBaggageItems(),
this.tags.containsKey(Tags.ERROR.getKey()),
errorFlag,
null,
null,
(String) this.tags.getOrDefault(Tags.SPAN_KIND.getKey(), ""),
true
);
} else {
@ -114,12 +142,12 @@ public class Tracer implements io.opentracing.Tracer {
generatedId,
generatedId,
0L,
(String) this.tags.getOrDefault(DDTags.SERVICE.getKey(), ""),
(String) this.tags.getOrDefault(DDTags.RESOURCE.getKey(), ""),
this.serviceName,
Optional.ofNullable(this.resourceName).orElse(this.operationName),
null,
errorFlag,
null,
this.tags.containsKey(Tags.ERROR.getKey()),
null,
(String) this.tags.getOrDefault(Tags.SPAN_KIND.getKey(), ""),
true);
}

View File

@ -174,8 +174,8 @@ public class DDSpanBuilderTest {
DDSpan parent = (DDSpan) tracer
.buildSpan(expectedName)
.withTag(DDTags.SERVICE.getKey(), expectedServiceName)
.withTag(DDTags.RESOURCE.getKey(), expectedResourceName)
.withServiceName(expectedServiceName)
.withResourceName(expectedResourceName)
.start();
parent.setBaggageItem(expectedBaggageItemKey, expectedBaggageItemValue);

View File

@ -1,10 +1,12 @@
package com.datadoghq.trace.impl;
import io.opentracing.Span;
import org.junit.Test;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.floatThat;
import static org.mockito.Mockito.mock;
@ -12,22 +14,20 @@ public class DDSpanTest {
@Test
public void shouldAddBaggageItem() {
public void testBaggageItem() {
Tracer mockedTracer = mock(Tracer.class);
DDSpanContext context = new DDSpanContext();
final String expectedBaggageItemKey = "fakeKey";
final String expectedBaggageItemValue = "fakeValue";
DDSpan span = new DDSpan(
mockedTracer,
null,
"fakeName",
null,
Optional.empty(),
null,
context
);
@ -39,4 +39,25 @@ public class DDSpanTest {
}
@Test
public void testGetSetOperationName() {
final String expectedOperationName1 = "fake";
final String expectedOperationName2 = "fake";
DDSpan span = new DDSpan(
null,
expectedOperationName1,
null,
null,
null
);
assertThat(span.getOperationName()).isEqualTo(expectedOperationName1);
span.setOperationName(expectedOperationName2);
assertThat(span.getOperationName()).isEqualTo(expectedOperationName1);
}
}