-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathsetupEventContextTrace.test.ts
109 lines (92 loc) · 2.99 KB
/
setupEventContextTrace.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
import type { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
import { captureException, setCurrentClient } from '@sentry/core';
import { describe, afterEach, beforeEach, expect, it, vi, afterAll } from 'vitest';
import { setupEventContextTrace } from '../../src/setupEventContextTrace';
import type { TestClientInterface } from '../helpers/TestClient';
import { TestClient, getDefaultTestClientOptions } from '../helpers/TestClient';
import { setupOtel } from '../helpers/initOtel';
import { cleanupOtel } from '../helpers/mockSdkInit';
const PUBLIC_DSN = 'https://username@domain/123';
describe('setupEventContextTrace', () => {
const beforeSend = vi.fn(() => null);
let client: TestClientInterface;
let provider: BasicTracerProvider | undefined;
beforeEach(() => {
client = new TestClient(
getDefaultTestClientOptions({
sampleRate: 1,
tracesSampleRate: 1,
beforeSend,
debug: true,
dsn: PUBLIC_DSN,
}),
);
setCurrentClient(client);
client.init();
setupEventContextTrace(client);
provider = setupOtel(client);
});
afterEach(() => {
beforeSend.mockReset();
cleanupOtel(provider);
});
afterAll(() => {
vi.clearAllMocks();
});
it('works with no active span', async () => {
const error = new Error('test');
captureException(error);
await client.flush();
expect(beforeSend).toHaveBeenCalledTimes(1);
expect(beforeSend).toHaveBeenCalledWith(
expect.objectContaining({
contexts: expect.objectContaining({
trace: {
span_id: expect.stringMatching(/[a-f0-9]{16}/),
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
},
}),
}),
expect.objectContaining({
event_id: expect.any(String),
originalException: error,
syntheticException: expect.any(Error),
}),
);
});
it('works with active span', async () => {
const error = new Error('test');
let outerId: string | undefined;
let innerId: string | undefined;
let traceId: string | undefined;
client.tracer.startActiveSpan('outer', outerSpan => {
outerId = outerSpan.spanContext().spanId;
traceId = outerSpan.spanContext().traceId;
client.tracer.startActiveSpan('inner', innerSpan => {
innerId = innerSpan.spanContext().spanId;
captureException(error);
});
});
await client.flush();
expect(outerId).toBeDefined();
expect(innerId).toBeDefined();
expect(traceId).toBeDefined();
expect(beforeSend).toHaveBeenCalledTimes(1);
expect(beforeSend).toHaveBeenCalledWith(
expect.objectContaining({
contexts: expect.objectContaining({
trace: {
span_id: innerId,
parent_span_id: outerId,
trace_id: traceId,
},
}),
}),
expect.objectContaining({
event_id: expect.any(String),
originalException: error,
syntheticException: expect.any(Error),
}),
);
});
});