Skip to content

Commit cd59310

Browse files
Fixed first span metadata processing.
1 parent 7d72919 commit cd59310

2 files changed

Lines changed: 111 additions & 7 deletions

File tree

dd-trace-core/src/main/java/datadog/trace/common/writer/ddagent/TraceMapperV1.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,23 @@ public TraceMapperV1() {
8383
public void map(List<? extends CoreSpan<?>> trace, Writable writable) {
8484
CoreSpan<?> firstSpan = trace.get(0);
8585
firstSpan.processTagsAndBaggage(spanMetadata, false);
86-
Metadata meta = spanMetadata.metadata;
86+
Metadata firstSpanMeta = spanMetadata.metadata;
8787

8888
// encoded fields: 1..7, but skipping #5, as not required by tracers and set by the agent.
8989
writable.startMap(6);
9090

9191
// priority = 1, the sampling priority of the trace
92-
encodeField(writable, 1, meta.samplingPriority()); // TODO double check
92+
encodeField(writable, 1, firstSpanMeta.samplingPriority()); // TODO double check
9393
// origin = 2, the optional string origin ("lambda", "rum", etc.) of the trace chunk
9494
encodeField(writable, 2, firstSpan.getOrigin()); // TODO double check
9595
// attributes = 3, a collection of key to value pairs common in all `spans`
9696
encodeAttributes(writable, 3, buildChunkAttributes(trace), emptyMap());
9797
// spans = 4, a list of spans in this chunk
98-
encodeSpans(writable, 4, trace);
98+
encodeSpans(writable, 4, trace, firstSpanMeta);
9999
// traceID = 6, the ID of the trace to which all spans in this chunk belong
100100
encodeField(writable, 6, firstSpan.getTraceId().to128BitBytes());
101101
// samplingMechanism = 7
102-
encodeField(writable, 7, parseSamplingMechanism(meta.getTags()));
102+
encodeField(writable, 7, parseSamplingMechanism(firstSpanMeta.getTags()));
103103
}
104104

105105
private Map<String, Object> buildChunkAttributes(List<? extends CoreSpan<?>> trace) {
@@ -114,7 +114,11 @@ private Map<String, Object> buildChunkAttributes(List<? extends CoreSpan<?>> tra
114114
return singletonMap("service", service);
115115
}
116116

117-
private void encodeSpans(Writable writable, int fieldId, List<? extends CoreSpan<?>> spans) {
117+
private void encodeSpans(
118+
Writable writable,
119+
int fieldId,
120+
List<? extends CoreSpan<?>> spans,
121+
Metadata firstSpanMetadata) {
118122
int spansCount = spans.size();
119123

120124
writable.writeInt(fieldId);
@@ -123,8 +127,13 @@ private void encodeSpans(Writable writable, int fieldId, List<? extends CoreSpan
123127
for (int i = 0; i < spansCount; i++) {
124128
final CoreSpan<?> span = spans.get(i);
125129

126-
span.processTagsAndBaggage(spanMetadata, false);
127-
Metadata meta = spanMetadata.metadata;
130+
Metadata meta;
131+
if (i == 0) {
132+
meta = firstSpanMetadata;
133+
} else {
134+
span.processTagsAndBaggage(spanMetadata, false);
135+
meta = spanMetadata.metadata;
136+
}
128137
TagMap tags = meta.getTags();
129138
Map<String, Object> metaStruct = span.getMetaStruct();
130139

dd-trace-core/src/test/groovy/datadog/trace/common/writer/ddagent/TraceMapperV1PayloadTest.groovy

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import datadog.trace.bootstrap.instrumentation.api.SpanLink
2424
import datadog.trace.bootstrap.instrumentation.api.Tags
2525
import datadog.trace.common.writer.Payload
2626
import datadog.trace.common.writer.TraceGenerator
27+
import datadog.trace.core.MetadataConsumer
2728
import datadog.trace.test.util.DDSpecification
2829
import java.nio.ByteBuffer
2930
import java.nio.channels.WritableByteChannel
@@ -286,6 +287,54 @@ class TraceMapperV1PayloadTest extends DDSpecification {
286287
assertEquals([:], links[1].attributes)
287288
}
288289

290+
def "test first span tags are processed once"() {
291+
setup:
292+
def firstSpan = new CountingPojoSpan(
293+
"service-a",
294+
"operation-a",
295+
"resource-a",
296+
DDTraceId.ONE,
297+
123L,
298+
0L,
299+
1000L,
300+
2000L,
301+
0,
302+
[:],
303+
[(Tags.HTTP_URL): "http://localhost:7777/"],
304+
"web",
305+
false,
306+
PrioritySampling.SAMPLER_KEEP,
307+
200,
308+
null)
309+
310+
def secondSpan = new CountingPojoSpan(
311+
"service-a",
312+
"operation-b",
313+
"resource-b",
314+
DDTraceId.ONE,
315+
456L,
316+
123L,
317+
1000L,
318+
2000L,
319+
0,
320+
[:],
321+
[(Tags.HTTP_URL): "http://localhost:7777/"],
322+
"web",
323+
false,
324+
PrioritySampling.SAMPLER_KEEP,
325+
200,
326+
null)
327+
328+
TraceMapperV1 mapper = new TraceMapperV1()
329+
330+
when:
331+
serializeMappedPayload(mapper, [[firstSpan, secondSpan]])
332+
333+
then:
334+
assertEquals(1, firstSpan.processTagsAndBaggageCount)
335+
assertEquals(1, secondSpan.processTagsAndBaggageCount)
336+
}
337+
289338
def "test missing span links encode empty links"() {
290339
setup:
291340
def span = new TraceGenerator.PojoSpan(
@@ -1339,6 +1388,52 @@ class TraceMapperV1PayloadTest extends DDSpecification {
13391388
}
13401389
}
13411390

1391+
private static class CountingPojoSpan extends TraceGenerator.PojoSpan {
1392+
int processTagsAndBaggageCount = 0
1393+
1394+
CountingPojoSpan(
1395+
String serviceName,
1396+
String operationName,
1397+
CharSequence resourceName,
1398+
DDTraceId traceId,
1399+
long spanId,
1400+
long parentId,
1401+
long start,
1402+
long duration,
1403+
int error,
1404+
Map<String, String> baggage,
1405+
Map<String, Object> tags,
1406+
CharSequence type,
1407+
boolean measured,
1408+
int samplingPriority,
1409+
int statusCode,
1410+
CharSequence origin) {
1411+
super(
1412+
serviceName,
1413+
operationName,
1414+
resourceName,
1415+
traceId,
1416+
spanId,
1417+
parentId,
1418+
start,
1419+
duration,
1420+
error,
1421+
baggage,
1422+
tags,
1423+
type,
1424+
measured,
1425+
samplingPriority,
1426+
statusCode,
1427+
origin)
1428+
}
1429+
1430+
@Override
1431+
void processTagsAndBaggage(MetadataConsumer consumer) {
1432+
processTagsAndBaggageCount++
1433+
super.processTagsAndBaggage(consumer)
1434+
}
1435+
}
1436+
13421437
private static class ByteArrayChannel implements WritableByteChannel {
13431438
private byte[] data = new byte[0]
13441439

0 commit comments

Comments
 (0)