Skip to content

Commit d25b179

Browse files
committed
ref(browser): Temporarily add sentry.previous_trace span attribute
1 parent 3c1b3c9 commit d25b179

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/previous-trace-links/default/test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ sentryTest("navigation spans link back to previous trace's root span", async ({
4949
},
5050
]);
5151

52+
expect(navigation1TraceContext?.data).toMatchObject({
53+
'sentry.previous_trace': `${pageloadTraceId}-${pageloadTraceContext?.span_id}-1`,
54+
});
55+
5256
expect(navigation2TraceContext?.links).toEqual([
5357
{
5458
trace_id: navigation1TraceId,
@@ -60,6 +64,10 @@ sentryTest("navigation spans link back to previous trace's root span", async ({
6064
},
6165
]);
6266

67+
expect(navigation2TraceContext?.data).toMatchObject({
68+
'sentry.previous_trace': `${navigation1TraceId}-${navigation1TraceContext?.span_id}-1`,
69+
});
70+
6371
expect(pageloadTraceId).not.toEqual(navigation1TraceId);
6472
expect(navigation1TraceId).not.toEqual(navigation2TraceId);
6573
expect(pageloadTraceId).not.toEqual(navigation2TraceId);

dev-packages/e2e-tests/test-applications/vue-3/src/main.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ Sentry.init({
2424
}),
2525
browserTracingIntegration({
2626
router,
27+
instrumentPageLoad: false,
28+
instrumentNavigation: false,
29+
enableInp: false,
30+
enableLongTask: false,
31+
enableLongAnimationFrame: false,
2732
}),
2833
],
2934
tunnel: `http://localhost:3031/`, // proxy server

packages/browser/src/tracing/previousTrace.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import type { Span } from '@sentry/core';
2-
import { logger, SEMANTIC_LINK_ATTRIBUTE_LINK_TYPE, spanToJSON, type SpanContextData } from '@sentry/core';
2+
import {
3+
logger,
4+
SEMANTIC_LINK_ATTRIBUTE_LINK_TYPE,
5+
spanIsSampled,
6+
spanToJSON,
7+
type SpanContextData,
8+
} from '@sentry/core';
39
import { WINDOW } from '../exports';
410
import { DEBUG_BUILD } from '../debug-build';
511

@@ -21,6 +27,8 @@ export const PREVIOUS_TRACE_MAX_DURATION = 3600;
2127
// session storage key
2228
export const PREVIOUS_TRACE_KEY = 'sentry_previous_trace';
2329

30+
export const PREVIOUS_TRACE_TMP_SPAN_ATTRIBUTE = 'sentry.previous_trace';
31+
2432
/**
2533
* Adds a previous_trace span link to the passed span if the passed
2634
* previousTraceInfo is still valid.
@@ -69,6 +77,15 @@ export function addPreviousTraceSpanLink(
6977
[SEMANTIC_LINK_ATTRIBUTE_LINK_TYPE]: 'previous_trace',
7078
},
7179
});
80+
81+
// TODO: Remove this once EAP can store span links. We currently only set this attribute so that we
82+
// can obtain the previous trace information from the EAP store. Long-term, EAP will handle
83+
// span links and then we should remove this again. Also throwing in a TODO(v10), to remind us
84+
// to check this at v10 time :)
85+
span.setAttribute(
86+
PREVIOUS_TRACE_TMP_SPAN_ATTRIBUTE,
87+
`${previousTraceInfo.spanContext.traceId}-${previousTraceInfo.spanContext.spanId}-${spanIsSampled(span) ? 1 : 0}`,
88+
);
7289
}
7390

7491
return {

packages/browser/test/tracing/previousTrace.test.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
getPreviousTraceFromSessionStorage,
66
PREVIOUS_TRACE_KEY,
77
PREVIOUS_TRACE_MAX_DURATION,
8+
PREVIOUS_TRACE_TMP_SPAN_ATTRIBUTE,
89
} from '../../src/tracing/previousTrace';
910
import { SentrySpan, spanToJSON, timestampInSeconds } from '@sentry/core';
1011
import { storePreviousTraceInSessionStorage } from '../../src/tracing/previousTrace';
@@ -34,7 +35,9 @@ describe('addPreviousTraceSpanLink', () => {
3435

3536
const updatedPreviousTraceInfo = addPreviousTraceSpanLink(previousTraceInfo, currentSpan);
3637

37-
expect(spanToJSON(currentSpan).links).toEqual([
38+
const spanJson = spanToJSON(currentSpan);
39+
40+
expect(spanJson.links).toEqual([
3841
{
3942
attributes: {
4043
'sentry.link.type': 'previous_trace',
@@ -45,6 +48,10 @@ describe('addPreviousTraceSpanLink', () => {
4548
},
4649
]);
4750

51+
expect(spanJson.data).toMatchObject({
52+
[PREVIOUS_TRACE_TMP_SPAN_ATTRIBUTE]: '123-456-1',
53+
});
54+
4855
expect(updatedPreviousTraceInfo).toEqual({
4956
spanContext: currentSpan.spanContext(),
5057
startTimestamp: currentSpanStart,
@@ -70,7 +77,11 @@ describe('addPreviousTraceSpanLink', () => {
7077

7178
const updatedPreviousTraceInfo = addPreviousTraceSpanLink(previousTraceInfo, currentSpan);
7279

73-
expect(spanToJSON(currentSpan).links).toBeUndefined();
80+
const spanJson = spanToJSON(currentSpan);
81+
82+
expect(spanJson.links).toBeUndefined();
83+
84+
expect(Object.keys(spanJson.data)).not.toContain(PREVIOUS_TRACE_TMP_SPAN_ATTRIBUTE);
7485

7586
// but still updates the previousTraceInfo to the current span
7687
expect(updatedPreviousTraceInfo).toEqual({
@@ -141,7 +152,9 @@ describe('addPreviousTraceSpanLink', () => {
141152

142153
const updatedPreviousTraceInfo = addPreviousTraceSpanLink(undefined, currentSpan);
143154

144-
expect(spanToJSON(currentSpan).links).toBeUndefined();
155+
const spanJson = spanToJSON(currentSpan);
156+
expect(spanJson.links).toBeUndefined();
157+
expect(Object.keys(spanJson.data)).not.toContain(PREVIOUS_TRACE_TMP_SPAN_ATTRIBUTE);
145158

146159
expect(updatedPreviousTraceInfo).toEqual({
147160
spanContext: currentSpan.spanContext(),
@@ -169,7 +182,9 @@ describe('addPreviousTraceSpanLink', () => {
169182

170183
const updatedPreviousTraceInfo = addPreviousTraceSpanLink(previousTraceInfo, currentSpan);
171184

172-
expect(spanToJSON(currentSpan).links).toBeUndefined();
185+
const spanJson = spanToJSON(currentSpan);
186+
expect(spanJson.links).toBeUndefined();
187+
expect(Object.keys(spanJson.data)).not.toContain(PREVIOUS_TRACE_TMP_SPAN_ATTRIBUTE);
173188

174189
expect(updatedPreviousTraceInfo).toBe(previousTraceInfo);
175190
});

0 commit comments

Comments
 (0)