Skip to content

Commit 228a478

Browse files
s1gr1donurtemizkan
authored andcommitted
fix(browser): Stringify span context in linked traces log statement (#18376)
I came upon this log `Sentry Logger [log]: Adding previous_trace [object Object] link to span [object Object]` and this PR fixes this by stringifying the context. One concern I have with that is that the object could be too large (stringifying takes too long) or circular. But this should be very unlikely in this case. However, if someone else shares this concerns we might change the log to either limit the depth or to only log specific entries of the object (might add bundle size). Closes #18377
1 parent e0ebf8f commit 228a478

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
@@ -176,10 +176,10 @@ export function addPreviousTraceSpanLink(
176176
if (Date.now() / 1000 - previousTraceInfo.startTimestamp <= PREVIOUS_TRACE_MAX_DURATION) {
177177
if (DEBUG_BUILD) {
178178
debug.log(
179-
`Adding previous_trace ${previousTraceSpanCtx} link to span ${{
179+
`Adding previous_trace \`${JSON.stringify(previousTraceSpanCtx)}\` link to span \`${JSON.stringify({
180180
op: spanJson.op,
181181
...span.spanContext(),
182-
}}`,
182+
})}\``,
183183
);
184184
}
185185

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)