@@ -356,29 +356,45 @@ describe("managed diagnostics-otel install runtime", () => {
356356 await runTurn ( gateway , "OTEL-MANAGED-SAMPLED-OUT" ) ;
357357 await sleep ( 1_500 ) ;
358358 expect ( configured . capturedRequests ) . toHaveLength ( 0 ) ;
359+ const sampledOutRequestCursor = configured . capturedRequests . length ;
360+ const sampledOutSpanCursor = configured . capturedSpans . length ;
359361 expect ( envOnly . capturedRequests ) . toHaveLength ( 0 ) ;
360362
361363 await restartWithOtelConfig ( {
362364 gateway,
363365 sampleRate : 1 ,
364366 traceEndpoint : configured . baseUrl ,
365367 } ) ;
368+ expect ( configured . capturedRequests ) . toHaveLength ( sampledOutRequestCursor ) ;
369+ expect ( configured . capturedSpans ) . toHaveLength ( sampledOutSpanCursor ) ;
370+ const sampledInRequestCursor = configured . capturedRequests . length ;
371+ const sampledInSpanCursor = configured . capturedSpans . length ;
366372 await runTurn ( gateway , "OTEL-MANAGED-INSTALL-OK" ) ;
367- const traceRequest = await waitFor (
368- ( ) =>
369- configured . capturedRequests . find (
370- ( request ) => request . path === "/v1/traces" && request . spanCount > 0 ,
371- ) ,
372- 15_000 ,
373- ) ;
373+ const sampledInExport = await waitFor ( ( ) => {
374+ let spanOffset = sampledInSpanCursor ;
375+ for ( const request of configured . capturedRequests . slice ( sampledInRequestCursor ) ) {
376+ const requestSpans = configured . capturedSpans . slice (
377+ spanOffset ,
378+ spanOffset + request . spanCount ,
379+ ) ;
380+ spanOffset += request . spanCount ;
381+ if (
382+ request . path === "/v1/traces" &&
383+ requestSpans . some ( ( span ) => span . name === "openclaw.run" )
384+ ) {
385+ return { request, spans : requestSpans } ;
386+ }
387+ }
388+ return undefined ;
389+ } , 15_000 ) ;
374390 // BatchSpanProcessor starts its timer on the first ended span. The first
375391 // export's earliest end timestamp is the boundary that must observe the clamp.
376- const firstRequestEndTimes = configured . capturedSpans
377- . slice ( 0 , traceRequest . spanCount )
378- . flatMap ( ( span ) => ( span . endTimeMs === undefined ? [ ] : [ span . endTimeMs ] ) ) ;
392+ const firstRequestEndTimes = sampledInExport . spans . flatMap ( ( span ) =>
393+ span . endTimeMs === undefined ? [ ] : [ span . endTimeMs ] ,
394+ ) ;
379395 expect ( firstRequestEndTimes . length ) . toBeGreaterThan ( 0 ) ;
380396 const firstSpanEndAt = Math . min ( ...firstRequestEndTimes ) ;
381- const exportDelayMs = ( traceRequest . receivedAtMs ?? 0 ) - firstSpanEndAt ;
397+ const exportDelayMs = ( sampledInExport . request . receivedAtMs ?? 0 ) - firstSpanEndAt ;
382398 expect ( exportDelayMs ) . toBeGreaterThanOrEqual ( 1_000 ) ;
383399 expect ( exportDelayMs ) . toBeLessThan ( 4_500 ) ;
384400 expect ( envOnly . capturedRequests ) . toHaveLength ( 0 ) ;
0 commit comments