-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathspanstreaming.test.ts
More file actions
203 lines (170 loc) · 6.29 KB
/
spanstreaming.test.ts
File metadata and controls
203 lines (170 loc) · 6.29 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
import * as SentryCore from '@sentry/core';
import { debug, SEMANTIC_ATTRIBUTE_SENTRY_SDK_INTEGRATIONS } from '@sentry/core';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { BrowserClient, spanStreamingIntegration } from '../../src';
import { getDefaultBrowserClientOptions } from '../helper/browser-client-options';
// Mock SpanBuffer as a class that can be instantiated
const mockSpanBufferInstance = vi.hoisted(() => ({
flush: vi.fn(),
add: vi.fn(),
drain: vi.fn(),
}));
const MockSpanBuffer = vi.hoisted(() => {
return vi.fn(() => mockSpanBufferInstance);
});
vi.mock('@sentry/core', async () => {
const original = await vi.importActual('@sentry/core');
return {
...original,
SpanBuffer: MockSpanBuffer,
};
});
describe('spanStreamingIntegration', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('has the correct hooks', () => {
const integration = spanStreamingIntegration();
expect(integration.name).toBe('SpanStreaming');
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(integration.beforeSetup).toBeDefined();
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(integration.setup).toBeDefined();
});
it('sets traceLifecycle to "stream" if not set', () => {
const client = new BrowserClient({
...getDefaultBrowserClientOptions(),
dsn: 'https://username@domain/123',
integrations: [spanStreamingIntegration()],
});
SentryCore.setCurrentClient(client);
client.init();
expect(client.getOptions().traceLifecycle).toBe('stream');
});
it.each(['static', 'somethingElse'])(
'logs a warning if traceLifecycle is not set to "stream" but to %s',
traceLifecycle => {
const debugSpy = vi.spyOn(debug, 'warn').mockImplementation(() => {});
const client = new BrowserClient({
...getDefaultBrowserClientOptions(),
dsn: 'https://username@domain/123',
integrations: [spanStreamingIntegration()],
// @ts-expect-error - we want to test the warning for invalid traceLifecycle values
traceLifecycle,
});
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 BrowserClient({
...getDefaultBrowserClientOptions(),
dsn: 'https://username@domain/123',
integrations: [spanStreamingIntegration()],
traceLifecycle: 'stream',
beforeSendSpan: (span: Span) => 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('does nothing if traceLifecycle set to "stream"', () => {
const client = new BrowserClient({
...getDefaultBrowserClientOptions(),
dsn: 'https://username@domain/123',
integrations: [spanStreamingIntegration()],
traceLifecycle: 'stream',
});
SentryCore.setCurrentClient(client);
client.init();
expect(client.getOptions().traceLifecycle).toBe('stream');
});
it('enqueues a span into the buffer when the span ends', () => {
const client = new BrowserClient({
...getDefaultBrowserClientOptions(),
dsn: 'https://username@domain/123',
integrations: [spanStreamingIntegration()],
tracesSampleRate: 1,
});
SentryCore.setCurrentClient(client);
client.init();
const span = new SentryCore.SentrySpan({ name: 'test', sampled: true });
client.emit('afterSpanEnd', span);
expect(mockSpanBufferInstance.add).toHaveBeenCalledWith({
_segmentSpan: span,
trace_id: span.spanContext().traceId,
span_id: span.spanContext().spanId,
end_timestamp: expect.any(Number),
is_segment: true,
name: 'test',
start_timestamp: expect.any(Number),
status: 'ok',
attributes: {
'sentry.origin': {
type: 'string',
value: 'manual',
},
'sentry.sdk.name': {
type: 'string',
value: 'sentry.javascript.browser',
},
'sentry.sdk.version': {
type: 'string',
value: expect.any(String),
},
[SEMANTIC_ATTRIBUTE_SENTRY_SDK_INTEGRATIONS]: {
type: 'array',
value: ['SpanStreaming'],
},
'sentry.segment.id': {
type: 'string',
value: span.spanContext().spanId,
},
'sentry.segment.name': {
type: 'string',
value: 'test',
},
},
});
});
it('does not enqueue a span into the buffer when the span is not sampled', () => {
const client = new BrowserClient({
...getDefaultBrowserClientOptions(),
dsn: 'https://username@domain/123',
integrations: [spanStreamingIntegration()],
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();
expect(mockSpanBufferInstance.flush).not.toHaveBeenCalled();
});
it('flushes the trace when the segment span ends after a delay for close to finished child spans', () => {
vi.useFakeTimers();
const client = new BrowserClient({
...getDefaultBrowserClientOptions(),
dsn: 'https://username@domain/123',
integrations: [spanStreamingIntegration()],
traceLifecycle: 'stream',
});
SentryCore.setCurrentClient(client);
client.init();
const span = new SentryCore.SentrySpan({ name: 'test' });
client.emit('afterSegmentSpanEnd', span);
vi.advanceTimersByTime(500);
expect(mockSpanBufferInstance.flush).toHaveBeenCalledWith(span.spanContext().traceId);
vi.useRealTimers();
});
});