Skip to content

Commit 7d72919

Browse files
Added process tags.
1 parent 37be0e8 commit 7d72919

2 files changed

Lines changed: 33 additions & 14 deletions

File tree

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package datadog.trace.common.writer.ddagent;
22

33
import static datadog.communication.http.OkHttpUtils.msgpackRequestBodyOf;
4+
import static java.util.Collections.emptyMap;
5+
import static java.util.Collections.singletonMap;
46

57
import datadog.common.container.ContainerInfo;
68
import datadog.communication.ddagent.TracerVersion;
@@ -11,6 +13,7 @@
1113
import datadog.environment.JavaVirtualMachine;
1214
import datadog.trace.api.Config;
1315
import datadog.trace.api.DDTags;
16+
import datadog.trace.api.ProcessTags;
1417
import datadog.trace.api.TagMap;
1518
import datadog.trace.api.sampling.SamplingMechanism;
1619
import datadog.trace.bootstrap.instrumentation.api.AgentSpanLink;
@@ -24,7 +27,6 @@
2427
import java.nio.ByteBuffer;
2528
import java.nio.channels.WritableByteChannel;
2629
import java.util.Arrays;
27-
import java.util.Collections;
2830
import java.util.HashMap;
2931
import java.util.LinkedHashMap;
3032
import java.util.List;
@@ -91,7 +93,7 @@ public void map(List<? extends CoreSpan<?>> trace, Writable writable) {
9193
// origin = 2, the optional string origin ("lambda", "rum", etc.) of the trace chunk
9294
encodeField(writable, 2, firstSpan.getOrigin()); // TODO double check
9395
// attributes = 3, a collection of key to value pairs common in all `spans`
94-
encodeAttributes(writable, 3, buildChunkAttributes(trace), Collections.emptyMap());
96+
encodeAttributes(writable, 3, buildChunkAttributes(trace), emptyMap());
9597
// spans = 4, a list of spans in this chunk
9698
encodeSpans(writable, 4, trace);
9799
// traceID = 6, the ID of the trace to which all spans in this chunk belong
@@ -102,14 +104,14 @@ public void map(List<? extends CoreSpan<?>> trace, Writable writable) {
102104

103105
private Map<String, Object> buildChunkAttributes(List<? extends CoreSpan<?>> trace) {
104106
if (trace.isEmpty()) {
105-
return Collections.emptyMap();
107+
return emptyMap();
106108
}
107109
CoreSpan<?> localRoot = trace.get(0).getLocalRootSpan();
108110
CharSequence service = localRoot == null ? null : localRoot.getServiceName();
109111
if (service == null) {
110-
return Collections.emptyMap();
112+
return emptyMap();
111113
}
112-
return Collections.singletonMap("service", service);
114+
return singletonMap("service", service);
113115
}
114116

115117
private void encodeSpans(Writable writable, int fieldId, List<? extends CoreSpan<?>> spans) {
@@ -180,7 +182,7 @@ private void encodeSpanLinks(Writable writable, int fieldId, List<AgentSpanLink>
180182
encodeField(writable, 2, link.spanId());
181183
Map<String, Object> attributes = new LinkedHashMap<>();
182184
attributes.putAll(link.attributes().asMap());
183-
encodeAttributes(writable, 3, attributes, Collections.emptyMap());
185+
encodeAttributes(writable, 3, attributes, emptyMap());
184186
encodeField(writable, 4, link.traceState());
185187
encodeField(writable, 5, link.traceFlags() & 0xFF);
186188
}
@@ -522,26 +524,34 @@ private ByteBuffer buildHeader() {
522524

523525
// containerID = 2, the string ID of the container where the tracer is running
524526
encodeField(headerWriter, 2, ContainerInfo.get().getContainerId());
527+
525528
// languageName = 3, the string language name of the tracer
526529
encodeField(headerWriter, 3, "java"); // TODO: check java or jvm?
530+
527531
// languageVersion = 4, the string language version of the tracer
528532
encodeField(headerWriter, 4, JavaVirtualMachine.getLangVersion());
533+
529534
// tracerVersion = 5, the string version of the tracer
530535
encodeField(headerWriter, 5, TracerVersion.TRACER_VERSION);
536+
531537
// runtimeID = 6, the V4 string UUID representation of a tracer session
532538
encodeField(headerWriter, 6, cfg.getRuntimeId());
539+
533540
// env=7, the optional `env` string tag that set with the tracer
534541
encodeField(headerWriter, 7, cfg.getEnv());
542+
535543
// hostname = 8, the optional string hostname of where the tracer is running
536544
encodeField(headerWriter, 8, cfg.getHostName());
545+
537546
// appVersion = 9, the optional string `version` tag for the application set in the tracer
538547
encodeField(headerWriter, 9, cfg.getVersion());
548+
539549
// attributes = 10, a collection of key to value pairs common in all `chunks`
540-
encodeAttributes(
541-
headerWriter,
542-
10,
543-
Collections.emptyMap(),
544-
Collections.emptyMap()); // TODO check useful attrs.
550+
CharSequence processTags = ProcessTags.getTagsForSerialization();
551+
Map<String, Object> tags =
552+
processTags != null ? singletonMap(DDTags.PROCESS_TAGS, processTags) : emptyMap();
553+
encodeAttributes(headerWriter, 10, tags, emptyMap());
554+
545555
// chunks = 11, a list of trace `chunks`, value is written by PayloadV1
546556
headerWriter.writeInt(11);
547557

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import static org.msgpack.core.MessageFormat.STR8
1313
import datadog.communication.serialization.ByteBufferConsumer
1414
import datadog.communication.serialization.FlushingBuffer
1515
import datadog.communication.serialization.msgpack.MsgPackWriter
16+
import datadog.trace.api.DDTags
1617
import datadog.trace.api.DDTraceId
1718
import datadog.trace.api.DDSpanId
19+
import datadog.trace.api.ProcessTags
1820
import datadog.trace.api.sampling.PrioritySampling
1921
import datadog.trace.api.sampling.SamplingMechanism
2022
import datadog.trace.bootstrap.instrumentation.api.SpanAttributes
@@ -123,6 +125,7 @@ class TraceMapperV1PayloadTest extends DDSpecification {
123125
int payloadFieldCount = unpacker.unpackMapHeader()
124126
Set<Integer> payloadFieldsSeen = new HashSet<>()
125127
int chunkCount = -1
128+
Map<String, Object> payloadAttributes = null
126129

127130
for (int i = 0; i < payloadFieldCount; i++) {
128131
int fieldId = unpacker.unpackInt()
@@ -139,7 +142,7 @@ class TraceMapperV1PayloadTest extends DDSpecification {
139142
readStreamingString(unpacker, stringTable)
140143
break
141144
case 10:
142-
assertEquals(0, unpacker.unpackArrayHeader())
145+
payloadAttributes = readAttributes(unpacker, stringTable)
143146
break
144147
case 11:
145148
chunkCount = unpacker.unpackArrayHeader()
@@ -155,6 +158,13 @@ class TraceMapperV1PayloadTest extends DDSpecification {
155158
assertEquals(10, payloadFieldCount)
156159
assertEquals((2..11).toSet(), payloadFieldsSeen)
157160
assertEquals(1, chunkCount)
161+
assertNotNull(payloadAttributes)
162+
if (ProcessTags.tagsForSerialization == null) {
163+
assertEquals(0, payloadAttributes.size())
164+
} else {
165+
assertEquals(1, payloadAttributes.size())
166+
assertEquals(ProcessTags.tagsForSerialization.toString(), payloadAttributes.get(DDTags.PROCESS_TAGS))
167+
}
158168
}
159169

160170
def "test sampling mechanism normalization from _dd.p.dm"() {
@@ -828,8 +838,7 @@ class TraceMapperV1PayloadTest extends DDSpecification {
828838
readStreamingString(unpacker, stringTable)
829839
break
830840
case 10:
831-
int attrArray = unpacker.unpackArrayHeader()
832-
assertEquals(0, attrArray)
841+
readAttributes(unpacker, stringTable)
833842
break
834843
default:
835844
Assertions.fail("Unexpected payload field id while skipping: " + fieldId)

0 commit comments

Comments
 (0)