Skip to content

Commit 3f4bdd3

Browse files
committed
Fix trace correlation
1 parent ec3a691 commit 3f4bdd3

3 files changed

Lines changed: 63 additions & 16 deletions

File tree

packages/debugger/src/domain/api.spec.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ describe('api', () => {
321321
expect(snapshot.duration).toBeGreaterThanOrEqual(10000000) // Should be in nanoseconds (>= 10ms)
322322
})
323323

324-
it('should include RUM context in logger', () => {
324+
it('should omit trace correlation when no active span context is available', () => {
325325
const probe: Probe = {
326326
id: 'test-probe',
327327
version: 0,
@@ -341,10 +341,41 @@ describe('api', () => {
341341
onReturn(probes, null, {}, {}, {})
342342

343343
const payload = mockBatchAdd.calls.mostRecent().args[0]
344-
const dd = payload.dd
345-
expect(dd).toEqual({
346-
trace_id: 'test-session',
347-
span_id: 'test-action',
344+
expect(payload.dd).toBeUndefined()
345+
})
346+
347+
it('should include trace correlation when active span context is available', () => {
348+
mockRumGetInternalContext.and.returnValue({
349+
session_id: 'test-session',
350+
view: { id: 'test-view' },
351+
user_action: { id: 'test-action' },
352+
application_id: 'test-app-id',
353+
trace_id: 'test-trace',
354+
span_id: 'test-span',
355+
})
356+
357+
const probe: Probe = {
358+
id: 'test-probe',
359+
version: 0,
360+
type: 'LOG_PROBE',
361+
where: { typeName: 'TestClass', methodName: 'traceContext' },
362+
template: 'Test',
363+
segments: [{ str: 'Test' }],
364+
captureSnapshot: false,
365+
capture: {},
366+
sampling: {},
367+
evaluateAt: 'ENTRY',
368+
}
369+
addProbe(probe)
370+
371+
const probes = getProbes('TestClass;traceContext')!
372+
onEntry(probes, {}, {})
373+
onReturn(probes, null, {}, {}, {})
374+
375+
const payload = mockBatchAdd.calls.mostRecent().args[0]
376+
expect(payload.dd).toEqual({
377+
trace_id: 'test-trace',
378+
span_id: 'test-span',
348379
})
349380
})
350381
})

packages/debugger/src/domain/api.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
1924
const globalObj = getGlobalObject<BrowserWindow & { DD_RUM?: Rum }>() // eslint-disable-line local-rules/disallow-side-effects
2025
const 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+
330348
declare const ServiceWorkerGlobalScope: typeof EventTarget
331349
declare function importScripts(...urls: string[]): void

test/e2e/scenario/debugger.scenario.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ test.describe('debugger', () => {
278278
expect(intakeRegistry.debuggerEvents[0].message).toBe('Probe hit')
279279
})
280280

281-
createTest('include RUM correlation data when RUM is active')
281+
createTest('omit trace correlation data when no active span is available')
282282
.withRum()
283283
.withDebugger()
284284
.run(async ({ intakeRegistry, flushEvents, page, browserName, servers }) => {
@@ -299,8 +299,6 @@ test.describe('debugger', () => {
299299
expect(intakeRegistry.debuggerEvents.length).toBeGreaterThanOrEqual(1)
300300

301301
const event = intakeRegistry.debuggerEvents[0]
302-
const dd = event.dd as { trace_id?: string; span_id?: string }
303-
expect(dd.trace_id).toBeDefined()
304-
expect(dd.trace_id).not.toBe('')
302+
expect(event.dd).toBeUndefined()
305303
})
306304
})

0 commit comments

Comments
 (0)