Skip to content

Commit 284eee8

Browse files
committed
Add dynamic sampling context handling to server timing tests
1 parent 48e03dd commit 284eee8

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

packages/remix/test/server/serverTimingTracePropagation.test.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { getActiveSpan, getRootSpan, getTraceData, isNodeEnv, spanToTraceHeader } from '@sentry/core';
1+
import {
2+
getActiveSpan,
3+
getDynamicSamplingContextFromSpan,
4+
getRootSpan,
5+
getTraceData,
6+
isNodeEnv,
7+
spanToTraceHeader,
8+
} from '@sentry/core';
29
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
310
import {
411
addSentryServerTimingHeader,
@@ -9,8 +16,14 @@ import {
916
} from '../../src/server/serverTimingTracePropagation';
1017

1118
// Mock @sentry/core - vi.mock is hoisted automatically
12-
const mockSpan = { spanId: 'test-span-id' };
13-
const mockRootSpan = { spanId: 'root-span-id' };
19+
const mockSpan = {
20+
spanId: 'test-span-id',
21+
spanContext: () => ({ traceId: '12345678901234567890123456789012' }),
22+
};
23+
const mockRootSpan = {
24+
spanId: 'root-span-id',
25+
spanContext: () => ({ traceId: '12345678901234567890123456789012' }),
26+
};
1427

1528
vi.mock('@sentry/core', () => ({
1629
debug: {
@@ -23,6 +36,11 @@ vi.mock('@sentry/core', () => ({
2336
'sentry-trace': '12345678901234567890123456789012-1234567890123456-1',
2437
baggage: 'sentry-environment=production,sentry-release=1.0.0',
2538
})),
39+
getDynamicSamplingContextFromSpan: vi.fn(() => ({
40+
trace_id: '12345678901234567890123456789012',
41+
environment: 'production',
42+
release: '1.0.0',
43+
})),
2644
spanToTraceHeader: vi.fn(() => '12345678901234567890123456789012-1234567890123456-1'),
2745
isNodeEnv: vi.fn(() => true),
2846
}));
@@ -94,7 +112,10 @@ describe('serverTimingTracePropagation', () => {
94112
});
95113

96114
it('uses explicitly provided span', () => {
97-
const customSpan = { spanId: 'custom-span' };
115+
const customSpan = {
116+
spanId: 'custom-span',
117+
spanContext: () => ({ traceId: 'custom-trace-id' }),
118+
};
98119
vi.mocked(spanToTraceHeader).mockReturnValue('custom-trace-id-custom-span-id-1');
99120
vi.mocked(getTraceData).mockReturnValue({
100121
'sentry-trace': 'custom-trace-id-custom-span-id-1',
@@ -134,6 +155,7 @@ describe('serverTimingTracePropagation', () => {
134155
'sentry-trace': '12345678901234567890123456789012-1234567890123456-1',
135156
baggage: '',
136157
});
158+
vi.mocked(getDynamicSamplingContextFromSpan).mockReturnValue({});
137159

138160
const result = generateSentryServerTimingHeader();
139161

@@ -144,6 +166,7 @@ describe('serverTimingTracePropagation', () => {
144166
vi.mocked(getTraceData).mockReturnValue({
145167
'sentry-trace': '12345678901234567890123456789012-1234567890123456-1',
146168
});
169+
vi.mocked(getDynamicSamplingContextFromSpan).mockReturnValue({});
147170

148171
const result = generateSentryServerTimingHeader();
149172

@@ -275,7 +298,10 @@ describe('serverTimingTracePropagation', () => {
275298
});
276299

277300
it('uses provided span option', () => {
278-
const customSpan = { spanId: 'custom' };
301+
const customSpan = {
302+
spanId: 'custom',
303+
spanContext: () => ({ traceId: 'custom-trace-id' }),
304+
};
279305
vi.mocked(spanToTraceHeader).mockReturnValue('custom-trace-header');
280306
vi.mocked(getTraceData).mockReturnValue({
281307
'sentry-trace': 'custom-trace-header',

0 commit comments

Comments
 (0)