diff --git a/packages/browser/src/tracing/linkedTraces.ts b/packages/browser/src/tracing/linkedTraces.ts index 330e193f80ef..d4cfc34f3f6a 100644 --- a/packages/browser/src/tracing/linkedTraces.ts +++ b/packages/browser/src/tracing/linkedTraces.ts @@ -177,10 +177,10 @@ export function addPreviousTraceSpanLink( if (Date.now() / 1000 - previousTraceInfo.startTimestamp <= PREVIOUS_TRACE_MAX_DURATION) { if (DEBUG_BUILD) { debug.log( - `Adding previous_trace ${previousTraceSpanCtx} link to span ${{ + `Adding previous_trace \`${JSON.stringify(previousTraceSpanCtx)}\` link to span \`${JSON.stringify({ op: spanJson.op, ...span.spanContext(), - }}`, + })}\``, ); } diff --git a/packages/browser/test/tracing/linkedTraces.test.ts b/packages/browser/test/tracing/linkedTraces.test.ts index 7c075da588ef..56616c6a7692 100644 --- a/packages/browser/test/tracing/linkedTraces.test.ts +++ b/packages/browser/test/tracing/linkedTraces.test.ts @@ -1,5 +1,5 @@ import type { Span } from '@sentry/core'; -import { addChildSpanToSpan, SentrySpan, spanToJSON, timestampInSeconds } from '@sentry/core'; +import { addChildSpanToSpan, debug, SentrySpan, spanToJSON, timestampInSeconds } from '@sentry/core'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { BrowserClient } from '../../src'; import type { PreviousTraceInfo } from '../../src/tracing/linkedTraces'; @@ -201,6 +201,47 @@ describe('addPreviousTraceSpanLink', () => { }); }); + it('logs a debug message when adding a previous trace link (with stringified context)', () => { + const debugLogSpy = vi.spyOn(debug, 'log'); + + const currentSpanStart = timestampInSeconds(); + + const previousTraceInfo: PreviousTraceInfo = { + spanContext: { traceId: '123', spanId: '456', traceFlags: 1 }, + startTimestamp: currentSpanStart - PREVIOUS_TRACE_MAX_DURATION + 1, + sampleRand: 0.0126, + sampleRate: 0.5, + }; + + const currentSpan = new SentrySpan({ + name: 'test', + op: 'navigation', + startTimestamp: currentSpanStart, + parentSpanId: '789', + spanId: 'abc', + traceId: 'def', + sampled: true, + }); + + const oldPropagationContext = { + sampleRand: 0.0126, + traceId: '123', + sampled: true, + dsc: { sample_rand: '0.0126', sample_rate: '0.5' }, + }; + + addPreviousTraceSpanLink(previousTraceInfo, currentSpan, oldPropagationContext); + + expect(debugLogSpy).not.toHaveBeenCalledWith(expect.stringContaining('[object Object]')); + expect(debugLogSpy).toHaveBeenCalledWith( + expect.stringContaining( + 'Adding previous_trace `{"traceId":"123","spanId":"456","traceFlags":1}` link to span `{"op":"navigation","spanId":"abc","traceId":"def","traceFlags":1}`', + ), + ); + + debugLogSpy.mockRestore(); + }); + it(`doesn't add a previous_trace span link if the previous trace was created more than ${PREVIOUS_TRACE_MAX_DURATION}s ago`, () => { const currentSpanStart = timestampInSeconds();