Add thread name and id to each span as additional tags.

This commit is contained in:
Tyler Benson 2017-06-22 17:54:43 -04:00
parent b824be1295
commit 79e0d3c027
5 changed files with 36 additions and 27 deletions

View File

@ -14,10 +14,6 @@
<description>Datadog core library</description> <description>Datadog core library</description>
<url>https://github.com/datadog/dd-trace-java</url> <url>https://github.com/datadog/dd-trace-java</url>
<properties>
<java.version>1.6</java.version>
</properties>
<dependencies> <dependencies>
<!-- Opentracing core --> <!-- Opentracing core -->

View File

@ -3,6 +3,7 @@ package com.datadoghq.trace;
import com.datadoghq.trace.integration.DDSpanContextDecorator; import com.datadoghq.trace.integration.DDSpanContextDecorator;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Maps;
import io.opentracing.tag.Tags; import io.opentracing.tag.Tags;
import java.util.*; import java.util.*;
@ -21,6 +22,8 @@ public class DDSpanContext implements io.opentracing.SpanContext {
private final long traceId; private final long traceId;
private final long spanId; private final long spanId;
private final long parentId; private final long parentId;
private final String threadName = Thread.currentThread().getName();
private final long threadId = Thread.currentThread().getId();
private Map<String, String> baggageItems; private Map<String, String> baggageItems;
// DD attributes // DD attributes
@ -193,6 +196,11 @@ public class DDSpanContext implements io.opentracing.SpanContext {
} }
public synchronized Map<String, Object> getTags() { 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); return Collections.unmodifiableMap(tags);
} }

View File

@ -4,4 +4,6 @@ public class DDTags {
public static final String SPAN_TYPE = "span-type"; public static final String SPAN_TYPE = "span-type";
public static final String SERVICE_NAME = "service-name"; public static final String SERVICE_NAME = "service-name";
public static final String RESOURCE_NAME = "resource-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";
} }

View File

@ -66,7 +66,7 @@ public class DDSpanBuilderTest {
.start(); .start();
assertThat(span.getTags()).isNotNull(); assertThat(span.getTags()).isNotNull();
assertThat(span.getTags()).isEmpty(); assertThat(span.getTags().size()).isEqualTo(2);
// with all custom fields provided // with all custom fields provided
final String expectedResource = "fakeResource"; final String expectedResource = "fakeResource";
@ -88,7 +88,8 @@ public class DDSpanBuilderTest {
assertThat(actualContext.getErrorFlag()).isTrue(); assertThat(actualContext.getErrorFlag()).isTrue();
assertThat(actualContext.getServiceName()).isEqualTo(expectedService); assertThat(actualContext.getServiceName()).isEqualTo(expectedService);
assertThat(actualContext.getSpanType()).isEqualTo(expectedType); 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());
} }

View File

@ -5,6 +5,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.google.common.collect.Maps;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -16,15 +17,28 @@ public class DDSpanSerializationTest {
ObjectMapper serializer; ObjectMapper serializer;
DDSpan span; DDSpan span;
DDActiveSpan activeSpan; DDActiveSpan activeSpan;
Map<String,Object> expected = Maps.newHashMap();
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
Map<String, String> baggage = new HashMap<String, String>(); Map<String, String> baggage = new HashMap<>();
baggage.put("a-baggage", "value"); baggage.put("a-baggage", "value");
Map<String, Object> tags = new HashMap<String, Object>(); Map<String, Object> tags = new HashMap<>();
baggage.put("k1", "v1"); 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( DDSpanContext context = new DDSpanContext(
1L, 1L,
@ -33,13 +47,16 @@ public class DDSpanSerializationTest {
"service", "service",
"operation", "operation",
null, null,
baggage, new HashMap<String, String>(baggage),
false, false,
"type", "type",
tags, tags,
null, null,
null); null);
baggage.put("thread-name", Thread.currentThread().getName());
baggage.put("thread-id", String.valueOf(Thread.currentThread().getId()));
span = new DDSpan( span = new DDSpan(
100L, 100L,
context); context);
@ -52,22 +69,7 @@ public class DDSpanSerializationTest {
@Test @Test
public void test() throws Exception { public void test() throws Exception {
assertThat(serializer.readTree(serializer.writeValueAsString(span)))
.isEqualTo(serializer.readTree(serializer.writeValueAsString(expected)));
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);
// }
}
} }