-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcaptureSpan.test.ts
More file actions
662 lines (582 loc) · 22.3 KB
/
captureSpan.test.ts
File metadata and controls
662 lines (582 loc) · 22.3 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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
import { describe, expect, it, vi } from 'vitest';
import type { StreamedSpanJSON } from '../../../../src';
import {
captureSpan,
SEMANTIC_ATTRIBUTE_SENTRY_ENVIRONMENT,
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_RELEASE,
SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE,
SEMANTIC_ATTRIBUTE_SENTRY_SDK_INTEGRATIONS,
SEMANTIC_ATTRIBUTE_SENTRY_SDK_NAME,
SEMANTIC_ATTRIBUTE_SENTRY_SDK_VERSION,
SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_ID,
SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_NAME,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
SEMANTIC_ATTRIBUTE_USER_EMAIL,
SEMANTIC_ATTRIBUTE_USER_ID,
SEMANTIC_ATTRIBUTE_USER_IP_ADDRESS,
SEMANTIC_ATTRIBUTE_USER_USERNAME,
startInactiveSpan,
startSpan,
withScope,
withStreamedSpan,
} from '../../../../src';
import { inferSpanDataFromOtelAttributes, safeSetSpanJSONAttributes } from '../../../../src/tracing/spans/captureSpan';
import { getDefaultTestClientOptions, TestClient } from '../../../mocks/client';
describe('captureSpan', () => {
it.each([true, false, undefined])(
'always applies scope user attributes to spans (sendDefaultPii: %s)',
sendDefaultPii => {
const client = new TestClient(
getDefaultTestClientOptions({
dsn: 'https://dsn@ingest.f00.f00/1',
tracesSampleRate: 1,
release: '1.0.0',
environment: 'staging',
sendDefaultPii,
}),
);
const span = withScope(scope => {
scope.setClient(client);
scope.setUser({
id: '123',
email: 'user@example.com',
username: 'testuser',
ip_address: '127.0.0.1',
});
const span = startInactiveSpan({ name: 'my-span', attributes: { 'sentry.op': 'http.client' } });
span.end();
return span;
});
expect(captureSpan(span, client)).toStrictEqual({
span_id: expect.stringMatching(/^[\da-f]{16}$/),
trace_id: expect.stringMatching(/^[\da-f]{32}$/),
parent_span_id: undefined,
links: undefined,
start_timestamp: expect.any(Number),
name: 'my-span',
end_timestamp: expect.any(Number),
status: 'ok',
is_segment: true,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: {
type: 'string',
value: 'http.client',
},
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: {
type: 'string',
value: 'manual',
},
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: {
type: 'integer',
value: 1,
},
[SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_NAME]: {
value: 'my-span',
type: 'string',
},
[SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_ID]: {
value: span.spanContext().spanId,
type: 'string',
},
'sentry.span.source': {
value: 'custom',
type: 'string',
},
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: {
value: 'custom',
type: 'string',
},
[SEMANTIC_ATTRIBUTE_SENTRY_RELEASE]: {
value: '1.0.0',
type: 'string',
},
[SEMANTIC_ATTRIBUTE_SENTRY_ENVIRONMENT]: {
value: 'staging',
type: 'string',
},
[SEMANTIC_ATTRIBUTE_USER_ID]: {
value: '123',
type: 'string',
},
[SEMANTIC_ATTRIBUTE_USER_EMAIL]: {
value: 'user@example.com',
type: 'string',
},
[SEMANTIC_ATTRIBUTE_USER_USERNAME]: {
value: 'testuser',
type: 'string',
},
[SEMANTIC_ATTRIBUTE_USER_IP_ADDRESS]: {
value: '127.0.0.1',
type: 'string',
},
},
_segmentSpan: span,
});
},
);
it('captures sdk name and version if available', () => {
const client = new TestClient(
getDefaultTestClientOptions({
dsn: 'https://dsn@ingest.f00.f00/1',
tracesSampleRate: 1,
release: '1.0.0',
environment: 'staging',
_metadata: {
sdk: {
name: 'sentry.javascript.node',
version: '1.0.0',
integrations: ['UnhandledRejection', 'Dedupe'],
},
},
}),
);
const span = withScope(scope => {
scope.setClient(client);
scope.setUser({
id: '123',
email: 'user@example.com',
username: 'testuser',
ip_address: '127.0.0.1',
});
const span = startInactiveSpan({ name: 'my-span', attributes: { 'sentry.op': 'http.client' } });
span.end();
return span;
});
expect(captureSpan(span, client)).toStrictEqual({
span_id: expect.stringMatching(/^[\da-f]{16}$/),
trace_id: expect.stringMatching(/^[\da-f]{32}$/),
parent_span_id: undefined,
links: undefined,
start_timestamp: expect.any(Number),
name: 'my-span',
end_timestamp: expect.any(Number),
status: 'ok',
is_segment: true,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: {
type: 'string',
value: 'http.client',
},
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: {
type: 'string',
value: 'manual',
},
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: {
type: 'integer',
value: 1,
},
[SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_NAME]: {
value: 'my-span',
type: 'string',
},
[SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_ID]: {
value: span.spanContext().spanId,
type: 'string',
},
'sentry.span.source': {
value: 'custom',
type: 'string',
},
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: {
value: 'custom',
type: 'string',
},
[SEMANTIC_ATTRIBUTE_SENTRY_RELEASE]: {
value: '1.0.0',
type: 'string',
},
[SEMANTIC_ATTRIBUTE_SENTRY_ENVIRONMENT]: {
value: 'staging',
type: 'string',
},
[SEMANTIC_ATTRIBUTE_SENTRY_SDK_NAME]: {
value: 'sentry.javascript.node',
type: 'string',
},
[SEMANTIC_ATTRIBUTE_SENTRY_SDK_VERSION]: {
value: '1.0.0',
type: 'string',
},
[SEMANTIC_ATTRIBUTE_USER_ID]: {
value: '123',
type: 'string',
},
[SEMANTIC_ATTRIBUTE_USER_EMAIL]: {
value: 'user@example.com',
type: 'string',
},
[SEMANTIC_ATTRIBUTE_USER_USERNAME]: {
value: 'testuser',
type: 'string',
},
[SEMANTIC_ATTRIBUTE_USER_IP_ADDRESS]: {
value: '127.0.0.1',
type: 'string',
},
},
_segmentSpan: span,
});
});
it('adds sentry.sdk.integrations to segment spans as an array attribute', () => {
const client = new TestClient(
getDefaultTestClientOptions({
dsn: 'https://dsn@ingest.f00.f00/1',
tracesSampleRate: 1,
release: '1.0.0',
environment: 'staging',
integrations: [
{ name: 'InboundFilters', setupOnce: () => {} },
{ name: 'BrowserTracing', setupOnce: () => {} },
],
_metadata: {
sdk: {
name: 'sentry.javascript.browser',
version: '9.0.0',
},
},
}),
);
client.init();
const span = withScope(scope => {
scope.setClient(client);
const span = startInactiveSpan({ name: 'my-span', attributes: { 'sentry.op': 'http.client' } });
span.end();
return span;
});
expect(captureSpan(span, client)).toStrictEqual({
span_id: expect.stringMatching(/^[\da-f]{16}$/),
trace_id: expect.stringMatching(/^[\da-f]{32}$/),
parent_span_id: undefined,
links: undefined,
start_timestamp: expect.any(Number),
name: 'my-span',
end_timestamp: expect.any(Number),
status: 'ok',
is_segment: true,
attributes: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: { type: 'string', value: 'http.client' },
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: { type: 'string', value: 'manual' },
[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: { type: 'integer', value: 1 },
[SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_NAME]: { value: 'my-span', type: 'string' },
[SEMANTIC_ATTRIBUTE_SENTRY_SEGMENT_ID]: { value: span.spanContext().spanId, type: 'string' },
'sentry.span.source': { value: 'custom', type: 'string' },
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: { value: 'custom', type: 'string' },
[SEMANTIC_ATTRIBUTE_SENTRY_RELEASE]: { value: '1.0.0', type: 'string' },
[SEMANTIC_ATTRIBUTE_SENTRY_ENVIRONMENT]: { value: 'staging', type: 'string' },
[SEMANTIC_ATTRIBUTE_SENTRY_SDK_NAME]: { value: 'sentry.javascript.browser', type: 'string' },
[SEMANTIC_ATTRIBUTE_SENTRY_SDK_VERSION]: { value: '9.0.0', type: 'string' },
[SEMANTIC_ATTRIBUTE_SENTRY_SDK_INTEGRATIONS]: {
type: 'array',
value: ['InboundFilters', 'BrowserTracing'],
},
},
_segmentSpan: span,
});
});
it('does not add sentry.sdk.integrations to non-segment child spans', () => {
const client = new TestClient(
getDefaultTestClientOptions({
dsn: 'https://dsn@ingest.f00.f00/1',
tracesSampleRate: 1,
integrations: [{ name: 'InboundFilters', setupOnce: () => {} }],
}),
);
client.init();
const serializedChild = withScope(scope => {
scope.setClient(client);
return startSpan({ name: 'segment' }, () => {
const childSpan = startInactiveSpan({ name: 'child' });
childSpan.end();
return captureSpan(childSpan, client);
});
});
expect(serializedChild.is_segment).toBe(false);
expect(serializedChild.attributes?.[SEMANTIC_ATTRIBUTE_SENTRY_SDK_INTEGRATIONS]).toBeUndefined();
});
describe('client hooks', () => {
it('calls processSpan and processSegmentSpan hooks for a segment span', () => {
const client = new TestClient(
getDefaultTestClientOptions({
dsn: 'https://dsn@ingest.f00.f00/1',
tracesSampleRate: 1,
release: '1.0.0',
environment: 'staging',
}),
);
const processSpanFn = vi.fn();
const processSegmentSpanFn = vi.fn();
client.on('processSpan', processSpanFn);
client.on('processSegmentSpan', processSegmentSpanFn);
const span = startInactiveSpan({ name: 'my-span', attributes: { 'sentry.op': 'http.client' } });
captureSpan(span, client);
expect(processSpanFn).toHaveBeenCalledWith(expect.objectContaining({ span_id: span.spanContext().spanId }));
expect(processSegmentSpanFn).toHaveBeenCalledWith(
expect.objectContaining({ span_id: span.spanContext().spanId }),
);
});
it('only calls processSpan hook for a child span', () => {
const client = new TestClient(
getDefaultTestClientOptions({
dsn: 'https://dsn@ingest.f00.f00/1',
tracesSampleRate: 1,
release: '1.0.0',
environment: 'staging',
sendDefaultPii: true,
}),
);
const processSpanFn = vi.fn();
const processSegmentSpanFn = vi.fn();
client.on('processSpan', processSpanFn);
client.on('processSegmentSpan', processSegmentSpanFn);
const serializedChildSpan = withScope(scope => {
scope.setClient(client);
scope.setUser({
id: '123',
email: 'user@example.com',
username: 'testuser',
ip_address: '127.0.0.1',
});
return startSpan({ name: 'segment' }, () => {
const childSpan = startInactiveSpan({ name: 'child' });
childSpan.end();
return captureSpan(childSpan, client);
});
});
expect(serializedChildSpan?.name).toBe('child');
expect(serializedChildSpan?.is_segment).toBe(false);
expect(processSpanFn).toHaveBeenCalledWith(expect.objectContaining({ span_id: serializedChildSpan?.span_id }));
expect(processSegmentSpanFn).not.toHaveBeenCalled();
});
});
describe('beforeSendSpan', () => {
it('applies beforeSendSpan if it is a span streaming compatible callback', () => {
const beforeSendSpan = withStreamedSpan(vi.fn(span => span));
const client = new TestClient(
getDefaultTestClientOptions({
dsn: 'https://dsn@ingest.f00.f00/1',
tracesSampleRate: 1,
release: '1.0.0',
environment: 'staging',
beforeSendSpan,
}),
);
const span = startInactiveSpan({ name: 'my-span', attributes: { 'sentry.op': 'http.client' } });
span.end();
captureSpan(span, client);
expect(beforeSendSpan).toHaveBeenCalledWith(expect.objectContaining({ span_id: span.spanContext().spanId }));
});
it("doesn't apply beforeSendSpan if it is not a span streaming compatible callback", () => {
const beforeSendSpan = vi.fn(span => span);
const client = new TestClient(
getDefaultTestClientOptions({
dsn: 'https://dsn@ingest.f00.f00/1',
tracesSampleRate: 1,
release: '1.0.0',
environment: 'staging',
beforeSendSpan,
}),
);
const span = startInactiveSpan({ name: 'my-span', attributes: { 'sentry.op': 'http.client' } });
span.end();
captureSpan(span, client);
expect(beforeSendSpan).not.toHaveBeenCalled();
});
it('logs a warning if the beforeSendSpan callback returns null', () => {
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
// @ts-expect-error - the types dissallow returning null but this is javascript, so we need to test it
const beforeSendSpan = withStreamedSpan(() => null);
const client = new TestClient(
getDefaultTestClientOptions({
dsn: 'https://dsn@ingest.f00.f00/1',
tracesSampleRate: 1,
release: '1.0.0',
environment: 'staging',
beforeSendSpan,
}),
);
const span = startInactiveSpan({ name: 'my-span', attributes: { 'sentry.op': 'http.client' } });
span.end();
captureSpan(span, client);
expect(consoleWarnSpy).toHaveBeenCalledWith(
'[Sentry] Returning null from `beforeSendSpan` is disallowed. To drop certain spans, configure the respective integrations directly or use `ignoreSpans`.',
);
consoleWarnSpy.mockRestore();
});
});
});
describe('safeSetSpanJSONAttributes', () => {
it('sets attributes that do not exist', () => {
const spanJSON = { attributes: { a: 1, b: 2 } };
// @ts-expect-error - only passing a partial object for this test
safeSetSpanJSONAttributes(spanJSON, { c: 3 });
expect(spanJSON.attributes).toEqual({ a: 1, b: 2, c: 3 });
});
it("doesn't set attributes that already exist", () => {
const spanJSON = { attributes: { a: 1, b: 2 } };
// @ts-expect-error - only passing a partial object for this test
safeSetSpanJSONAttributes(spanJSON, { a: 3 });
expect(spanJSON.attributes).toEqual({ a: 1, b: 2 });
});
it.each([null, undefined])("doesn't overwrite attributes previously set to %s", val => {
const spanJSON = { attributes: { a: val, b: 2 } };
// @ts-expect-error - only passing a partial object for this test
safeSetSpanJSONAttributes(spanJSON, { a: 1 });
expect(spanJSON.attributes).toEqual({ a: val, b: 2 });
});
it("doesn't overwrite falsy attribute values (%s)", () => {
const spanJSON = { attributes: { a: false, b: '', c: 0 } };
// @ts-expect-error - only passing a partial object for this test
safeSetSpanJSONAttributes(spanJSON, { a: 1, b: 'test', c: 1 });
expect(spanJSON.attributes).toEqual({ a: false, b: '', c: 0 });
});
it('handles an undefined attributes property', () => {
const spanJSON: Partial<StreamedSpanJSON> = {};
// @ts-expect-error - only passing a partial object for this test
safeSetSpanJSONAttributes(spanJSON, { a: 1 });
expect(spanJSON.attributes).toEqual({ a: 1 });
});
it("doesn't apply undefined or null values to attributes", () => {
const spanJSON = { attributes: {} };
// @ts-expect-error - only passing a partial object for this test
safeSetSpanJSONAttributes(spanJSON, { a: undefined, b: null });
expect(spanJSON.attributes).toEqual({});
});
});
describe('inferSpanDataFromOtelAttributes', () => {
function makeSpanJSON(name: string, attributes: Record<string, unknown>): StreamedSpanJSON {
return {
name,
span_id: 'abc123',
trace_id: 'def456',
start_timestamp: 0,
end_timestamp: 1,
status: 'ok',
is_segment: false,
attributes,
};
}
describe('http spans', () => {
it('infers http.client op for CLIENT kind', () => {
const spanJSON = makeSpanJSON('GET', { 'http.request.method': 'GET' });
inferSpanDataFromOtelAttributes(spanJSON, 2); // SPAN_KIND_CLIENT
expect(spanJSON.attributes?.['sentry.op']).toBe('http.client');
});
it('infers http.server op for SERVER kind', () => {
const spanJSON = makeSpanJSON('GET', { 'http.request.method': 'GET' });
inferSpanDataFromOtelAttributes(spanJSON, 1); // SPAN_KIND_SERVER
expect(spanJSON.attributes?.['sentry.op']).toBe('http.server');
});
it('infers http op when kind is unknown', () => {
const spanJSON = makeSpanJSON('GET', { 'http.request.method': 'GET' });
inferSpanDataFromOtelAttributes(spanJSON);
expect(spanJSON.attributes?.['sentry.op']).toBe('http');
});
it('appends prefetch to op', () => {
const spanJSON = makeSpanJSON('GET', { 'http.request.method': 'GET', 'sentry.http.prefetch': true });
inferSpanDataFromOtelAttributes(spanJSON, 2);
expect(spanJSON.attributes?.['sentry.op']).toBe('http.client.prefetch');
});
it('sets name and source from http.route', () => {
const spanJSON = makeSpanJSON('GET', { 'http.request.method': 'GET', 'http.route': '/users/:id' });
inferSpanDataFromOtelAttributes(spanJSON, 1);
expect(spanJSON.name).toBe('GET /users/:id');
expect(spanJSON.attributes?.['sentry.source']).toBe('route');
});
it('infers name from url.full when no http.route and sets source to url', () => {
const spanJSON = makeSpanJSON('GET', { 'http.request.method': 'GET', 'url.full': 'http://example.com/api' });
inferSpanDataFromOtelAttributes(spanJSON, 2);
expect(spanJSON.name).toBe('GET http://example.com/api');
expect(spanJSON.attributes?.['sentry.source']).toBe('url');
});
it('does not overwrite sentry.op if already set', () => {
const spanJSON = makeSpanJSON('GET', { 'http.request.method': 'GET', 'sentry.op': 'http.client.custom' });
inferSpanDataFromOtelAttributes(spanJSON, 2);
expect(spanJSON.attributes?.['sentry.op']).toBe('http.client.custom');
});
it('restores custom span name from sentry.custom_span_name', () => {
const spanJSON = makeSpanJSON('overwritten-by-otel', {
'http.request.method': 'GET',
'sentry.custom_span_name': 'my-custom-name',
'sentry.source': 'custom',
'http.route': '/users/:id',
});
inferSpanDataFromOtelAttributes(spanJSON, 1);
expect(spanJSON.name).toBe('my-custom-name');
});
it('does not overwrite name when sentry.source is custom', () => {
const spanJSON = makeSpanJSON('my-name', {
'http.request.method': 'GET',
'sentry.source': 'custom',
'http.route': '/users/:id',
});
inferSpanDataFromOtelAttributes(spanJSON, 1);
expect(spanJSON.name).toBe('my-name');
});
it('supports legacy http.method attribute', () => {
const spanJSON = makeSpanJSON('GET', { 'http.method': 'GET' });
inferSpanDataFromOtelAttributes(spanJSON, 2);
expect(spanJSON.attributes?.['sentry.op']).toBe('http.client');
});
});
describe('db spans', () => {
it('infers db op', () => {
const spanJSON = makeSpanJSON('redis', { 'db.system': 'redis' });
inferSpanDataFromOtelAttributes(spanJSON);
expect(spanJSON.attributes?.['sentry.op']).toBe('db');
});
it('sets name from db.statement', () => {
const spanJSON = makeSpanJSON('mysql', { 'db.system': 'mysql', 'db.statement': 'SELECT * FROM users' });
inferSpanDataFromOtelAttributes(spanJSON);
expect(spanJSON.name).toBe('SELECT * FROM users');
expect(spanJSON.attributes?.['sentry.source']).toBe('task');
});
it('skips db inference for cache spans', () => {
const spanJSON = makeSpanJSON('cache-get', { 'db.system': 'redis', 'sentry.op': 'cache.get_item' });
inferSpanDataFromOtelAttributes(spanJSON);
expect(spanJSON.attributes?.['sentry.op']).toBe('cache.get_item');
expect(spanJSON.name).toBe('cache-get');
});
it('restores custom span name from sentry.custom_span_name', () => {
const spanJSON = makeSpanJSON('overwritten', {
'db.system': 'mysql',
'db.statement': 'SELECT 1',
'sentry.custom_span_name': 'my-db-span',
'sentry.source': 'custom',
});
inferSpanDataFromOtelAttributes(spanJSON);
expect(spanJSON.name).toBe('my-db-span');
});
});
describe('other span types', () => {
it('infers rpc op', () => {
const spanJSON = makeSpanJSON('grpc', { 'rpc.service': 'UserService' });
inferSpanDataFromOtelAttributes(spanJSON);
expect(spanJSON.attributes?.['sentry.op']).toBe('rpc');
});
it('infers message op', () => {
const spanJSON = makeSpanJSON('kafka', { 'messaging.system': 'kafka' });
inferSpanDataFromOtelAttributes(spanJSON);
expect(spanJSON.attributes?.['sentry.op']).toBe('message');
});
it('infers faas op from trigger', () => {
const spanJSON = makeSpanJSON('lambda', { 'faas.trigger': 'http' });
inferSpanDataFromOtelAttributes(spanJSON);
expect(spanJSON.attributes?.['sentry.op']).toBe('http');
});
});
it('does nothing when attributes are missing', () => {
const spanJSON = makeSpanJSON('test', undefined as unknown as Record<string, unknown>);
spanJSON.attributes = undefined;
inferSpanDataFromOtelAttributes(spanJSON, 2);
expect(spanJSON.attributes).toBeUndefined();
});
it('does nothing for spans without recognizable attributes', () => {
const spanJSON = makeSpanJSON('test', { 'custom.attr': 'value' });
inferSpanDataFromOtelAttributes(spanJSON);
expect(spanJSON.attributes?.['sentry.op']).toBeUndefined();
expect(spanJSON.name).toBe('test');
});
});