Skip to content

Commit 947b900

Browse files
committed
Add lang meta to the span context
1 parent ccb7904 commit 947b900

File tree

3 files changed

+109
-100
lines changed

3 files changed

+109
-100
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
package com.datadoghq.trace;
22

33

4-
import java.util.ArrayList;
5-
import java.util.Collections;
6-
import java.util.HashMap;
7-
import java.util.List;
8-
import java.util.Map;
9-
104
import com.datadoghq.trace.integration.DDSpanContextDecorator;
115
import com.fasterxml.jackson.annotation.JsonIgnore;
12-
136
import io.opentracing.tag.Tags;
147

8+
import java.util.*;
9+
1510
/**
1611
* SpanContext represents Span state that must propagate to descendant Spans and across process boundaries.
1712
* <p>
@@ -21,6 +16,7 @@
2116
*/
2217
public class DDSpanContext implements io.opentracing.SpanContext {
2318

19+
public static final String LANGUAGE_FIELDNAME = "lang";
2420
// Opentracing attributes
2521
private final long traceId;
2622
private final long spanId;
@@ -53,6 +49,8 @@ public class DDSpanContext implements io.opentracing.SpanContext {
5349
* Each span have an operation name describing the current span
5450
*/
5551
private String operationName;
52+
53+
5654
/**
5755
* Tags are associated to the current span, they will not propagate to the children span
5856
*/

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

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
package com.datadoghq.trace;
22

3-
import java.util.ArrayList;
4-
import java.util.Collections;
5-
import java.util.HashMap;
6-
import java.util.List;
7-
import java.util.Map;
8-
9-
import org.slf4j.Logger;
10-
import org.slf4j.LoggerFactory;
11-
123
import com.datadoghq.trace.integration.DDSpanContextDecorator;
134
import com.datadoghq.trace.propagation.Codec;
145
import com.datadoghq.trace.propagation.HTTPCodec;
156
import com.datadoghq.trace.sampling.AllSampler;
167
import com.datadoghq.trace.sampling.Sampler;
178
import com.datadoghq.trace.writer.LoggingWriter;
189
import com.datadoghq.trace.writer.Writer;
19-
20-
import io.opentracing.ActiveSpan;
2110
import io.opentracing.BaseSpan;
2211
import io.opentracing.Span;
2312
import io.opentracing.SpanContext;
2413
import io.opentracing.propagation.Format;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
16+
17+
import java.util.*;
2518

2619

2720
/**
@@ -152,50 +145,50 @@ public void write(List<DDBaseSpan<?>> trace) {
152145
public void close() {
153146
writer.close();
154147
}
155-
148+
156149
private final ThreadLocal<DDActiveSpan> currentActiveSpan = new ThreadLocal<DDActiveSpan>();
157150

158151
@Override
159152
public DDActiveSpan activeSpan() {
160153
return currentActiveSpan.get();
161154
}
162-
155+
163156
/**
164157
* Set the newly created active span as the active one from the Tracer's perspective
165-
*
158+
*
166159
* @param activeSpan
167160
*/
168-
protected void makeActive(DDActiveSpan activeSpan){
161+
protected void makeActive(DDActiveSpan activeSpan) {
169162
//We cannot make active a preably deactivated span
170-
if(activeSpan!=null && activeSpan.isDeactivated())
163+
if (activeSpan != null && activeSpan.isDeactivated())
171164
currentActiveSpan.set(null);
172165
else
173166
currentActiveSpan.set(activeSpan);
174167
}
175-
168+
176169
/**
177170
* Deactivate the current span (if active) and make the parent active (again)
178-
*
171+
*
179172
* @param activeSpan
180173
*/
181-
protected void deactivate(DDActiveSpan activeSpan){
174+
protected void deactivate(DDActiveSpan activeSpan) {
182175
DDActiveSpan current = activeSpan();
183-
if(current==activeSpan){
176+
if (current == activeSpan) {
184177
//The parent becomes the active span
185178
makeActive(activeSpan.getParent());
186179
}
187180
}
188181

189182
@Override
190183
public DDActiveSpan makeActive(Span span) {
191-
if(!(span instanceof DDSpan))
192-
throw new IllegalArgumentException("Cannot transform a non DDSpan into a DDActiveSpan. Provided class: "+span.getClass());
193-
184+
if (!(span instanceof DDSpan))
185+
throw new IllegalArgumentException("Cannot transform a non DDSpan into a DDActiveSpan. Provided class: " + span.getClass());
186+
194187
//Wrap the provided manual span into an active one with the current parent
195-
DDActiveSpan activeSpan = new DDActiveSpan(activeSpan(),(DDSpan)span);
196-
188+
DDActiveSpan activeSpan = new DDActiveSpan(activeSpan(), (DDSpan) span);
189+
197190
makeActive(activeSpan);
198-
191+
199192
return activeSpan;
200193
}
201194

@@ -229,22 +222,22 @@ public SpanBuilder ignoreActiveSpan() {
229222
public DDActiveSpan startActive() {
230223
//Set the active span as parent if ignoreActiveSpan==true
231224
DDActiveSpan activeParent = null;
232-
if(!ignoreActiveSpan){
225+
if (!ignoreActiveSpan) {
233226
DDActiveSpan current = activeSpan();
234-
if(current!=null){
227+
if (current != null) {
235228
activeParent = current;
236-
229+
237230
//Ensure parent inheritance
238231
asChildOf(activeParent);
239232
}
240233
}
241-
234+
242235
//Create the active span
243-
DDActiveSpan activeSpan = new DDActiveSpan(activeParent,this.timestamp, buildSpanContext());
236+
DDActiveSpan activeSpan = new DDActiveSpan(activeParent, this.timestamp, buildSpanContext());
244237
logger.debug("{} - Starting a new active span.", activeSpan);
245-
238+
246239
makeActive(activeSpan);
247-
240+
248241
return activeSpan;
249242
}
250243

@@ -284,7 +277,7 @@ public DDTracer.DDSpanBuilder withTag(String tag, boolean bool) {
284277
return withTag(tag, (Object) bool);
285278
}
286279

287-
280+
288281
public DDSpanBuilder(String operationName) {
289282
this.operationName = operationName;
290283
}
@@ -380,25 +373,32 @@ private DDSpanContext buildSpanContext() {
380373
}
381374
}
382375

376+
383377
String operationName = this.operationName != null ? this.operationName : this.resourceName;
384378

385379
//this.operationName, this.tags,
386380

387381
// some attributes are inherited from the parent
388382
context = new DDSpanContext(
389383
this.parent == null ? generatedId : p.getTraceId(),
390-
generatedId,
391-
this.parent == null ? 0L : p.getSpanId(),
392-
serviceName,
393-
operationName,
394-
this.resourceName,
395-
this.parent == null ? null : p.getBaggageItems(),
396-
errorFlag,
397-
spanType,
398-
this.tags,
399-
this.parent == null ? null : p.getTrace(),
400-
DDTracer.this
401-
);
384+
generatedId,
385+
this.parent == null ? 0L : p.getSpanId(),
386+
serviceName,
387+
operationName,
388+
this.resourceName,
389+
this.parent == null ? null : p.getBaggageItems(),
390+
errorFlag,
391+
spanType,
392+
this.tags,
393+
this.parent == null ? null : p.getTrace(),
394+
DDTracer.this
395+
);
396+
397+
398+
// Force the lang meta
399+
if (context.getBaggageItem(DDSpanContext.LANGUAGE_FIELDNAME) == null) {
400+
context.setBaggageItem(DDSpanContext.LANGUAGE_FIELDNAME, "java");
401+
}
402402

403403
return context;
404404
}

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

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
import org.junit.Before;
55
import org.junit.Test;
66

7-
import com.datadoghq.trace.DDSpan;
8-
import com.datadoghq.trace.DDSpanContext;
9-
import com.datadoghq.trace.DDTracer;
10-
117
import java.util.ArrayList;
128
import java.util.HashMap;
139
import java.util.Map;
@@ -40,60 +36,75 @@ public void shouldBuildSimpleSpan() {
4036
}
4137

4238
@Test
43-
public void shouldBuildMoreComplexSpan() {
39+
public void shouldBuildMoreComplexSpan() {
4440

45-
final String expectedName = "fakeName";
46-
final Map tags = new HashMap<String, Object>() {
47-
{
48-
put("1", true);
49-
put("2", "fakeString");
50-
put("3", 42.0);
51-
}
52-
};
41+
final String expectedName = "fakeName";
42+
final Map tags = new HashMap<String, Object>() {
43+
{
44+
put("1", true);
45+
put("2", "fakeString");
46+
put("3", 42.0);
47+
}
48+
};
5349

54-
DDSpan span = tracer
55-
.buildSpan(expectedName)
56-
.withServiceName("foo")
57-
.withTag("1", (Boolean) tags.get("1"))
58-
.withTag("2", (String) tags.get("2"))
59-
.withTag("3", (Number) tags.get("3"))
60-
.start();
50+
DDSpan span = tracer
51+
.buildSpan(expectedName)
52+
.withServiceName("foo")
53+
.withTag("1", (Boolean) tags.get("1"))
54+
.withTag("2", (String) tags.get("2"))
55+
.withTag("3", (Number) tags.get("3"))
56+
.start();
6157

62-
assertThat(span.getOperationName()).isEqualTo(expectedName);
63-
assertThat(span.getTags()).containsAllEntriesOf(tags);
58+
assertThat(span.getOperationName()).isEqualTo(expectedName);
59+
assertThat(span.getTags()).containsAllEntriesOf(tags);
6460

65-
// with no tag provided
61+
// with no tag provided
6662

67-
span = tracer
68-
.buildSpan(expectedName)
69-
.withServiceName("foo")
70-
.start();
63+
span = tracer
64+
.buildSpan(expectedName)
65+
.withServiceName("foo")
66+
.start();
7167

72-
assertThat(span.getTags()).isNotNull();
73-
assertThat(span.getTags()).isEmpty();
68+
assertThat(span.getTags()).isNotNull();
69+
assertThat(span.getTags()).isEmpty();
7470

75-
// with all custom fields provided
76-
final String expectedResource = "fakeResource";
77-
final String expectedService = "fakeService";
78-
final String expectedType = "fakeType";
71+
// with all custom fields provided
72+
final String expectedResource = "fakeResource";
73+
final String expectedService = "fakeService";
74+
final String expectedType = "fakeType";
7975

80-
span = tracer
81-
.buildSpan(expectedName)
82-
.withServiceName("foo")
83-
.withResourceName(expectedResource)
84-
.withServiceName(expectedService)
85-
.withErrorFlag()
86-
.withSpanType(expectedType)
87-
.start();
76+
span = tracer
77+
.buildSpan(expectedName)
78+
.withServiceName("foo")
79+
.withResourceName(expectedResource)
80+
.withServiceName(expectedService)
81+
.withErrorFlag()
82+
.withSpanType(expectedType)
83+
.start();
8884

89-
DDSpanContext actualContext = span.context();
85+
DDSpanContext actualContext = span.context();
9086

91-
assertThat(actualContext.getResourceName()).isEqualTo(expectedResource);
92-
assertThat(actualContext.getErrorFlag()).isTrue();
93-
assertThat(actualContext.getServiceName()).isEqualTo(expectedService);
94-
assertThat(actualContext.getSpanType()).isEqualTo(expectedType);
87+
assertThat(actualContext.getResourceName()).isEqualTo(expectedResource);
88+
assertThat(actualContext.getErrorFlag()).isTrue();
89+
assertThat(actualContext.getServiceName()).isEqualTo(expectedService);
90+
assertThat(actualContext.getSpanType()).isEqualTo(expectedType);
9591

96-
}
92+
93+
}
94+
95+
@Test
96+
public void shouldAddLangMeta() {
97+
98+
final String expectedName = "fakeName";
99+
100+
DDSpan span = tracer
101+
.buildSpan(expectedName)
102+
.withServiceName("foo")
103+
.start();
104+
105+
assertThat(span.getBaggageItem(DDSpanContext.LANGUAGE_FIELDNAME)).isEqualTo("java");
106+
107+
}
97108

98109
@Test
99110
public void shouldBuildSpanTimestampInNano() {

0 commit comments

Comments
 (0)