Skip to content

Commit d9dc044

Browse files
author
Guillaume Polaert
authored
Merge pull request #18 from DataDog/tyler/thread-id
Add thread name and id to each span as additional tags.
2 parents b824be1 + 79e0d3c commit d9dc044

File tree

5 files changed

+36
-27
lines changed

5 files changed

+36
-27
lines changed

dd-trace/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
<description>Datadog core library</description>
1515
<url>https://github.com/datadog/dd-trace-java</url>
1616

17-
<properties>
18-
<java.version>1.6</java.version>
19-
</properties>
20-
2117
<dependencies>
2218

2319
<!-- Opentracing core -->

dd-trace/src/main/java/com/datadoghq/trace/DDSpanContext.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import com.datadoghq.trace.integration.DDSpanContextDecorator;
55
import com.fasterxml.jackson.annotation.JsonIgnore;
6+
import com.google.common.collect.Maps;
67
import io.opentracing.tag.Tags;
78

89
import java.util.*;
@@ -21,6 +22,8 @@ public class DDSpanContext implements io.opentracing.SpanContext {
2122
private final long traceId;
2223
private final long spanId;
2324
private final long parentId;
25+
private final String threadName = Thread.currentThread().getName();
26+
private final long threadId = Thread.currentThread().getId();
2427
private Map<String, String> baggageItems;
2528

2629
// DD attributes
@@ -126,7 +129,7 @@ public String getResourceName() {
126129
public boolean getErrorFlag() {
127130
return errorFlag;
128131
}
129-
132+
130133
public void setErrorFlag(boolean errorFlag) {
131134
this.errorFlag = errorFlag;
132135
}
@@ -193,6 +196,11 @@ public synchronized void setTag(String tag, Object value) {
193196
}
194197

195198
public synchronized Map<String, Object> getTags() {
199+
if(tags.isEmpty()) {
200+
tags = Maps.newHashMapWithExpectedSize(2);
201+
}
202+
tags.put(DDTags.THREAD_NAME, threadName);
203+
tags.put(DDTags.THREAD_ID, threadId);
196204
return Collections.unmodifiableMap(tags);
197205
}
198206

dd-trace/src/main/java/com/datadoghq/trace/DDTags.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ public class DDTags {
44
public static final String SPAN_TYPE = "span-type";
55
public static final String SERVICE_NAME = "service-name";
66
public static final String RESOURCE_NAME = "resource-name";
7+
public static final String THREAD_NAME = "thread-name";
8+
public static final String THREAD_ID = "thread-id";
79
}

dd-trace/src/test/java/com/datadoghq/trace/DDSpanBuilderTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void shouldBuildMoreComplexSpan() {
6666
.start();
6767

6868
assertThat(span.getTags()).isNotNull();
69-
assertThat(span.getTags()).isEmpty();
69+
assertThat(span.getTags().size()).isEqualTo(2);
7070

7171
// with all custom fields provided
7272
final String expectedResource = "fakeResource";
@@ -88,7 +88,8 @@ public void shouldBuildMoreComplexSpan() {
8888
assertThat(actualContext.getErrorFlag()).isTrue();
8989
assertThat(actualContext.getServiceName()).isEqualTo(expectedService);
9090
assertThat(actualContext.getSpanType()).isEqualTo(expectedType);
91-
91+
assertThat(actualContext.getTags().get(DDTags.THREAD_NAME)).isEqualTo(Thread.currentThread().getName());
92+
assertThat(actualContext.getTags().get(DDTags.THREAD_ID)).isEqualTo(Thread.currentThread().getId());
9293

9394
}
9495

dd-trace/src/test/java/com/datadoghq/trace/DDSpanSerializationTest.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.HashMap;
66
import java.util.Map;
77

8+
import com.google.common.collect.Maps;
89
import org.junit.Before;
910
import org.junit.Test;
1011

@@ -16,15 +17,28 @@ public class DDSpanSerializationTest {
1617
ObjectMapper serializer;
1718
DDSpan span;
1819
DDActiveSpan activeSpan;
20+
Map<String,Object> expected = Maps.newHashMap();
1921

2022
@Before
2123
public void setUp() throws Exception {
2224

23-
Map<String, String> baggage = new HashMap<String, String>();
25+
Map<String, String> baggage = new HashMap<>();
2426
baggage.put("a-baggage", "value");
25-
Map<String, Object> tags = new HashMap<String, Object>();
27+
Map<String, Object> tags = new HashMap<>();
2628
baggage.put("k1", "v1");
2729

30+
expected.put("meta", baggage);
31+
expected.put("service", "service");
32+
expected.put("error", 0);
33+
expected.put("type", "type");
34+
expected.put("name", "operation");
35+
expected.put("duration", 33000);
36+
expected.put("resource", "operation");
37+
expected.put("start", 100000);
38+
expected.put("span_id", 2l);
39+
expected.put("parent_id", 0l);
40+
expected.put("trace_id", 1l);
41+
2842

2943
DDSpanContext context = new DDSpanContext(
3044
1L,
@@ -33,13 +47,16 @@ public void setUp() throws Exception {
3347
"service",
3448
"operation",
3549
null,
36-
baggage,
50+
new HashMap<String, String>(baggage),
3751
false,
3852
"type",
3953
tags,
4054
null,
4155
null);
4256

57+
baggage.put("thread-name", Thread.currentThread().getName());
58+
baggage.put("thread-id", String.valueOf(Thread.currentThread().getId()));
59+
4360
span = new DDSpan(
4461
100L,
4562
context);
@@ -52,22 +69,7 @@ public void setUp() throws Exception {
5269

5370
@Test
5471
public void test() throws Exception {
55-
56-
57-
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}";
58-
// FIXME At the moment, just compare the string sizes
59-
try {
60-
assertThat(serializer.writeValueAsString(span).length()).isEqualTo(expected.length());
61-
62-
} catch (AssertionError e) {
63-
assertThat(serializer.writeValueAsString(span)).isEqualTo(expected);
64-
}
65-
66-
// try {
67-
// assertThat(serializer.writeValueAsString(activeSpan).length()).isEqualTo(expected.length());
68-
// } catch (AssertionError e) {
69-
// assertThat(serializer.writeValueAsString(activeSpan)).isEqualTo(expected);
70-
// }
72+
assertThat(serializer.readTree(serializer.writeValueAsString(span)))
73+
.isEqualTo(serializer.readTree(serializer.writeValueAsString(expected)));
7174
}
72-
7375
}

0 commit comments

Comments
 (0)