Skip to content

Commit 2109c63

Browse files
jbachorikclaude
andcommitted
Add ThreadContext.put(spanId, traceIdHigh, traceIdLow) for 128-bit trace IDs
Routes to ContextApi::setOtel() via new setContextOtel0 JNI method, enabling tracers to set full W3C 128-bit trace IDs. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
1 parent 2000f0b commit 2109c63

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

ddprof-lib/src/main/cpp/javaApi.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,11 @@ Java_com_datadoghq_profiler_ThreadContext_setContext0(JNIEnv* env, jclass unused
572572
return Contexts::checksum(spanId, rootSpanId);
573573
}
574574

575+
extern "C" DLLEXPORT void JNICALL
576+
Java_com_datadoghq_profiler_ThreadContext_setContextOtel0(JNIEnv* env, jclass unused, jlong traceIdHigh, jlong traceIdLow, jlong spanId) {
577+
ContextApi::setOtel(traceIdHigh, traceIdLow, spanId);
578+
}
579+
575580
// Legacy API: writes directly to Context.tags[] regardless of storage mode.
576581
// In OTEL mode, writeCurrentContext() in flightRecorder.cpp reads both Context.tags[]
577582
// and the OTEP attrs_data, so tags set via this path are still recorded in JFR.

ddprof-lib/src/main/java/com/datadoghq/profiler/ThreadContext.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,19 @@ public long put(long spanId, long rootSpanId) {
161161
return (useJNI || otelMode) ? setContext0(spanId, rootSpanId) : putContextJava(spanId, rootSpanId);
162162
}
163163

164+
/**
165+
* Sets trace context with full 128-bit W3C trace ID support.
166+
* In OTEL mode, all three values are written to the OTEP record.
167+
* In profiler mode, traceIdHigh is ignored (Datadog uses 64-bit trace IDs).
168+
*
169+
* @param spanId The span ID
170+
* @param traceIdHigh Upper 64 bits of the 128-bit trace ID
171+
* @param traceIdLow Lower 64 bits of the 128-bit trace ID (rootSpanId in Datadog)
172+
*/
173+
public void put(long spanId, long traceIdHigh, long traceIdLow) {
174+
setContextOtel0(traceIdHigh, traceIdLow, spanId);
175+
}
176+
164177
public void putCustom(int offset, int value) {
165178
if (offset >= MAX_CUSTOM_SLOTS) {
166179
throw new IllegalArgumentException("Invalid offset: " + offset + " (max " + MAX_CUSTOM_SLOTS + ")");
@@ -215,6 +228,7 @@ public boolean setContextAttribute(int keyIndex, String value) {
215228
}
216229

217230
private static native long setContext0(long spanId, long rootSpanId);
231+
private static native void setContextOtel0(long traceIdHigh, long traceIdLow, long spanId);
218232
private static native void setContextSlot0(int offset, int value);
219233
private static native boolean setContextAttribute0(int keyIndex, String value);
220234

0 commit comments

Comments
 (0)