Skip to content

Commit 37be0e8

Browse files
Added chunk attributes.
1 parent fb856c0 commit 37be0e8

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,7 @@ public void map(List<? extends CoreSpan<?>> trace, Writable writable) {
9191
// origin = 2, the optional string origin ("lambda", "rum", etc.) of the trace chunk
9292
encodeField(writable, 2, firstSpan.getOrigin()); // TODO double check
9393
// attributes = 3, a collection of key to value pairs common in all `spans`
94-
encodeAttributes(
95-
writable,
96-
3,
97-
Collections.emptyMap(),
98-
Collections.emptyMap()); // TODO double check if something useful can be added
94+
encodeAttributes(writable, 3, buildChunkAttributes(trace), Collections.emptyMap());
9995
// spans = 4, a list of spans in this chunk
10096
encodeSpans(writable, 4, trace);
10197
// traceID = 6, the ID of the trace to which all spans in this chunk belong
@@ -104,6 +100,18 @@ public void map(List<? extends CoreSpan<?>> trace, Writable writable) {
104100
encodeField(writable, 7, parseSamplingMechanism(meta.getTags()));
105101
}
106102

103+
private Map<String, Object> buildChunkAttributes(List<? extends CoreSpan<?>> trace) {
104+
if (trace.isEmpty()) {
105+
return Collections.emptyMap();
106+
}
107+
CoreSpan<?> localRoot = trace.get(0).getLocalRootSpan();
108+
CharSequence service = localRoot == null ? null : localRoot.getServiceName();
109+
if (service == null) {
110+
return Collections.emptyMap();
111+
}
112+
return Collections.singletonMap("service", service);
113+
}
114+
107115
private void encodeSpans(Writable writable, int fieldId, List<? extends CoreSpan<?>> spans) {
108116
int spansCount = spans.size();
109117

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ class TraceMapperV1PayloadTest extends DDSpecification {
556556

557557
Integer priority = null
558558
String origin = null
559-
int chunkAttributesCount = -1
559+
Map<String, Object> chunkAttributes = null
560560
byte[] traceId = null
561561
Integer samplingMechanism = null
562562
List<TraceGenerator.PojoSpan> decodedSpans = null
@@ -571,7 +571,7 @@ class TraceMapperV1PayloadTest extends DDSpecification {
571571
origin = readStreamingString(unpacker, stringTable)
572572
break
573573
case 3:
574-
chunkAttributesCount = unpacker.unpackArrayHeader()
574+
chunkAttributes = readAttributes(unpacker, stringTable)
575575
break
576576
case 4:
577577
decodedSpans = verifySpans(unpacker, expectedTrace, stringTable)
@@ -591,14 +591,16 @@ class TraceMapperV1PayloadTest extends DDSpecification {
591591

592592
assertNotNull(priority)
593593
assertNotNull(origin)
594+
assertNotNull(chunkAttributes)
594595
assertNotNull(decodedSpans)
595596
assertNotNull(traceId)
596597
assertNotNull(samplingMechanism)
597-
assertEquals(0, chunkAttributesCount)
598598

599599
TraceGenerator.PojoSpan firstSpan = expectedTrace.get(0)
600600
assertEquals(firstSpan.samplingPriority(), priority)
601601
assertEqualsWithNullAsEmpty(firstSpan.getOrigin(), origin)
602+
assertEquals(1, chunkAttributes.size())
603+
assertEqualsWithNullAsEmpty(firstSpan.getLocalRootSpan().getServiceName(), chunkAttributes.get("service"))
602604
assertArrayEquals(firstSpan.getTraceId().to128BitBytes(), traceId)
603605
assertEquals(expectedSamplingMechanism(firstSpan.getTags()), samplingMechanism)
604606
}
@@ -843,19 +845,25 @@ class TraceMapperV1PayloadTest extends DDSpecification {
843845
readStreamingString(unpacker, stringTable)
844846
break
845847
case 3:
846-
unpacker.unpackArrayHeader()
848+
readAttributes(unpacker, stringTable)
847849
break
848850
case 4:
849851
int spanCount = unpacker.unpackArrayHeader()
850852
for (int i = 0; i < spanCount; i++) {
851853
skipSpan(unpacker, stringTable)
852854
}
853855
break
856+
case 5:
857+
unpacker.unpackBoolean()
858+
break
854859
case 6:
855860
int len = unpacker.unpackBinaryHeader()
856861
byte[] ignored = new byte[len]
857862
unpacker.readPayload(ignored)
858863
break
864+
case 7:
865+
unpacker.unpackInt()
866+
break
859867
default:
860868
Assertions.fail("Unexpected chunk field id while skipping: " + fieldId)
861869
}

0 commit comments

Comments
 (0)