-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathspanstreaming.test.ts
More file actions
167 lines (141 loc) · 5.12 KB
/
spanstreaming.test.ts
File metadata and controls
167 lines (141 loc) · 5.12 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
import * as SentryCore from '@sentry/core';
import { debug } from '@sentry/core';
import { 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', () => {
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('logs a warning if traceLifecycle is not set to "stream"', () => {
const debugSpy = vi.spyOn(debug, 'warn').mockImplementation(() => {});
const client = new BrowserClient({
...getDefaultBrowserClientOptions(),
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 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()],
});
SentryCore.setCurrentClient(client);
client.init();
const span = new SentryCore.SentrySpan({ name: 'test' });
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),
},
'sentry.segment.id': {
type: 'string',
value: span.spanContext().spanId,
},
'sentry.segment.name': {
type: 'string',
value: 'test',
},
},
});
});
it('flushes the trace when the segment span ends', () => {
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);
expect(mockSpanBufferInstance.flush).toHaveBeenCalledWith(span.spanContext().traceId);
});
});