-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathsentrySpan.test.ts
483 lines (411 loc) · 14.5 KB
/
sentrySpan.test.ts
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, getCurrentScope, setCurrentClient, timestampInSeconds } from '../../../src';
import { SentrySpan } from '../../../src/tracing/sentrySpan';
import { SPAN_STATUS_ERROR } from '../../../src/tracing/spanstatus';
import { TRACE_FLAG_NONE, TRACE_FLAG_SAMPLED, spanToJSON } from '../../../src/utils/spanUtils';
import { TestClient, getDefaultTestClientOptions } from '../../mocks/client';
describe('SentrySpan', () => {
describe('name', () => {
it('works with name', () => {
const span = new SentrySpan({ name: 'span name' });
expect(spanToJSON(span).description).toEqual('span name');
});
it('allows to update the name via updateName', () => {
const span = new SentrySpan({ name: 'span name' });
expect(spanToJSON(span).description).toEqual('span name');
span.updateName('new name');
expect(spanToJSON(span).description).toEqual('new name');
});
it('sets the source to custom when calling updateName', () => {
const span = new SentrySpan({
name: 'original name',
attributes: { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url' },
});
span.updateName('new name');
const spanJson = spanToJSON(span);
expect(spanJson.description).toEqual('new name');
expect(spanJson.data?.[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]).toEqual('custom');
});
});
describe('setters', () => {
test('setName', () => {
const span = new SentrySpan({});
expect(spanToJSON(span).description).toBeUndefined();
span.updateName('foo');
expect(spanToJSON(span).description).toBe('foo');
});
});
describe('status', () => {
test('setStatus', () => {
const span = new SentrySpan({});
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'permission_denied' });
expect(spanToJSON(span).status).toBe('permission_denied');
});
});
describe('toJSON', () => {
test('simple', () => {
const span = spanToJSON(
new SentrySpan({ traceId: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', spanId: 'bbbbbbbbbbbbbbbb' }),
);
expect(span).toHaveProperty('span_id', 'bbbbbbbbbbbbbbbb');
expect(span).toHaveProperty('trace_id', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
});
test('with parent', () => {
const spanA = new SentrySpan({ traceId: 'a', spanId: 'b' });
const spanB = new SentrySpan({
traceId: 'c',
spanId: 'd',
sampled: false,
parentSpanId: spanA.spanContext().spanId,
});
const serialized = spanToJSON(spanB);
expect(serialized).toHaveProperty('parent_span_id', 'b');
expect(serialized).toHaveProperty('span_id', 'd');
expect(serialized).toHaveProperty('trace_id', 'c');
});
test('should drop all `undefined` values', () => {
const spanA = new SentrySpan({ traceId: 'a', spanId: 'b' });
const spanB = new SentrySpan({
parentSpanId: spanA.spanContext().spanId,
spanId: 'd',
traceId: 'c',
});
const serialized = spanToJSON(spanB);
expect(serialized).toStrictEqual({
start_timestamp: expect.any(Number),
parent_span_id: 'b',
span_id: 'd',
trace_id: 'c',
origin: 'manual',
data: {
'sentry.origin': 'manual',
},
});
});
});
describe('end', () => {
test('simple', () => {
const span = new SentrySpan({});
expect(spanToJSON(span).timestamp).toBeUndefined();
span.end();
expect(spanToJSON(span).timestamp).toBeGreaterThan(1);
});
test('with endTime in seconds', () => {
const span = new SentrySpan({});
expect(spanToJSON(span).timestamp).toBeUndefined();
const endTime = Date.now() / 1000;
span.end(endTime);
expect(spanToJSON(span).timestamp).toBe(endTime);
});
test('with endTime in milliseconds', () => {
const span = new SentrySpan({});
expect(spanToJSON(span).timestamp).toBeUndefined();
const endTime = Date.now();
span.end(endTime);
expect(spanToJSON(span).timestamp).toBe(endTime / 1000);
});
test('uses sampled config for standalone span', () => {
const client = new TestClient(
getDefaultTestClientOptions({
dsn: 'https://username@domain/123',
enableSend: true,
}),
);
setCurrentClient(client);
// @ts-expect-error Accessing private transport API
const mockSend = jest.spyOn(client._transport, 'send');
const notSampledSpan = new SentrySpan({
name: 'not-sampled',
isStandalone: true,
startTimestamp: 1,
endTimestamp: 2,
sampled: false,
});
notSampledSpan.end();
expect(mockSend).not.toHaveBeenCalled();
const sampledSpan = new SentrySpan({
name: 'is-sampled',
isStandalone: true,
startTimestamp: 1,
endTimestamp: 2,
sampled: true,
});
sampledSpan.end();
expect(mockSend).toHaveBeenCalledTimes(1);
});
test('sends the span if `beforeSendSpan` does not modify the span', () => {
const beforeSendSpan = jest.fn(span => span);
const client = new TestClient(
getDefaultTestClientOptions({
dsn: 'https://username@domain/123',
enableSend: true,
beforeSendSpan,
}),
);
setCurrentClient(client);
// @ts-expect-error Accessing private transport API
const mockSend = jest.spyOn(client._transport, 'send');
const span = new SentrySpan({
name: 'test',
isStandalone: true,
startTimestamp: 1,
endTimestamp: 2,
sampled: true,
});
span.end();
expect(mockSend).toHaveBeenCalled();
});
test('does not send the span if `beforeSendSpan` drops the span', () => {
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined);
const beforeSendSpan = jest.fn(() => null);
const client = new TestClient(
getDefaultTestClientOptions({
dsn: 'https://username@domain/123',
enableSend: true,
beforeSendSpan,
}),
);
setCurrentClient(client);
const recordDroppedEventSpy = jest.spyOn(client, 'recordDroppedEvent');
// @ts-expect-error Accessing private transport API
const mockSend = jest.spyOn(client._transport, 'send');
const span = new SentrySpan({
name: 'test',
isStandalone: true,
startTimestamp: 1,
endTimestamp: 2,
sampled: true,
});
span.end();
expect(mockSend).not.toHaveBeenCalled();
expect(recordDroppedEventSpy).toHaveBeenCalledWith('before_send', 'span');
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
expect(consoleWarnSpy).toBeCalledWith(
'[Sentry] Deprecation warning: Returning null from `beforeSendSpan` will be disallowed from SDK version 9.0.0 onwards. The callback will only support mutating spans. To drop certain spans, configure the respective integrations directly.',
);
consoleWarnSpy.mockRestore();
});
test('build TransactionEvent for basic root span', () => {
const client = new TestClient(
getDefaultTestClientOptions({
dsn: 'https://username@domain/123',
}),
);
setCurrentClient(client);
const scope = getCurrentScope();
const captureEventSpy = jest.spyOn(scope, 'captureEvent').mockImplementation(() => 'testId');
const span = new SentrySpan({
name: 'test',
startTimestamp: 1,
sampled: true,
});
span.end(2);
expect(captureEventSpy).toHaveBeenCalledTimes(1);
expect(captureEventSpy).toHaveBeenCalledWith({
_metrics_summary: undefined,
contexts: {
trace: {
data: {
'sentry.origin': 'manual',
},
origin: 'manual',
span_id: expect.stringMatching(/^[a-f0-9]{16}$/),
trace_id: expect.stringMatching(/^[a-f0-9]{32}$/),
},
},
sdkProcessingMetadata: {
capturedSpanIsolationScope: undefined,
capturedSpanScope: undefined,
dynamicSamplingContext: {
environment: 'production',
public_key: 'username',
trace_id: expect.stringMatching(/^[a-f0-9]{32}$/),
transaction: 'test',
},
},
spans: [],
start_timestamp: 1,
timestamp: 2,
transaction: 'test',
type: 'transaction',
});
});
});
describe('setAttribute', () => {
it('allows to set attributes', () => {
const span = new SentrySpan();
span.setAttribute('str', 'bar');
span.setAttribute('num', 1);
span.setAttribute('zero', 0);
span.setAttribute('bool', true);
span.setAttribute('false', false);
span.setAttribute('undefined', undefined);
span.setAttribute('numArray', [1, 2]);
span.setAttribute('strArray', ['aa', 'bb']);
span.setAttribute('boolArray', [true, false]);
span.setAttribute('arrayWithUndefined', [1, undefined, 2]);
expect(span['_attributes']).toEqual({
str: 'bar',
num: 1,
zero: 0,
bool: true,
false: false,
numArray: [1, 2],
strArray: ['aa', 'bb'],
boolArray: [true, false],
arrayWithUndefined: [1, undefined, 2],
// origin is set by default to 'manual' in the SentrySpan constructor
'sentry.origin': 'manual',
});
});
it('deletes attributes when setting to `undefined`', () => {
const span = new SentrySpan();
span.setAttribute('str', 'bar');
// length 2 because `sentry.origin` is always set by default
expect(Object.keys(span['_attributes']).length).toEqual(2);
span.setAttribute('str', undefined);
expect(Object.keys(span['_attributes']).length).toEqual(1);
});
it('disallows invalid attribute types', () => {
const span = new SentrySpan();
/** @ts-expect-error this is invalid */
span.setAttribute('str', {});
/** @ts-expect-error this is invalid */
span.setAttribute('str', null);
/** @ts-expect-error this is invalid */
span.setAttribute('str', [1, 'a']);
});
});
describe('setAttributes', () => {
it('allows to set attributes', () => {
const span = new SentrySpan();
const initialAttributes = span['_attributes'];
expect(initialAttributes).toEqual({
// origin is set by default to 'manual' in the SentrySpan constructor
'sentry.origin': 'manual',
});
const newAttributes = {
str: 'bar',
num: 1,
zero: 0,
bool: true,
false: false,
undefined: undefined,
numArray: [1, 2],
strArray: ['aa', 'bb'],
boolArray: [true, false],
arrayWithUndefined: [1, undefined, 2],
};
span.setAttributes(newAttributes);
expect(span['_attributes']).toEqual({
str: 'bar',
num: 1,
zero: 0,
bool: true,
false: false,
numArray: [1, 2],
strArray: ['aa', 'bb'],
boolArray: [true, false],
arrayWithUndefined: [1, undefined, 2],
'sentry.origin': 'manual',
});
expect(span['_attributes']).not.toBe(newAttributes);
span.setAttributes({
num: 2,
numArray: [3, 4],
});
expect(span['_attributes']).toEqual({
str: 'bar',
num: 2,
zero: 0,
bool: true,
false: false,
numArray: [3, 4],
strArray: ['aa', 'bb'],
boolArray: [true, false],
arrayWithUndefined: [1, undefined, 2],
'sentry.origin': 'manual',
});
});
it('deletes attributes when setting to `undefined`', () => {
const span = new SentrySpan();
span.setAttribute('str', 'bar');
// length 2 because `sentry.origin` is always set by default
expect(Object.keys(span['_attributes']).length).toEqual(2);
span.setAttributes({ str: undefined });
expect(Object.keys(span['_attributes']).length).toEqual(1);
});
});
describe('end', () => {
it('works without endTimestamp', () => {
const span = new SentrySpan();
const now = timestampInSeconds();
span.end();
expect(spanToJSON(span).timestamp).toBeGreaterThanOrEqual(now);
});
it('works with endTimestamp in seconds', () => {
const span = new SentrySpan();
const timestamp = timestampInSeconds() - 1;
span.end(timestamp);
expect(spanToJSON(span).timestamp).toEqual(timestamp);
});
it('works with endTimestamp in milliseconds', () => {
const span = new SentrySpan();
const timestamp = Date.now() - 1000;
span.end(timestamp);
expect(spanToJSON(span).timestamp).toEqual(timestamp / 1000);
});
it('works with endTimestamp in array form', () => {
const span = new SentrySpan();
const seconds = Math.floor(timestampInSeconds() - 1);
span.end([seconds, 0]);
expect(spanToJSON(span).timestamp).toEqual(seconds);
});
it('skips if span is already ended', () => {
const startTimestamp = timestampInSeconds() - 5;
const endTimestamp = timestampInSeconds() - 1;
const span = new SentrySpan({ startTimestamp, endTimestamp });
span.end();
expect(spanToJSON(span).timestamp).toBe(endTimestamp);
});
});
describe('isRecording', () => {
it('returns true for sampled span', () => {
const span = new SentrySpan({ sampled: true });
expect(span.isRecording()).toEqual(true);
});
it('returns false for sampled, finished span', () => {
const span = new SentrySpan({ sampled: true, endTimestamp: Date.now() });
expect(span.isRecording()).toEqual(false);
});
it('returns false for unsampled span', () => {
const span = new SentrySpan({ sampled: false });
expect(span.isRecording()).toEqual(false);
});
});
describe('spanContext', () => {
it('works with default span', () => {
const span = new SentrySpan();
expect(span.spanContext()).toEqual({
spanId: span['_spanId'],
traceId: span['_traceId'],
traceFlags: TRACE_FLAG_NONE,
});
});
it('works sampled span', () => {
const span = new SentrySpan({ sampled: true });
expect(span.spanContext()).toEqual({
spanId: span['_spanId'],
traceId: span['_traceId'],
traceFlags: TRACE_FLAG_SAMPLED,
});
});
it('works unsampled span', () => {
const span = new SentrySpan({ sampled: false });
expect(span.spanContext()).toEqual({
spanId: span['_spanId'],
traceId: span['_traceId'],
traceFlags: TRACE_FLAG_NONE,
});
});
});
});