Skip to content

Commit 5201fc8

Browse files
committed
fix tests
1 parent 8bd3ef8 commit 5201fc8

File tree

2 files changed

+224
-26
lines changed

2 files changed

+224
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
import type { Client } from '@sentry/types';
2+
import { Scope, SentrySpan, getCurrentScope, getGlobalScope, getIsolationScope, setCurrentClient } from '../../../src';
3+
import { freezeDscOnSpan } from '../../../src/tracing/dynamicSamplingContext';
4+
import { getSentryHeaders } from '../../../src/tracing/sentryHeaders';
5+
import type { TestClientOptions } from '../../mocks/client';
6+
import { TestClient, getDefaultTestClientOptions } from '../../mocks/client';
7+
8+
const dsn = 'https://[email protected]/42';
9+
10+
const SCOPE_TRACE_ID = '12345678901234567890123456789012';
11+
const SCOPE_SPAN_ID = '1234567890123456';
12+
13+
function setupClient(opts?: TestClientOptions): Client {
14+
getCurrentScope().clear();
15+
getIsolationScope().clear();
16+
getGlobalScope().clear();
17+
18+
getCurrentScope().setPropagationContext({
19+
traceId: SCOPE_TRACE_ID,
20+
spanId: SCOPE_SPAN_ID,
21+
});
22+
23+
const options = getDefaultTestClientOptions({
24+
dsn,
25+
...opts,
26+
});
27+
const client = new TestClient(options);
28+
setCurrentClient(client);
29+
client.init();
30+
31+
return client;
32+
}
33+
34+
describe('getSentryHeaders', () => {
35+
beforeEach(() => {});
36+
37+
afterEach(() => {
38+
jest.clearAllMocks();
39+
});
40+
41+
it('works with a minimal client', () => {
42+
const client = setupClient();
43+
44+
const { sentryTrace, baggage } = getSentryHeaders({ client });
45+
46+
expect(sentryTrace).toEqual(`${SCOPE_TRACE_ID}-${SCOPE_SPAN_ID}`);
47+
expect(baggage).toEqual(`sentry-environment=production,sentry-public_key=123,sentry-trace_id=${SCOPE_TRACE_ID}`);
48+
});
49+
50+
it('allows to pass a specific scope', () => {
51+
const client = setupClient();
52+
53+
const traceId = '12345678901234567890123456789099';
54+
const spanId = '1234567890123499';
55+
const scope = new Scope();
56+
scope.setPropagationContext({
57+
traceId,
58+
spanId,
59+
});
60+
61+
const { sentryTrace, baggage } = getSentryHeaders({ client, scope });
62+
63+
expect(sentryTrace).toEqual(`${traceId}-${spanId}`);
64+
expect(baggage).toEqual(`sentry-environment=production,sentry-public_key=123,sentry-trace_id=${traceId}`);
65+
});
66+
67+
it('uses DSC from scope, if available', () => {
68+
const client = setupClient();
69+
70+
const traceId = '12345678901234567890123456789099';
71+
const spanId = '1234567890123499';
72+
const scope = new Scope();
73+
scope.setPropagationContext({
74+
traceId,
75+
spanId,
76+
dsc: {
77+
environment: 'test-dev',
78+
public_key: '456',
79+
trace_id: '12345678901234567890123456789088',
80+
},
81+
});
82+
83+
const { sentryTrace, baggage } = getSentryHeaders({ client, scope });
84+
85+
expect(sentryTrace).toEqual(`${traceId}-${spanId}`);
86+
expect(baggage).toEqual(
87+
'sentry-environment=test-dev,sentry-public_key=456,sentry-trace_id=12345678901234567890123456789088',
88+
);
89+
});
90+
91+
it('works with a minimal unsampled span', () => {
92+
const client = setupClient();
93+
94+
const traceId = '12345678901234567890123456789099';
95+
const spanId = '1234567890123499';
96+
97+
const span = new SentrySpan({
98+
traceId,
99+
spanId,
100+
sampled: false,
101+
});
102+
103+
const { sentryTrace, baggage } = getSentryHeaders({ client, span });
104+
105+
expect(sentryTrace).toEqual(`${traceId}-${spanId}-0`);
106+
expect(baggage).toEqual(`sentry-environment=production,sentry-public_key=123,sentry-trace_id=${traceId}`);
107+
});
108+
109+
it('works with a minimal sampled span', () => {
110+
const client = setupClient();
111+
112+
const traceId = '12345678901234567890123456789099';
113+
const spanId = '1234567890123499';
114+
115+
const span = new SentrySpan({
116+
traceId,
117+
spanId,
118+
sampled: true,
119+
});
120+
121+
const { sentryTrace, baggage } = getSentryHeaders({ client, span });
122+
123+
expect(sentryTrace).toEqual(`${traceId}-${spanId}-1`);
124+
expect(baggage).toEqual(`sentry-environment=production,sentry-public_key=123,sentry-trace_id=${traceId}`);
125+
});
126+
127+
it('works with a SentrySpan with frozen DSC', () => {
128+
const client = setupClient();
129+
130+
const traceId = '12345678901234567890123456789099';
131+
const spanId = '1234567890123499';
132+
133+
const span = new SentrySpan({
134+
traceId,
135+
spanId,
136+
sampled: true,
137+
});
138+
139+
freezeDscOnSpan(span, {
140+
environment: 'test-dev',
141+
public_key: '456',
142+
trace_id: '12345678901234567890123456789088',
143+
});
144+
145+
const { sentryTrace, baggage } = getSentryHeaders({ client, span });
146+
147+
expect(sentryTrace).toEqual(`${traceId}-${spanId}-1`);
148+
expect(baggage).toEqual(
149+
'sentry-environment=test-dev,sentry-public_key=456,sentry-trace_id=12345678901234567890123456789088',
150+
);
151+
});
152+
153+
it('works with an OTEL span with frozen DSC in traceState', () => {
154+
const client = setupClient();
155+
156+
const traceId = '12345678901234567890123456789099';
157+
const spanId = '1234567890123499';
158+
159+
const span = new SentrySpan({
160+
traceId,
161+
spanId,
162+
sampled: true,
163+
});
164+
165+
span.spanContext = () => {
166+
const traceState = {
167+
set: () => traceState,
168+
unset: () => traceState,
169+
get: (key: string) => {
170+
if (key === 'sentry.dsc') {
171+
return 'sentry-environment=test-dev,sentry-public_key=456,sentry-trace_id=12345678901234567890123456789088';
172+
}
173+
return undefined;
174+
},
175+
serialize: () => '',
176+
};
177+
178+
return {
179+
traceId,
180+
spanId,
181+
sampled: true,
182+
traceFlags: 1,
183+
traceState,
184+
};
185+
};
186+
187+
const { sentryTrace, baggage } = getSentryHeaders({ client, span });
188+
189+
expect(sentryTrace).toEqual(`${traceId}-${spanId}-1`);
190+
expect(baggage).toEqual(
191+
'sentry-environment=test-dev,sentry-public_key=456,sentry-trace_id=12345678901234567890123456789088',
192+
);
193+
});
194+
});

packages/core/test/lib/utils/traceData.test.ts

+30-26
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,20 @@ import { SentrySpan, getTraceData } from '../../../src/';
22
import * as SentryCoreCurrentScopes from '../../../src/currentScopes';
33
import * as SentryCoreExports from '../../../src/exports';
44
import * as SentryCoreTracing from '../../../src/tracing';
5+
import * as SentryCoreTracingDsc from '../../../src/tracing/dynamicSamplingContext';
6+
import { freezeDscOnSpan } from '../../../src/tracing/dynamicSamplingContext';
57
import * as SentryCoreSpanUtils from '../../../src/utils/spanUtils';
68

79
import { isValidBaggageString } from '../../../src/utils/traceData';
810

911
const TRACE_FLAG_SAMPLED = 1;
1012

11-
const mockedSpan = new SentrySpan({
12-
traceId: '12345678901234567890123456789012',
13-
spanId: '1234567890123456',
14-
sampled: true,
15-
});
16-
1713
const mockedClient = {} as any;
1814

1915
const mockedScope = {
2016
getPropagationContext: () => ({
2117
traceId: '123',
18+
spanId: '456',
2219
}),
2320
} as any;
2421

@@ -33,11 +30,16 @@ describe('getTraceData', () => {
3330

3431
it('returns the tracing data from the span, if a span is available', () => {
3532
{
36-
jest.spyOn(SentryCoreTracing, 'getDynamicSamplingContextFromSpan').mockReturnValueOnce({
37-
environment: 'production',
33+
const mockedSpan = new SentrySpan({
34+
traceId: '12345678901234567890123456789012',
35+
spanId: '1234567890123456',
36+
sampled: true,
3837
});
39-
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => mockedSpan);
40-
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(() => mockedScope);
38+
freezeDscOnSpan(mockedSpan, { environment: 'production' });
39+
40+
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementation(() => mockedSpan);
41+
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementation(() => mockedScope);
42+
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementation(() => mockedClient);
4143

4244
const data = getTraceData();
4345

@@ -49,8 +51,8 @@ describe('getTraceData', () => {
4951
});
5052

5153
it('returns propagationContext DSC data if no span is available', () => {
52-
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => undefined);
53-
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(
54+
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementation(() => undefined);
55+
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementation(
5456
() =>
5557
({
5658
getPropagationContext: () => ({
@@ -65,7 +67,7 @@ describe('getTraceData', () => {
6567
}),
6668
}) as any,
6769
);
68-
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementationOnce(() => mockedClient);
70+
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementation(() => mockedClient);
6971

7072
const traceData = getTraceData();
7173

@@ -76,13 +78,13 @@ describe('getTraceData', () => {
7678
});
7779

7880
it('returns only the `sentry-trace` value if no DSC is available', () => {
79-
jest.spyOn(SentryCoreTracing, 'getDynamicSamplingContextFromClient').mockReturnValueOnce({
81+
jest.spyOn(SentryCoreTracingDsc, 'getDynamicSamplingContextFromSpan').mockReturnValue({
8082
trace_id: '',
8183
public_key: undefined,
8284
});
8385

8486
// @ts-expect-error - we don't need to provide all the properties
85-
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => ({
87+
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementation(() => ({
8688
isRecording: () => true,
8789
spanContext: () => {
8890
return {
@@ -93,8 +95,13 @@ describe('getTraceData', () => {
9395
},
9496
}));
9597

96-
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(() => mockedScope);
97-
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementationOnce(() => mockedClient);
98+
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementation(() => mockedScope);
99+
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementation(() => {
100+
return {
101+
getOptions: () => ({}),
102+
getDsn: () => ({}),
103+
} as any;
104+
});
98105

99106
const traceData = getTraceData();
100107

@@ -103,14 +110,14 @@ describe('getTraceData', () => {
103110
});
104111
});
105112

106-
it('returns only the `sentry-trace` tag if no DSC is available without a client', () => {
113+
it('returns empty object without a client', () => {
107114
jest.spyOn(SentryCoreTracing, 'getDynamicSamplingContextFromClient').mockReturnValueOnce({
108115
trace_id: '',
109116
public_key: undefined,
110117
});
111118

112119
// @ts-expect-error - we don't need to provide all the properties
113-
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => ({
120+
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementation(() => ({
114121
isRecording: () => true,
115122
spanContext: () => {
116123
return {
@@ -120,20 +127,17 @@ describe('getTraceData', () => {
120127
};
121128
},
122129
}));
123-
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementationOnce(() => mockedScope);
124-
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementationOnce(() => undefined);
130+
jest.spyOn(SentryCoreCurrentScopes, 'getCurrentScope').mockImplementation(() => mockedScope);
131+
jest.spyOn(SentryCoreCurrentScopes, 'getClient').mockImplementation(() => undefined);
125132

126133
const traceData = getTraceData();
127134

128-
expect(traceData).toEqual({
129-
'sentry-trace': '12345678901234567890123456789012-1234567890123456-1',
130-
});
131-
expect('baggage' in traceData).toBe(false);
135+
expect(traceData).toEqual({});
132136
});
133137

134138
it('returns an empty object if the `sentry-trace` value is invalid', () => {
135139
// @ts-expect-error - we don't need to provide all the properties
136-
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementationOnce(() => ({
140+
jest.spyOn(SentryCoreSpanUtils, 'getActiveSpan').mockImplementation(() => ({
137141
isRecording: () => true,
138142
spanContext: () => {
139143
return {

0 commit comments

Comments
 (0)