Skip to content

Commit a6dcc02

Browse files
committed
fix(browser): Stringify span context in linked traces log statement
1 parent 514eeb4 commit a6dcc02

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

packages/browser/src/tracing/linkedTraces.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ export function addPreviousTraceSpanLink(
177177
if (Date.now() / 1000 - previousTraceInfo.startTimestamp <= PREVIOUS_TRACE_MAX_DURATION) {
178178
if (DEBUG_BUILD) {
179179
debug.log(
180-
`Adding previous_trace ${previousTraceSpanCtx} link to span ${{
180+
`Adding previous_trace \`${JSON.stringify(previousTraceSpanCtx)}\` link to span \`${JSON.stringify({
181181
op: spanJson.op,
182182
...span.spanContext(),
183-
}}`,
183+
})}\``,
184184
);
185185
}
186186

packages/browser/test/tracing/linkedTraces.test.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Span } from '@sentry/core';
2-
import { addChildSpanToSpan, SentrySpan, spanToJSON, timestampInSeconds } from '@sentry/core';
2+
import { addChildSpanToSpan, debug, SentrySpan, spanToJSON, timestampInSeconds } from '@sentry/core';
33
import { beforeEach, describe, expect, it, vi } from 'vitest';
44
import { BrowserClient } from '../../src';
55
import type { PreviousTraceInfo } from '../../src/tracing/linkedTraces';
@@ -201,6 +201,47 @@ describe('addPreviousTraceSpanLink', () => {
201201
});
202202
});
203203

204+
it('logs a debug message when adding a previous trace link (with stringified context)', () => {
205+
const debugLogSpy = vi.spyOn(debug, 'log');
206+
207+
const currentSpanStart = timestampInSeconds();
208+
209+
const previousTraceInfo: PreviousTraceInfo = {
210+
spanContext: { traceId: '123', spanId: '456', traceFlags: 1 },
211+
startTimestamp: currentSpanStart - PREVIOUS_TRACE_MAX_DURATION + 1,
212+
sampleRand: 0.0126,
213+
sampleRate: 0.5,
214+
};
215+
216+
const currentSpan = new SentrySpan({
217+
name: 'test',
218+
op: 'navigation',
219+
startTimestamp: currentSpanStart,
220+
parentSpanId: '789',
221+
spanId: 'abc',
222+
traceId: 'def',
223+
sampled: true,
224+
});
225+
226+
const oldPropagationContext = {
227+
sampleRand: 0.0126,
228+
traceId: '123',
229+
sampled: true,
230+
dsc: { sample_rand: '0.0126', sample_rate: '0.5' },
231+
};
232+
233+
addPreviousTraceSpanLink(previousTraceInfo, currentSpan, oldPropagationContext);
234+
235+
expect(debugLogSpy).not.toHaveBeenCalledWith(expect.stringContaining('[object Object]'));
236+
expect(debugLogSpy).toHaveBeenCalledWith(
237+
expect.stringContaining(
238+
'Adding previous_trace `{"traceId":"123","spanId":"456","traceFlags":1}` link to span `{"op":"navigation","spanId":"abc","traceId":"def","traceFlags":1}`',
239+
),
240+
);
241+
242+
debugLogSpy.mockRestore();
243+
});
244+
204245
it(`doesn't add a previous_trace span link if the previous trace was created more than ${PREVIOUS_TRACE_MAX_DURATION}s ago`, () => {
205246
const currentSpanStart = timestampInSeconds();
206247

0 commit comments

Comments
 (0)