Skip to content

Commit b8e3bd6

Browse files
Added missing thread metadata in V1 trace mapper. (#11227)
Added missing thread metadata in V1 trace mapper. Merge branch 'master' into alexeyk/v1-thread-attrs Removed not needed code. Co-authored-by: alexey.kuznetsov <alexey.kuznetsov@datadoghq.com>
1 parent b868f3c commit b8e3bd6

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,14 @@ private void encodeSpanAttributes(
374374
(tagCount
375375
+ baggage.size()
376376
+ metaStruct.size()
377+
+ 2
377378
+ (writeHttpStatus ? 1 : 0)
378379
+ (writeTopLevel ? 1 : 0))
379380
* 3);
380381

382+
writeAttribute(writable, DDTags.THREAD_ID, meta.getThreadId());
383+
writeAttribute(writable, DDTags.THREAD_NAME, meta.getThreadName());
384+
381385
for (Map.Entry<String, String> entry : baggage.entrySet()) {
382386
writeAttribute(writable, entry.getKey(), entry.getValue());
383387
}
@@ -470,16 +474,19 @@ private void encodeAttributes(Writable writable, int fieldId, Map<String, ?> att
470474

471475
private void writeAttribute(Writable writable, String key, Object value) {
472476
writeStreamingString(writable, key);
477+
473478
if (value instanceof Number) {
474479
writable.writeInt(VALUE_TYPE_FLOAT);
475480
writable.writeDouble(((Number) value).doubleValue());
476481
return;
477482
}
483+
478484
if (value instanceof Boolean) {
479485
writable.writeInt(VALUE_TYPE_BOOLEAN);
480486
writable.writeBoolean((Boolean) value);
481487
return;
482488
}
489+
483490
if (!(value instanceof String) && value != null) {
484491
log.debug("Not a string value for key: {}, value: {}", key, value);
485492
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,40 @@ class TraceMapperV1PayloadTest extends DDSpecification {
701701
assertEquals(4.25d, (attributes.get("tag.double") as Number).doubleValue(), 0.000001d)
702702
}
703703

704+
def "test thread metadata is encoded in v1 attributes"() {
705+
setup:
706+
def span = new TraceGenerator.PojoSpan(
707+
"service-a",
708+
"operation-a",
709+
"resource-a",
710+
DDTraceId.ONE,
711+
123L,
712+
0L,
713+
1000L,
714+
2000L,
715+
0,
716+
[:],
717+
[:],
718+
"web",
719+
false,
720+
PrioritySampling.SAMPLER_KEEP,
721+
0,
722+
null)
723+
724+
TraceMapperV1 mapper = new TraceMapperV1()
725+
byte[] encoded = serializeMappedPayload(mapper, [[span]])
726+
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(encoded)
727+
List<String> stringTable = new ArrayList<>()
728+
stringTable.add("")
729+
730+
when:
731+
Map<String, Object> attributes = readFirstSpanAttributes(unpacker, stringTable)
732+
733+
then:
734+
assertAttributeValueEquals(span.getTag(DDTags.THREAD_ID), attributes.get(DDTags.THREAD_ID), DDTags.THREAD_ID)
735+
assertEquals(span.getTag(DDTags.THREAD_NAME).toString(), attributes.get(DDTags.THREAD_NAME))
736+
}
737+
704738
private static final class PayloadVerifier implements ByteBufferConsumer, WritableByteChannel {
705739

706740
private final List<List<TraceGenerator.PojoSpan>> expectedTraces
@@ -973,6 +1007,8 @@ class TraceMapperV1PayloadTest extends DDSpecification {
9731007
for (Map.Entry<String, String> entry : expectedSpan.getBaggage().entrySet()) {
9741008
expectedAttributes.put(entry.getKey(), entry.getValue())
9751009
}
1010+
expectedAttributes.put(DDTags.THREAD_ID, expectedSpan.getTag(DDTags.THREAD_ID))
1011+
expectedAttributes.put(DDTags.THREAD_NAME, expectedSpan.getTag(DDTags.THREAD_NAME))
9761012
for (Map.Entry<String, Object> entry : expectedSpan.getTags().entrySet()) {
9771013
if (DDTags.SPAN_EVENTS == entry.getKey()) {
9781014
continue

0 commit comments

Comments
 (0)