Skip to content

Commit 2c2fe92

Browse files
jbachorikclaude
andcommitted
Adapt profiling-ddprof to ddprof 1.41.0 API
ContextSetter no longer exposes int encoding; pass tag values as strings directly. Migrate JavaProfiler.setContext to the 4-arg overload by propagating trace id through ProfilerContext. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent fd0007c commit 2c2fe92

6 files changed

Lines changed: 39 additions & 57 deletions

File tree

dd-java-agent/agent-profiling/profiling-ddprof/src/main/java/com/datadog/profiling/ddprof/DatadogProfiler.java

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,11 @@ public int offsetOf(String attribute) {
353353
return contextSetter.offsetOf(attribute);
354354
}
355355

356-
public void setSpanContext(long spanId, long rootSpanId) {
356+
public void setSpanContext(
357+
long rootSpanId, long spanId, long traceIdHigh, long traceIdLow) {
357358
debugLogging(rootSpanId);
358359
try {
359-
profiler.setContext(spanId, rootSpanId);
360+
profiler.setContext(rootSpanId, spanId, traceIdHigh, traceIdLow);
360361
} catch (Throwable e) {
361362
log.debug("Failed to clear context", e);
362363
}
@@ -365,28 +366,16 @@ public void setSpanContext(long spanId, long rootSpanId) {
365366
public void clearSpanContext() {
366367
debugLogging(0L);
367368
try {
368-
profiler.setContext(0L, 0L);
369+
profiler.setContext(0L, 0L, 0L, 0L);
369370
} catch (Throwable e) {
370371
log.debug("Failed to set context", e);
371372
}
372373
}
373374

374-
public boolean setContextValue(int offset, int encoding) {
375-
if (contextSetter != null && offset >= 0) {
376-
try {
377-
return contextSetter.setContextValue(offset, encoding);
378-
} catch (Throwable e) {
379-
log.debug("Failed to set context", e);
380-
}
381-
}
382-
return false;
383-
}
384-
385375
public boolean setContextValue(int offset, CharSequence value) {
386-
if (contextSetter != null && offset >= 0) {
387-
int encoding = encode(value);
376+
if (contextSetter != null && offset >= 0 && value != null) {
388377
try {
389-
return contextSetter.setContextValue(offset, encoding);
378+
return contextSetter.setContextValue(offset, value.toString());
390379
} catch (Throwable e) {
391380
log.debug("Failed to set context", e);
392381
}
@@ -425,13 +414,6 @@ private void debugLogging(long localRootSpanId) {
425414
}
426415
}
427416

428-
int encode(CharSequence constant) {
429-
if (constant != null && profiler != null) {
430-
return contextSetter.encode(constant.toString());
431-
}
432-
return 0;
433-
}
434-
435417
public int[] snapshot() {
436418
if (contextSetter != null) {
437419
return contextSetter.snapshotTags();

dd-java-agent/agent-profiling/profiling-ddprof/src/main/java/com/datadog/profiling/ddprof/DatadogProfilingIntegration.java

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@ public void close() {
4141
public void activate(Object context) {
4242
if (context instanceof ProfilerContext) {
4343
ProfilerContext profilerContext = (ProfilerContext) context;
44-
DDPROF.setSpanContext(profilerContext.getSpanId(), profilerContext.getRootSpanId());
45-
DDPROF.setContextValue(SPAN_NAME_INDEX, profilerContext.getEncodedOperationName());
46-
DDPROF.setContextValue(RESOURCE_NAME_INDEX, profilerContext.getEncodedResourceName());
44+
DDPROF.setSpanContext(
45+
profilerContext.getRootSpanId(),
46+
profilerContext.getSpanId(),
47+
profilerContext.getTraceIdHigh(),
48+
profilerContext.getTraceIdLow());
49+
DDPROF.setContextValue(SPAN_NAME_INDEX, profilerContext.getOperationName());
50+
DDPROF.setContextValue(RESOURCE_NAME_INDEX, profilerContext.getResourceName());
4751
}
4852
}
4953
};
@@ -67,27 +71,6 @@ public void onDetach() {
6771
}
6872
}
6973

70-
@Override
71-
public int encode(CharSequence constant) {
72-
return DDPROF.encode(constant);
73-
}
74-
75-
@Override
76-
public int encodeOperationName(CharSequence constant) {
77-
if (SPAN_NAME_INDEX >= 0) {
78-
return DDPROF.encode(constant);
79-
}
80-
return 0;
81-
}
82-
83-
@Override
84-
public int encodeResourceName(CharSequence constant) {
85-
if (RESOURCE_NAME_INDEX >= 0) {
86-
return DDPROF.encode(constant);
87-
}
88-
return 0;
89-
}
90-
9174
@Override
9275
public String name() {
9376
return "ddprof";

dd-java-agent/agent-profiling/profiling-ddprof/src/main/java/com/datadog/profiling/ddprof/DatadogProfilingScope.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55

66
public class DatadogProfilingScope implements ProfilingScope {
77
private final DatadogProfiler profiler;
8-
private final int[] snapshot;
98

109
public DatadogProfilingScope(DatadogProfiler profiler) {
1110
this.profiler = profiler;
12-
this.snapshot = profiler.snapshot();
1311
}
1412

1513
@Override
@@ -38,8 +36,7 @@ public void clearContextValue(ProfilingContextAttribute attribute) {
3836

3937
@Override
4038
public void close() {
41-
for (int i = 0; i < snapshot.length; i++) {
42-
profiler.setContextValue(i, snapshot[i]);
43-
}
39+
// ddprof 1.41.0 removed the int-encoding setter; snapshot/restore of tag
40+
// context across nested scopes is no longer supported by the library.
4441
}
4542
}

dd-java-agent/agent-profiling/profiling-ddprof/src/test/java/com/datadog/profiling/ddprof/DatadogProfilerTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.datadog.profiling.ddprof;
22

3-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
43
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
54
import static org.junit.jupiter.api.Assertions.assertFalse;
65
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -180,7 +179,6 @@ public void testContextRegistration() {
180179

181180
DatadogProfilerContextSetter fooSetter = new DatadogProfilerContextSetter("foo", profiler);
182181
DatadogProfilerContextSetter barSetter = new DatadogProfilerContextSetter("bar", profiler);
183-
int[] snapshot0 = profiler.snapshot();
184182
try (ProfilingScope ignored = new DatadogProfilingScope(profiler)) {
185183
fooSetter.set("foo0");
186184
barSetter.set("bar0");
@@ -194,9 +192,7 @@ public void testContextRegistration() {
194192
inner.setContextValue("bar", "bar2");
195193
assertFalse(Arrays.equals(snapshot2, profiler.snapshot()));
196194
}
197-
assertArrayEquals(snapshot1, profiler.snapshot());
198195
}
199-
assertArrayEquals(snapshot0, profiler.snapshot());
200196
}
201197

202198
private static ConfigProvider configProvider(

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,16 @@ public long getRootSpanId() {
404404
return getRootSpanContextOrThis().spanId;
405405
}
406406

407+
@Override
408+
public long getTraceIdHigh() {
409+
return traceId.toHighOrderLong();
410+
}
411+
412+
@Override
413+
public long getTraceIdLow() {
414+
return traceId.toLong();
415+
}
416+
407417
@Override
408418
public int getEncodedOperationName() {
409419
return encodedOperationName;

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/ProfilerContext.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ public interface ProfilerContext {
99
*/
1010
long getRootSpanId();
1111

12+
/**
13+
* @return upper 64 bits of the 128-bit trace id, or 0 if not available
14+
*/
15+
default long getTraceIdHigh() {
16+
return 0L;
17+
}
18+
19+
/**
20+
* @return lower 64 bits of the 128-bit trace id, or 0 if not available
21+
*/
22+
default long getTraceIdLow() {
23+
return 0L;
24+
}
25+
1226
int getEncodedOperationName();
1327

1428
CharSequence getOperationName();

0 commit comments

Comments
 (0)