Repository navigation
Expand file tree
/
Copy pathperformance.test.ts
More file actions
245 lines (216 loc) · 6.58 KB
/
Copy pathperformance.test.ts
File metadata and controls
245 lines (216 loc) · 6.58 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import {
setAsyncContextStrategy,
setCurrentClient,
startInactiveSpan,
startSpan,
startSpanManual,
withIsolationScope,
withScope,
} from '@sentry/core';
import type { Span, TransactionEvent } from '@sentry/types';
import { NodeClient, defaultStackParser } from '../src';
import { setNodeAsyncContextStrategy } from '../src/async';
import { getDefaultNodeClientOptions } from './helper/node-client-options';
const dsn = 'https://53039209a22b4ec1bcc296a3c9fdecd6@sentry.io/4291';
beforeAll(() => {
setNodeAsyncContextStrategy();
});
afterAll(() => {
setAsyncContextStrategy(undefined);
});
describe('startSpan()', () => {
it('should correctly separate spans when called after one another with interwoven timings', async () => {
const transactionEventPromise = new Promise<TransactionEvent>(resolve => {
setCurrentClient(
new NodeClient(
getDefaultNodeClientOptions({
stackParser: defaultStackParser,
tracesSampleRate: 1,
beforeSendTransaction: event => {
resolve(event);
return null;
},
dsn,
}),
),
);
});
startSpan({ name: 'first' }, () => {
return new Promise<void>(resolve => {
setTimeout(resolve, 500);
});
});
startSpan({ name: 'second' }, () => {
return new Promise<void>(resolve => {
setTimeout(resolve, 250);
});
});
const transactionEvent = await transactionEventPromise;
// Any transaction events happening shouldn't have any child spans
expect(transactionEvent.spans).toStrictEqual([]);
});
it('should correctly nest spans when called within one another', async () => {
const transactionEventPromise = new Promise<TransactionEvent>(resolve => {
setCurrentClient(
new NodeClient(
getDefaultNodeClientOptions({
stackParser: defaultStackParser,
tracesSampleRate: 1,
beforeSendTransaction: event => {
resolve(event);
return null;
},
dsn,
}),
),
);
});
startSpan({ name: 'first' }, () => {
startSpan({ name: 'second' }, () => undefined);
});
const transactionEvent = await transactionEventPromise;
expect(transactionEvent.spans).toHaveLength(1);
expect(transactionEvent.spans?.[0].description).toBe('second');
});
});
describe('startSpanManual()', () => {
it('should correctly separate spans when called after one another with interwoven timings', async () => {
const transactionEventPromise = new Promise<TransactionEvent>(resolve => {
setCurrentClient(
new NodeClient(
getDefaultNodeClientOptions({
stackParser: defaultStackParser,
tracesSampleRate: 1,
beforeSendTransaction: event => {
resolve(event);
return null;
},
dsn,
}),
),
);
});
startSpanManual({ name: 'first' }, span => {
return new Promise<void>(resolve => {
setTimeout(() => {
span.end();
resolve();
}, 500);
});
});
startSpanManual({ name: 'second' }, span => {
return new Promise<void>(resolve => {
setTimeout(() => {
span.end();
resolve();
}, 500);
});
});
const transactionEvent = await transactionEventPromise;
// Any transaction events happening shouldn't have any child spans
expect(transactionEvent.spans).toStrictEqual([]);
});
it('should correctly nest spans when called within one another', async () => {
const transactionEventPromise = new Promise<TransactionEvent>(resolve => {
setCurrentClient(
new NodeClient(
getDefaultNodeClientOptions({
stackParser: defaultStackParser,
tracesSampleRate: 1,
beforeSendTransaction: event => {
resolve(event);
return null;
},
dsn,
}),
),
);
});
startSpanManual({ name: 'first' }, span1 => {
startSpanManual({ name: 'second' }, span2 => {
span2?.end();
});
span1?.end();
});
const transactionEvent = await transactionEventPromise;
expect(transactionEvent.spans?.[0].description).toBe('second');
});
it('should use the scopes at time of creation instead of the scopes at time of termination', async () => {
const transactionEventPromise = new Promise<TransactionEvent>(resolve => {
setCurrentClient(
new NodeClient(
getDefaultNodeClientOptions({
stackParser: defaultStackParser,
tracesSampleRate: 1,
beforeSendTransaction: event => {
resolve(event);
return null;
},
dsn,
}),
),
);
});
withIsolationScope(isolationScope1 => {
isolationScope1.setTag('isolationScope', 1);
withScope(scope1 => {
scope1.setTag('scope', 1);
startSpanManual({ name: 'my-span' }, span => {
withIsolationScope(isolationScope2 => {
isolationScope2.setTag('isolationScope', 2);
withScope(scope2 => {
scope2.setTag('scope', 2);
span.end();
});
});
});
});
});
expect(await transactionEventPromise).toMatchObject({
tags: {
scope: 1,
isolationScope: 1,
},
});
});
});
describe('startInactiveSpan()', () => {
it('should use the scopes at time of creation instead of the scopes at time of termination', async () => {
const transactionEventPromise = new Promise<TransactionEvent>(resolve => {
setCurrentClient(
new NodeClient(
getDefaultNodeClientOptions({
stackParser: defaultStackParser,
tracesSampleRate: 1,
beforeSendTransaction: event => {
resolve(event);
return null;
},
dsn,
}),
),
);
});
let span: Span | undefined;
withIsolationScope(isolationScope => {
isolationScope.setTag('isolationScope', 1);
withScope(scope => {
scope.setTag('scope', 1);
span = startInactiveSpan({ name: 'my-span' });
});
});
withIsolationScope(isolationScope => {
isolationScope.setTag('isolationScope', 2);
withScope(scope => {
scope.setTag('scope', 2);
span?.end();
});
});
expect(await transactionEventPromise).toMatchObject({
tags: {
scope: 1,
isolationScope: 1,
},
});
});
});