-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathspanStreaming.test.ts
More file actions
135 lines (111 loc) · 4.24 KB
/
spanStreaming.test.ts
File metadata and controls
135 lines (111 loc) · 4.24 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
import * as SentryCore from '../../src';
import { debug } from '../../src';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { spanStreamingIntegration } from '../../src/integrations/spanStreaming';
import { TestClient, getDefaultTestClientOptions } from '../mocks/client';
const mockSpanBufferInstance = vi.hoisted(() => ({
flush: vi.fn(),
add: vi.fn(),
drain: vi.fn(),
}));
const MockSpanBuffer = vi.hoisted(() => {
return vi.fn(() => mockSpanBufferInstance);
});
vi.mock('../../src/tracing/spans/spanBuffer', async () => {
const original = await vi.importActual('../../src/tracing/spans/spanBuffer');
return {
...original,
SpanBuffer: MockSpanBuffer,
};
});
describe('spanStreamingIntegration (core)', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('has the correct name and setup hook', () => {
const integration = spanStreamingIntegration();
expect(integration.name).toBe('SpanStreaming');
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(integration.setup).toBeDefined();
});
it('logs a warning if traceLifecycle is not set to "stream"', () => {
const debugSpy = vi.spyOn(debug, 'warn').mockImplementation(() => {});
const client = new TestClient({
...getDefaultTestClientOptions(),
dsn: 'https://username@domain/123',
integrations: [spanStreamingIntegration()],
traceLifecycle: 'static',
});
SentryCore.setCurrentClient(client);
client.init();
expect(debugSpy).toHaveBeenCalledWith(
'SpanStreaming integration requires `traceLifecycle` to be set to "stream"! Falling back to static trace lifecycle.',
);
debugSpy.mockRestore();
expect(client.getOptions().traceLifecycle).toBe('static');
});
it('falls back to static trace lifecycle if beforeSendSpan is not compatible with span streaming', () => {
const debugSpy = vi.spyOn(debug, 'warn').mockImplementation(() => {});
const client = new TestClient({
...getDefaultTestClientOptions(),
dsn: 'https://username@domain/123',
integrations: [spanStreamingIntegration()],
traceLifecycle: 'stream',
beforeSendSpan: (span: SentryCore.SpanJSON) => span,
});
SentryCore.setCurrentClient(client);
client.init();
expect(debugSpy).toHaveBeenCalledWith(
'SpanStreaming integration requires a beforeSendSpan callback using `withStreamedSpan`! Falling back to static trace lifecycle.',
);
debugSpy.mockRestore();
expect(client.getOptions().traceLifecycle).toBe('static');
});
it('sets up buffer when traceLifecycle is "stream"', () => {
const client = new TestClient({
...getDefaultTestClientOptions(),
dsn: 'https://username@domain/123',
integrations: [spanStreamingIntegration()],
traceLifecycle: 'stream',
});
SentryCore.setCurrentClient(client);
client.init();
expect(MockSpanBuffer).toHaveBeenCalledWith(client);
expect(client.getOptions().traceLifecycle).toBe('stream');
});
it('enqueues a span into the buffer when the span ends', () => {
const client = new TestClient({
...getDefaultTestClientOptions(),
dsn: 'https://username@domain/123',
integrations: [spanStreamingIntegration()],
traceLifecycle: 'stream',
tracesSampleRate: 1,
});
SentryCore.setCurrentClient(client);
client.init();
const span = new SentryCore.SentrySpan({ name: 'test', sampled: true });
client.emit('afterSpanEnd', span);
expect(mockSpanBufferInstance.add).toHaveBeenCalledWith(
expect.objectContaining({
_segmentSpan: span,
trace_id: span.spanContext().traceId,
span_id: span.spanContext().spanId,
name: 'test',
}),
);
});
it('does not enqueue a span into the buffer when the span is not sampled', () => {
const client = new TestClient({
...getDefaultTestClientOptions(),
dsn: 'https://username@domain/123',
integrations: [spanStreamingIntegration()],
traceLifecycle: 'stream',
tracesSampleRate: 1,
});
SentryCore.setCurrentClient(client);
client.init();
const span = new SentryCore.SentrySpan({ name: 'test', sampled: false });
client.emit('afterSpanEnd', span);
expect(mockSpanBufferInstance.add).not.toHaveBeenCalled();
});
});