Add thread name and id to each span as additional tags.
This commit is contained in:
parent
b824be1295
commit
79e0d3c027
|
@ -14,10 +14,6 @@
|
|||
<description>Datadog core library</description>
|
||||
<url>https://github.com/datadog/dd-trace-java</url>
|
||||
|
||||
<properties>
|
||||
<java.version>1.6</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Opentracing core -->
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.datadoghq.trace;
|
|||
|
||||
import com.datadoghq.trace.integration.DDSpanContextDecorator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.google.common.collect.Maps;
|
||||
import io.opentracing.tag.Tags;
|
||||
|
||||
import java.util.*;
|
||||
|
@ -21,6 +22,8 @@ public class DDSpanContext implements io.opentracing.SpanContext {
|
|||
private final long traceId;
|
||||
private final long spanId;
|
||||
private final long parentId;
|
||||
private final String threadName = Thread.currentThread().getName();
|
||||
private final long threadId = Thread.currentThread().getId();
|
||||
private Map<String, String> baggageItems;
|
||||
|
||||
// DD attributes
|
||||
|
@ -126,7 +129,7 @@ public class DDSpanContext implements io.opentracing.SpanContext {
|
|||
public boolean getErrorFlag() {
|
||||
return errorFlag;
|
||||
}
|
||||
|
||||
|
||||
public void setErrorFlag(boolean errorFlag) {
|
||||
this.errorFlag = errorFlag;
|
||||
}
|
||||
|
@ -193,6 +196,11 @@ public class DDSpanContext implements io.opentracing.SpanContext {
|
|||
}
|
||||
|
||||
public synchronized Map<String, Object> getTags() {
|
||||
if(tags.isEmpty()) {
|
||||
tags = Maps.newHashMapWithExpectedSize(2);
|
||||
}
|
||||
tags.put(DDTags.THREAD_NAME, threadName);
|
||||
tags.put(DDTags.THREAD_ID, threadId);
|
||||
return Collections.unmodifiableMap(tags);
|
||||
}
|
||||
|
||||
|
|
|
@ -4,4 +4,6 @@ public class DDTags {
|
|||
public static final String SPAN_TYPE = "span-type";
|
||||
public static final String SERVICE_NAME = "service-name";
|
||||
public static final String RESOURCE_NAME = "resource-name";
|
||||
public static final String THREAD_NAME = "thread-name";
|
||||
public static final String THREAD_ID = "thread-id";
|
||||
}
|
||||
|
|
|
@ -66,7 +66,7 @@ public class DDSpanBuilderTest {
|
|||
.start();
|
||||
|
||||
assertThat(span.getTags()).isNotNull();
|
||||
assertThat(span.getTags()).isEmpty();
|
||||
assertThat(span.getTags().size()).isEqualTo(2);
|
||||
|
||||
// with all custom fields provided
|
||||
final String expectedResource = "fakeResource";
|
||||
|
@ -88,7 +88,8 @@ public class DDSpanBuilderTest {
|
|||
assertThat(actualContext.getErrorFlag()).isTrue();
|
||||
assertThat(actualContext.getServiceName()).isEqualTo(expectedService);
|
||||
assertThat(actualContext.getSpanType()).isEqualTo(expectedType);
|
||||
|
||||
assertThat(actualContext.getTags().get(DDTags.THREAD_NAME)).isEqualTo(Thread.currentThread().getName());
|
||||
assertThat(actualContext.getTags().get(DDTags.THREAD_ID)).isEqualTo(Thread.currentThread().getId());
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -16,15 +17,28 @@ public class DDSpanSerializationTest {
|
|||
ObjectMapper serializer;
|
||||
DDSpan span;
|
||||
DDActiveSpan activeSpan;
|
||||
Map<String,Object> expected = Maps.newHashMap();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
||||
Map<String, String> baggage = new HashMap<String, String>();
|
||||
Map<String, String> baggage = new HashMap<>();
|
||||
baggage.put("a-baggage", "value");
|
||||
Map<String, Object> tags = new HashMap<String, Object>();
|
||||
Map<String, Object> tags = new HashMap<>();
|
||||
baggage.put("k1", "v1");
|
||||
|
||||
expected.put("meta", baggage);
|
||||
expected.put("service", "service");
|
||||
expected.put("error", 0);
|
||||
expected.put("type", "type");
|
||||
expected.put("name", "operation");
|
||||
expected.put("duration", 33000);
|
||||
expected.put("resource", "operation");
|
||||
expected.put("start", 100000);
|
||||
expected.put("span_id", 2l);
|
||||
expected.put("parent_id", 0l);
|
||||
expected.put("trace_id", 1l);
|
||||
|
||||
|
||||
DDSpanContext context = new DDSpanContext(
|
||||
1L,
|
||||
|
@ -33,13 +47,16 @@ public class DDSpanSerializationTest {
|
|||
"service",
|
||||
"operation",
|
||||
null,
|
||||
baggage,
|
||||
new HashMap<String, String>(baggage),
|
||||
false,
|
||||
"type",
|
||||
tags,
|
||||
null,
|
||||
null);
|
||||
|
||||
baggage.put("thread-name", Thread.currentThread().getName());
|
||||
baggage.put("thread-id", String.valueOf(Thread.currentThread().getId()));
|
||||
|
||||
span = new DDSpan(
|
||||
100L,
|
||||
context);
|
||||
|
@ -52,22 +69,7 @@ public class DDSpanSerializationTest {
|
|||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
|
||||
|
||||
String expected = "{\"meta\":{\"a-baggage\":\"value\",\"k1\":\"v1\"},\"service\":\"service\",\"error\":0,\"type\":\"type\",\"name\":\"operation\",\"duration\":33000,\"resource\":\"operation\",\"start\":100000,\"span_id\":2,\"parent_id\":0,\"trace_id\":1}";
|
||||
// FIXME At the moment, just compare the string sizes
|
||||
try {
|
||||
assertThat(serializer.writeValueAsString(span).length()).isEqualTo(expected.length());
|
||||
|
||||
} catch (AssertionError e) {
|
||||
assertThat(serializer.writeValueAsString(span)).isEqualTo(expected);
|
||||
}
|
||||
|
||||
// try {
|
||||
// assertThat(serializer.writeValueAsString(activeSpan).length()).isEqualTo(expected.length());
|
||||
// } catch (AssertionError e) {
|
||||
// assertThat(serializer.writeValueAsString(activeSpan)).isEqualTo(expected);
|
||||
// }
|
||||
assertThat(serializer.readTree(serializer.writeValueAsString(span)))
|
||||
.isEqualTo(serializer.readTree(serializer.writeValueAsString(expected)));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue