Skip to content

Commit fa321a1

Browse files
Fixed v1 encoding int/uint32 and long/ulong.
1 parent 07eebed commit fa321a1

File tree

1 file changed

+49
-39
lines changed

1 file changed

+49
-39
lines changed

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

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,18 @@ public void map(List<? extends CoreSpan<?>> trace, Writable writable) {
8888
// encoded fields: 1..7, but skipping #5, as not required by tracers and set by the agent.
8989
writable.startMap(6);
9090

91-
// priority = 1, the sampling priority of the trace
92-
encodeField(writable, 1, firstSpanMeta.samplingPriority());
91+
// priority = 1, the sampling priority of the trace, int32
92+
encodeInt(writable, 1, firstSpanMeta.samplingPriority());
9393
// origin = 2, the optional string origin ("lambda", "rum", etc.) of the trace chunk
94-
encodeField(writable, 2, firstSpan.getOrigin());
94+
encodeString(writable, 2, firstSpan.getOrigin());
9595
// attributes = 3, a collection of key to value pairs common in all `spans`
9696
encodeAttributes(writable, 3, buildChunkAttributes(trace));
9797
// spans = 4, a list of spans in this chunk
9898
encodeSpans(writable, 4, trace, firstSpanMeta);
9999
// traceID = 6, the ID of the trace to which all spans in this chunk belong
100-
encodeField(writable, 6, firstSpan.getTraceId().to128BitBytes());
101-
// samplingMechanism = 7
102-
encodeField(writable, 7, parseSamplingMechanism(firstSpanMeta.getTags()));
100+
encodeBytes(writable, 6, firstSpan.getTraceId().to128BitBytes());
101+
// samplingMechanism = 7, uint32
102+
encodeInt(writable, 7, parseSamplingMechanism(firstSpanMeta.getTags()));
103103
}
104104

105105
private Map<String, Object> buildChunkAttributes(List<? extends CoreSpan<?>> trace) {
@@ -141,39 +141,39 @@ private void encodeSpans(
141141
writable.startMap(16);
142142

143143
// service = 1, the string name of the service that this span is associated with
144-
encodeField(writable, 1, span.getServiceName());
144+
encodeString(writable, 1, span.getServiceName());
145145
// name = 2, the string operation name of this span
146-
encodeField(writable, 2, span.getOperationName());
146+
encodeString(writable, 2, span.getOperationName());
147147
// resource = 3, the string resource name of this span,
148148
// sometimes called endpoint for web spans
149-
encodeField(writable, 3, span.getResourceName());
149+
encodeString(writable, 3, span.getResourceName());
150150
// spanID = 4, the ID of this span
151-
encodeUnsignedField(writable, 4, span.getSpanId());
151+
encodeLongUnsigned(writable, 4, span.getSpanId());
152152
// parentID = 5, the ID of this span's parent, or zero if there is no parent
153-
encodeUnsignedField(writable, 5, span.getParentId());
153+
encodeLongUnsigned(writable, 5, span.getParentId());
154154
// start = 6, the number of nanoseconds from the Unix epoch to the start of this span
155-
encodeField(writable, 6, span.getStartTime());
155+
encodeLong(writable, 6, span.getStartTime());
156156
// duration = 7, the time length of this span in nanoseconds
157-
encodeField(writable, 7, PendingTrace.getDurationNano(span));
157+
encodeLong(writable, 7, PendingTrace.getDurationNano(span));
158158
// error = 8, if there is an error associated with this span
159-
encodeField(writable, 8, span.getError() != 0);
159+
encodeBoolean(writable, 8, span.getError() != 0);
160160
// attributes = 9, a collection of string key to value pairs on the span
161161
encodeSpanAttributes(writable, 9, meta, metaStruct);
162162
// type = 10, the string type of the service with which this span is associated
163163
// (example values: web, db, lambda)
164-
encodeField(writable, 10, span.getType());
164+
encodeString(writable, 10, span.getType());
165165
// links = 11, a collection of links to other spans
166166
encodeSpanLinks(writable, 11, meta.getSpanLinks());
167167
// events = 12, a collection of events that occurred during this span
168168
encodeSpanEvents(writable, 12, tags.getObject(DDTags.SPAN_EVENTS));
169169
// env = 13, the optional string environment of this span
170-
encodeField(writable, 13, tags.getString(Tags.ENV));
170+
encodeString(writable, 13, tags.getString(Tags.ENV));
171171
// version = 14, the optional string version of this span
172-
encodeField(writable, 14, tags.getString(Tags.VERSION));
172+
encodeString(writable, 14, tags.getString(Tags.VERSION));
173173
// component = 15, the string component name of this span
174-
encodeField(writable, 15, tags.getString(Tags.COMPONENT));
175-
// kind = 16, the SpanKind of this span as defined in the OTEL Specification
176-
encodeField(writable, 16, getSpanKindValue(tags.getString(Tags.SPAN_KIND)));
174+
encodeString(writable, 15, tags.getString(Tags.COMPONENT));
175+
// kind = 16, the SpanKind of this span as defined in the OTEL Specification, uint32
176+
encodeInt(writable, 16, getSpanKindValue(tags.getString(Tags.SPAN_KIND)));
177177
}
178178
}
179179

@@ -187,11 +187,16 @@ private void encodeSpanLinks(Writable writable, int fieldId, List<AgentSpanLink>
187187
writable.startArray(links.size());
188188
for (AgentSpanLink link : links) {
189189
writable.startMap(5);
190-
encodeField(writable, 1, link.traceId().to128BitBytes());
191-
encodeUnsignedField(writable, 2, link.spanId());
190+
// 1: the ID of the trace that this link targets
191+
encodeBytes(writable, 1, link.traceId().to128BitBytes());
192+
// 2: the ID of the span that this link targets, fixed64
193+
encodeLongUnsigned(writable, 2, link.spanId());
194+
// 3: a collection of attribute string key to value pairs on the link, map<uint32, AnyValue>
192195
encodeAttributes(writable, 3, link.attributes().asMap());
193-
encodeField(writable, 4, link.traceState());
194-
encodeField(writable, 5, link.traceFlags() & 0xFF);
196+
// 4: optional W3C trace state string
197+
encodeString(writable, 4, link.traceState());
198+
// 5: optional W3C trace flags (if set, the high bit, bit 31, must be set), uint32
199+
encodeInt(writable, 5, link.traceFlags() & 0xFF);
195200
}
196201
}
197202

@@ -225,8 +230,8 @@ private void encodeSpanEvents(Writable writable, int fieldId, Object eventsObjec
225230
eventMap.get("attributes") instanceof Map ? (Map<?, ?>) eventMap.get("attributes") : null;
226231

227232
writable.startMap(3);
228-
encodeField(writable, 1, timeUnixNano);
229-
encodeField(writable, 2, String.valueOf(nameObject));
233+
encodeLong(writable, 1, timeUnixNano);
234+
encodeString(writable, 2, String.valueOf(nameObject));
230235
encodeEventAttributes(writable, 3, attributes);
231236
}
232237
}
@@ -469,27 +474,32 @@ private byte[] serializeMetaStructValue(Object value) {
469474
}
470475
}
471476

472-
private void encodeField(Writable writable, int fieldId, boolean value) {
477+
private void encodeBoolean(Writable writable, int fieldId, boolean value) {
473478
writable.writeInt(fieldId);
474479
writable.writeBoolean(value);
475480
}
476481

477-
private void encodeField(Writable writable, int fieldId, long value) {
482+
private void encodeInt(Writable writable, int fieldId, int value) {
483+
writable.writeInt(fieldId);
484+
writable.writeInt(value);
485+
}
486+
487+
private void encodeLong(Writable writable, int fieldId, long value) {
478488
writable.writeInt(fieldId);
479489
writable.writeLong(value);
480490
}
481491

482-
private void encodeUnsignedField(Writable writable, int fieldId, long value) {
492+
private void encodeLongUnsigned(Writable writable, int fieldId, long value) {
483493
writable.writeInt(fieldId);
484494
writable.writeUnsignedLong(value);
485495
}
486496

487-
private void encodeField(Writable writable, int fieldId, CharSequence value) {
497+
private void encodeString(Writable writable, int fieldId, CharSequence value) {
488498
writable.writeInt(fieldId);
489499
writeStreamingString(writable, value);
490500
}
491501

492-
private void encodeField(Writable writable, int fieldId, byte[] value) {
502+
private void encodeBytes(Writable writable, int fieldId, byte[] value) {
493503
writable.writeInt(fieldId);
494504
writable.writeBinary(value);
495505
}
@@ -574,28 +584,28 @@ private ByteBuffer buildHeader() {
574584
Config cfg = Config.get();
575585

576586
// containerID = 2, the string ID of the container where the tracer is running
577-
encodeField(headerWriter, 2, ContainerInfo.get().getContainerId());
587+
encodeString(headerWriter, 2, ContainerInfo.get().getContainerId());
578588

579589
// languageName = 3, the string language name of the tracer
580-
encodeField(headerWriter, 3, "java");
590+
encodeString(headerWriter, 3, "java");
581591

582592
// languageVersion = 4, the string language version of the tracer
583-
encodeField(headerWriter, 4, JavaVirtualMachine.getLangVersion());
593+
encodeString(headerWriter, 4, JavaVirtualMachine.getLangVersion());
584594

585595
// tracerVersion = 5, the string version of the tracer
586-
encodeField(headerWriter, 5, TracerVersion.TRACER_VERSION);
596+
encodeString(headerWriter, 5, TracerVersion.TRACER_VERSION);
587597

588598
// runtimeID = 6, the V4 string UUID representation of a tracer session
589-
encodeField(headerWriter, 6, cfg.getRuntimeId());
599+
encodeString(headerWriter, 6, cfg.getRuntimeId());
590600

591601
// env=7, the optional `env` string tag that set with the tracer
592-
encodeField(headerWriter, 7, cfg.getEnv());
602+
encodeString(headerWriter, 7, cfg.getEnv());
593603

594604
// hostname = 8, the optional string hostname of where the tracer is running
595-
encodeField(headerWriter, 8, cfg.getHostName());
605+
encodeString(headerWriter, 8, cfg.getHostName());
596606

597607
// appVersion = 9, the optional string `version` tag for the application set in the tracer
598-
encodeField(headerWriter, 9, cfg.getVersion());
608+
encodeString(headerWriter, 9, cfg.getVersion());
599609

600610
// attributes = 10, a collection of key to value pairs common in all `chunks`
601611
CharSequence processTags = ProcessTags.getTagsForSerialization();

0 commit comments

Comments
 (0)