Skip to content

Commit b2cd8a0

Browse files
committed
Provide optimized writers for OpenTelemetry's "trace.proto" wire protocol
1 parent ca32af7 commit b2cd8a0

File tree

7 files changed

+884
-5
lines changed

7 files changed

+884
-5
lines changed

dd-trace-core/src/main/java/datadog/trace/core/DDSpan.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ public TraceConfig traceConfig() {
884884
return context.getTraceCollector().getTraceConfig();
885885
}
886886

887-
List<? extends AgentSpanLink> getLinks() {
887+
public List<? extends AgentSpanLink> getLinks() {
888888
return this.links;
889889
}
890890

dd-trace-core/src/main/java/datadog/trace/core/otlp/common/OtlpCommonProto.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import datadog.communication.serialization.SimpleUtf8Cache;
1616
import datadog.communication.serialization.StreamingBuffer;
1717
import datadog.trace.api.Config;
18+
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
1819
import datadog.trace.bootstrap.otel.common.OtelInstrumentationScope;
1920
import java.nio.ByteBuffer;
2021
import java.util.List;
@@ -45,6 +46,10 @@ private OtlpCommonProto() {}
4546
? new GenerationalUtf8Cache(Config.get().getTagValueUtf8CacheSize())
4647
: null;
4748

49+
public static void recalibrateCaches() {
50+
VALUE_CACHE.recalibrate();
51+
}
52+
4853
public static int sizeVarInt(int value) {
4954
return 1 + (31 - Integer.numberOfLeadingZeros(value)) / 7;
5055
}
@@ -153,8 +158,13 @@ public static void writeInstrumentationScope(
153158
}
154159

155160
@SuppressWarnings("unchecked")
156-
public static void writeAttribute(StreamingBuffer buf, int type, String key, Object value) {
157-
byte[] keyUtf8 = keyUtf8(key);
161+
public static void writeAttribute(StreamingBuffer buf, int type, CharSequence key, Object value) {
162+
byte[] keyUtf8;
163+
if (key instanceof UTF8BytesString) {
164+
keyUtf8 = ((UTF8BytesString) key).getUtf8Bytes();
165+
} else {
166+
keyUtf8 = keyUtf8(key.toString());
167+
}
158168
switch (type) {
159169
case STRING:
160170
writeStringAttribute(buf, keyUtf8, valueUtf8((String) value));
@@ -185,6 +195,19 @@ public static void writeAttribute(StreamingBuffer buf, int type, String key, Obj
185195
}
186196
}
187197

198+
public static void writeAttribute(
199+
StreamingBuffer buf, UTF8BytesString key, UTF8BytesString value) {
200+
writeStringAttribute(buf, key.getUtf8Bytes(), value.getUtf8Bytes());
201+
}
202+
203+
public static void writeAttribute(StreamingBuffer buf, UTF8BytesString key, String value) {
204+
writeStringAttribute(buf, key.getUtf8Bytes(), valueUtf8(value));
205+
}
206+
207+
public static void writeAttribute(StreamingBuffer buf, UTF8BytesString key, long value) {
208+
writeLongAttribute(buf, key.getUtf8Bytes(), value);
209+
}
210+
188211
private static byte[] keyUtf8(String key) {
189212
return KEY_CACHE != null ? KEY_CACHE.getUtf8(key) : key.getBytes(UTF_8);
190213
}

dd-trace-core/src/main/java/datadog/trace/core/otlp/metrics/OtlpMetricsProtoCollector.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import datadog.trace.bootstrap.otlp.metrics.OtlpMetricVisitor;
2626
import datadog.trace.bootstrap.otlp.metrics.OtlpMetricsVisitor;
2727
import datadog.trace.bootstrap.otlp.metrics.OtlpScopedMetricsVisitor;
28+
import datadog.trace.core.otlp.common.OtlpCommonProto;
2829
import datadog.trace.core.otlp.common.OtlpPayload;
2930
import java.util.ArrayDeque;
3031
import java.util.ArrayList;
@@ -93,6 +94,7 @@ public OtlpPayload collectMetrics() {
9394
}
9495

9596
OtlpPayload collectMetrics(Consumer<OtlpMetricsVisitor> registry) {
97+
OtlpCommonProto.recalibrateCaches();
9698
start();
9799
try {
98100
registry.accept(this);
@@ -108,15 +110,15 @@ private void start() {
108110
startNanos = endNanos;
109111
endNanos = timeSource.getCurrentTimeNanos();
110112

111-
// clear payloadChunks in case it wasn't fully consumed via OtlpMetricsPayload
113+
// clear payloadChunks in case it wasn't fully consumed via OtlpPayload
112114
payloadChunks.clear();
113115
}
114116

115117
/** Cleanup elements used to collect metrics data. */
116118
private void stop() {
117119
buf.reset();
118120

119-
// leave payloadChunks in place so it can be consumed via OtlpMetricsPayload
121+
// leave payloadChunks in place so it can be consumed via OtlpPayload
120122
scopedChunks.clear();
121123
metricChunks.clear();
122124

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package datadog.trace.core.otlp.trace;
2+
3+
import datadog.trace.core.DDSpan;
4+
import datadog.trace.core.otlp.common.OtlpPayload;
5+
import java.util.List;
6+
7+
/** Collects trace spans ready for export. */
8+
public interface OtlpTraceCollector {
9+
OtlpTraceCollector NOOP_COLLECTOR = spans -> OtlpPayload.EMPTY;
10+
11+
OtlpPayload collectSpans(List<DDSpan> spans);
12+
}

0 commit comments

Comments
 (0)