Skip to content

Commit 2422667

Browse files
jbachorikclaude
andcommitted
Clear stale sidecar/record on ThreadContext init, fix contextattribute= no-op
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
1 parent de54e6c commit 2422667

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ public ThreadContext(ByteBuffer recordBuffer, ByteBuffer tlsPtrBuffer, ByteBuffe
100100
throw new UnsupportedOperationException(
101101
"ByteBuffer context path requires little-endian platform");
102102
}
103+
// Zero sidecar + record to prevent stale encodings from a previous profiler session.
104+
// The native ProfiledThread survives across sessions, so the sidecar may hold
105+
// old tag encodings and the record may hold old attrs_data.
106+
for (int i = 0; i < MAX_CUSTOM_SLOTS; i++) {
107+
this.sidecarBuffer.putInt(i * 4, 0);
108+
}
109+
this.sidecarBuffer.putLong(this.lrsSidecarOffset, 0);
110+
this.recordBuffer.put(this.validOffset, (byte) 0);
111+
this.recordBuffer.putShort(this.attrsDataSizeOffset, (short) 0);
112+
this.tlsPtrBuffer.putLong(0, 0);
103113
}
104114

105115
/**

ddprof-test/src/test/java/com/datadoghq/profiler/context/OtelContextStorageModeTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void cleanup() {
6363
public void testOtelStorageModeContext() throws Exception {
6464
Path jfrFile = Files.createTempFile("otel-ctx-otel", ".jfr");
6565

66-
profiler.execute(String.format("start,cpu=1ms,jfr,file=%s", jfrFile.toAbsolutePath()));
66+
profiler.execute(String.format("start,cpu=1ms,attributes=tag1;tag2;tag3,jfr,file=%s", jfrFile.toAbsolutePath()));
6767
profilerStarted = true;
6868

6969
long localRootSpanId = 0x1111222233334444L;
@@ -84,7 +84,7 @@ public void testOtelStorageModeContext() throws Exception {
8484
public void testOtelModeStartsOnAnyPlatform() throws Exception {
8585
Path jfrFile = Files.createTempFile("otel-ctx-any", ".jfr");
8686

87-
profiler.execute(String.format("start,cpu=1ms,jfr,file=%s", jfrFile.toAbsolutePath()));
87+
profiler.execute(String.format("start,cpu=1ms,attributes=tag1;tag2;tag3,jfr,file=%s", jfrFile.toAbsolutePath()));
8888
profilerStarted = true;
8989

9090
// Context operations should not crash
@@ -98,7 +98,7 @@ public void testOtelModeStartsOnAnyPlatform() throws Exception {
9898
public void testOtelModeClearContext() throws Exception {
9999
Path jfrFile = Files.createTempFile("otel-ctx-clear", ".jfr");
100100

101-
profiler.execute(String.format("start,cpu=1ms,jfr,file=%s", jfrFile.toAbsolutePath()));
101+
profiler.execute(String.format("start,cpu=1ms,attributes=tag1;tag2;tag3,jfr,file=%s", jfrFile.toAbsolutePath()));
102102
profilerStarted = true;
103103

104104
profiler.setContext(0xCAFEBABEL, 0xDEADBEEFL, 0L, 0x12345678L);
@@ -117,7 +117,7 @@ public void testOtelModeClearContext() throws Exception {
117117
public void testOtelModeCustomAttributes() throws Exception {
118118
Path jfrFile = Files.createTempFile("otel-ctx-attrs", ".jfr");
119119

120-
profiler.execute(String.format("start,cpu=1ms,contextattribute=http.route;db.system,jfr,file=%s", jfrFile.toAbsolutePath()));
120+
profiler.execute(String.format("start,cpu=1ms,attributes=http.route;db.system,jfr,file=%s", jfrFile.toAbsolutePath()));
121121
profilerStarted = true;
122122

123123
// Register attribute keys
@@ -146,7 +146,7 @@ public void testOtelModeCustomAttributes() throws Exception {
146146
public void testSetContextAttribute() throws Exception {
147147
Path jfrFile = Files.createTempFile("otel-ctx-attr", ".jfr");
148148

149-
profiler.execute(String.format("start,cpu=1ms,contextattribute=http.route,jfr,file=%s", jfrFile.toAbsolutePath()));
149+
profiler.execute(String.format("start,cpu=1ms,attributes=http.route,jfr,file=%s", jfrFile.toAbsolutePath()));
150150
profilerStarted = true;
151151

152152
profiler.setContext(0x456L, 0x123L, 0L, 0x789L);
@@ -163,7 +163,7 @@ public void testSetContextAttribute() throws Exception {
163163
public void testOtelModeAttributeOverflow() throws Exception {
164164
Path jfrFile = Files.createTempFile("otel-ctx-overflow", ".jfr");
165165

166-
profiler.execute(String.format("start,cpu=1ms,contextattribute=k0;k1;k2;k3;k4,jfr,file=%s", jfrFile.toAbsolutePath()));
166+
profiler.execute(String.format("start,cpu=1ms,attributes=k0;k1;k2;k3;k4,jfr,file=%s", jfrFile.toAbsolutePath()));
167167
profilerStarted = true;
168168

169169
OTelContext.getInstance().registerAttributeKeys("k0", "k1", "k2", "k3", "k4");

0 commit comments

Comments
 (0)