@@ -15,6 +15,11 @@ interface Rum {
1515 getInternalContext ?: ( ) => RumInternalContext | undefined
1616}
1717
18+ interface TraceCorrelationContext extends Context {
19+ trace_id : string
20+ span_id : string
21+ }
22+
1823// Cache hostname at module initialization since it won't change during the app lifetime
1924const globalObj = getGlobalObject < BrowserWindow & { DD_RUM ?: Rum } > ( ) // eslint-disable-line local-rules/disallow-side-effects
2025const hostname = 'location' in globalObj ? globalObj . location . hostname : 'unknown'
@@ -273,7 +278,6 @@ function sendDebuggerSnapshot(probe: InitializedProbe, result: ActiveEntry): voi
273278 : undefined ,
274279 }
275280
276- const rumApi = globalObj . DD_RUM
277281 const debuggerApi = globalObj . DD_DEBUGGER !
278282
279283 // TODO: Fill out logger with the right information
@@ -286,11 +290,7 @@ function sendDebuggerSnapshot(probe: InitializedProbe, result: ActiveEntry): voi
286290 }
287291
288292 // Get the RUM internal context for trace correlation
289- const rumContext = rumApi ?. getInternalContext ?.( )
290- const dd = {
291- trace_id : rumContext ?. session_id ,
292- span_id : rumContext ?. user_action ?. id || rumContext ?. view ?. id ,
293- }
293+ const dd = getTraceCorrelationContext ( )
294294
295295 const ddtags = [
296296 buildTag ( 'sdk_version' , debuggerApi . version ) ,
@@ -307,7 +307,7 @@ function sendDebuggerSnapshot(probe: InitializedProbe, result: ActiveEntry): voi
307307 service : debuggerConfig . service ,
308308 ddtags : ddtags . join ( ',' ) ,
309309 logger,
310- dd ,
310+ ... ( dd ? { dd } : { } ) ,
311311 debugger : { snapshot } ,
312312 }
313313
@@ -327,5 +327,23 @@ function detectThreadName() {
327327 return 'unknown'
328328}
329329
330+ function getTraceCorrelationContext ( ) : TraceCorrelationContext | undefined {
331+ const rumContext = globalObj . DD_RUM ?. getInternalContext ?.( )
332+ const traceId = getStringContextValue ( rumContext , 'trace_id' )
333+ const spanId = getStringContextValue ( rumContext , 'span_id' )
334+
335+ return traceId && spanId
336+ ? {
337+ trace_id : traceId ,
338+ span_id : spanId ,
339+ }
340+ : undefined
341+ }
342+
343+ function getStringContextValue ( context : Context | undefined , key : string ) : string | undefined {
344+ const value = context ?. [ key ]
345+ return typeof value === 'string' && value !== '' ? value : undefined
346+ }
347+
330348declare const ServiceWorkerGlobalScope : typeof EventTarget
331349declare function importScripts ( ...urls : string [ ] ) : void
0 commit comments