@@ -96,18 +96,16 @@ public void testOtelStorageModeContext() throws Exception {
9696 profiler .execute (String .format ("start,cpu=1ms,ctxstorage=otel,jfr,file=%s" , jfrFile .toAbsolutePath ()));
9797 profilerStarted = true ;
9898
99- // Set context — writes to OTEP #4947 TLS record
99+ // Reset cached ThreadContext so it picks up the current OTEL mode
100+ profiler .resetThreadContext ();
101+
100102 long spanId = 0xAAAABBBBCCCCDDDDL ;
101103 long rootSpanId = 0x1111222233334444L ;
102104 profiler .setContext (spanId , rootSpanId );
103105
104- // Verify context round-trips through JNI, bypassing cached ThreadContext
105- // (ThreadContext caches otelMode at construction time; if a profiler-mode test
106- // ran first on this thread, the cached flag would be wrong)
107- long [] ctx = ThreadContext .readOtelContext ();
108- assertNotNull (ctx , "Context should be readable in OTEL mode" );
109- assertEquals (spanId , ctx [0 ], "SpanId should match in OTEL mode" );
110- assertEquals (rootSpanId , ctx [1 ], "RootSpanId should match in OTEL mode" );
106+ ThreadContext ctx = profiler .getThreadContext ();
107+ assertEquals (spanId , ctx .getSpanId (), "SpanId should match in OTEL mode" );
108+ assertEquals (rootSpanId , ctx .getRootSpanId (), "RootSpanId should match in OTEL mode" );
111109 }
112110
113111 /**
@@ -120,6 +118,8 @@ public void testOtelModeStartsOnAnyPlatform() throws Exception {
120118 profiler .execute (String .format ("start,cpu=1ms,ctxstorage=otel,jfr,file=%s" , jfrFile .toAbsolutePath ()));
121119 profilerStarted = true ;
122120
121+ profiler .resetThreadContext ();
122+
123123 // Context operations should not crash
124124 profiler .setContext (0x123L , 0x456L );
125125 }
@@ -134,19 +134,14 @@ public void testOtelModeClearContext() throws Exception {
134134 profiler .execute (String .format ("start,cpu=1ms,ctxstorage=otel,jfr,file=%s" , jfrFile .toAbsolutePath ()));
135135 profilerStarted = true ;
136136
137- // Set context
138- profiler .setContext (0xDEADBEEFL , 0xCAFEBABEL );
137+ profiler .resetThreadContext ();
139138
140- // Clear context
139+ profiler . setContext ( 0xDEADBEEFL , 0xCAFEBABEL );
141140 profiler .setContext (0 , 0 );
142141
143- // Verify context is cleared (use direct JNI read, not cached ThreadContext)
144- long [] ctx = ThreadContext .readOtelContext ();
145- // After clear, readContextDirect returns zeros (OTEL record is invalidated)
146- long clearedSpanId = (ctx != null ) ? ctx [0 ] : 0 ;
147- long clearedRootSpanId = (ctx != null ) ? ctx [1 ] : 0 ;
148- assertEquals (0 , clearedSpanId , "SpanId should be zero after clear" );
149- assertEquals (0 , clearedRootSpanId , "RootSpanId should be zero after clear" );
142+ ThreadContext ctx = profiler .getThreadContext ();
143+ assertEquals (0 , ctx .getSpanId (), "SpanId should be zero after clear" );
144+ assertEquals (0 , ctx .getRootSpanId (), "RootSpanId should be zero after clear" );
150145 }
151146
152147 /**
@@ -164,24 +159,22 @@ public void testOtelModeCustomAttributes() throws Exception {
164159 // Register attribute keys
165160 OTelContext .getInstance ().registerAttributeKeys ("http.route" , "db.system" );
166161
167- // Set context first (trace/span)
162+ profiler .resetThreadContext ();
163+
168164 long spanId = 0xAAAABBBBCCCCDDDDL ;
169165 long rootSpanId = 0x1111222233334444L ;
170166 profiler .setContext (spanId , rootSpanId );
171167
172- // Set custom attribute via new API
173168 ThreadContext ctx = profiler .getThreadContext ();
174169 boolean result = ctx .setContextAttribute (0 , "GET /api/users" );
175170 assertTrue (result , "setContextAttribute should succeed" );
176171
177172 result = ctx .setContextAttribute (1 , "postgresql" );
178173 assertTrue (result , "setContextAttribute for second key should succeed" );
179174
180- // Verify trace context is still intact (use direct JNI read)
181- long [] context = ThreadContext .readOtelContext ();
182- assertNotNull (context , "Context should be readable after setAttribute" );
183- assertEquals (spanId , context [0 ], "SpanId should match after setAttribute" );
184- assertEquals (rootSpanId , context [1 ], "RootSpanId should match after setAttribute" );
175+ // Verify trace context is still intact
176+ assertEquals (spanId , ctx .getSpanId (), "SpanId should match after setAttribute" );
177+ assertEquals (rootSpanId , ctx .getRootSpanId (), "RootSpanId should match after setAttribute" );
185178 }
186179
187180 /**
@@ -214,6 +207,8 @@ public void testOtelModeAttributeOverflow() throws Exception {
214207
215208 OTelContext .getInstance ().registerAttributeKeys ("k0" , "k1" , "k2" , "k3" , "k4" );
216209
210+ profiler .resetThreadContext ();
211+
217212 profiler .setContext (0x1L , 0x2L );
218213
219214 ThreadContext ctx = profiler .getThreadContext ();
0 commit comments