-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathdynamicSamplingContext.test.ts
More file actions
129 lines (108 loc) · 4.38 KB
/
Copy pathdynamicSamplingContext.test.ts
File metadata and controls
129 lines (108 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import type { TransactionSource } from '@sentry/types';
import { Hub, SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, makeMain } from '../../../src';
import { Transaction, getDynamicSamplingContextFromSpan, startInactiveSpan } from '../../../src/tracing';
import { addTracingExtensions } from '../../../src/tracing';
import { TestClient, getDefaultTestClientOptions } from '../../mocks/client';
describe('getDynamicSamplingContextFromSpan', () => {
// eslint-disable-next-line deprecation/deprecation
let hub: Hub;
beforeEach(() => {
const options = getDefaultTestClientOptions({ tracesSampleRate: 1.0, release: '1.0.1' });
const client = new TestClient(options);
// eslint-disable-next-line deprecation/deprecation
hub = new Hub(client);
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);
addTracingExtensions();
});
afterEach(() => {
jest.resetAllMocks();
});
test('returns the DSC provided during transaction creation', () => {
// eslint-disable-next-line deprecation/deprecation -- using old API on purpose
const transaction = new Transaction({
name: 'tx',
metadata: { dynamicSamplingContext: { environment: 'myEnv' } },
});
const dynamicSamplingContext = getDynamicSamplingContextFromSpan(transaction);
expect(dynamicSamplingContext).toStrictEqual({ environment: 'myEnv' });
});
test('returns a new DSC, if no DSC was provided during transaction creation (via attributes)', () => {
const transaction = startInactiveSpan({ name: 'tx' });
// Setting the attribute should overwrite the computed values
transaction?.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, 0.56);
transaction?.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route');
const dynamicSamplingContext = getDynamicSamplingContextFromSpan(transaction!);
expect(dynamicSamplingContext).toStrictEqual({
release: '1.0.1',
environment: 'production',
sampled: 'true',
sample_rate: '0.56',
trace_id: expect.any(String),
transaction: 'tx',
});
});
test('returns a new DSC, if no DSC was provided during transaction creation (via deprecated metadata)', () => {
const transaction = startInactiveSpan({
name: 'tx',
});
const dynamicSamplingContext = getDynamicSamplingContextFromSpan(transaction!);
expect(dynamicSamplingContext).toStrictEqual({
release: '1.0.1',
environment: 'production',
sampled: 'true',
sample_rate: '1',
trace_id: expect.any(String),
transaction: 'tx',
});
});
test('returns a new DSC, if no DSC was provided during transaction creation (via new Txn and deprecated metadata)', () => {
// eslint-disable-next-line deprecation/deprecation -- using old API on purpose
const transaction = new Transaction({
name: 'tx',
metadata: {
sampleRate: 0.56,
source: 'route',
},
sampled: true,
});
const dynamicSamplingContext = getDynamicSamplingContextFromSpan(transaction!);
expect(dynamicSamplingContext).toStrictEqual({
release: '1.0.1',
environment: 'production',
sampled: 'true',
sample_rate: '0.56',
trace_id: expect.any(String),
transaction: 'tx',
});
});
describe('Including transaction name in DSC', () => {
test('is not included if transaction source is url', () => {
// eslint-disable-next-line deprecation/deprecation -- using old API on purpose
const transaction = new Transaction({
name: 'tx',
metadata: {
source: 'url',
sampleRate: 0.56,
},
});
const dsc = getDynamicSamplingContextFromSpan(transaction);
expect(dsc.transaction).toBeUndefined();
});
test.each([
['is included if transaction source is parameterized route/url', 'route'],
['is included if transaction source is a custom name', 'custom'],
])('%s', (_: string, source) => {
const transaction = startInactiveSpan({
name: 'tx',
metadata: {
...(source && { source: source as TransactionSource }),
},
});
// Only setting the attribute manually because we're directly calling new Transaction()
transaction?.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source);
const dsc = getDynamicSamplingContextFromSpan(transaction!);
expect(dsc.transaction).toEqual('tx');
});
});
});