-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathserver-runtime-client.test.ts
More file actions
409 lines (331 loc) · 12.9 KB
/
server-runtime-client.test.ts
File metadata and controls
409 lines (331 loc) · 12.9 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
import { describe, expect, it, test, vi } from 'vitest';
import { applySdkMetadata, createTransport, Scope } from '../../src';
import { _INTERNAL_captureMetric, _INTERNAL_getMetricBuffer } from '../../src/metrics/internal';
import type { ServerRuntimeClientOptions } from '../../src/server-runtime-client';
import { ServerRuntimeClient } from '../../src/server-runtime-client';
import type { Event, EventHint } from '../../src/types-hoist/event';
const PUBLIC_DSN = 'https://username@domain/123';
function getDefaultClientOptions(options: Partial<ServerRuntimeClientOptions> = {}): ServerRuntimeClientOptions {
return {
integrations: [],
transport: () => createTransport({ recordDroppedEvent: () => undefined }, _ => Promise.resolve({})),
stackParser: () => [],
...options,
};
}
describe('ServerRuntimeClient', () => {
let client: ServerRuntimeClient;
const currentScope = new Scope();
const isolationScope = new Scope();
describe('_prepareEvent', () => {
test('adds platform to event', () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN });
const client = new ServerRuntimeClient({ ...options, platform: 'blargh' });
const event: Event = {};
const hint: EventHint = {};
client['_prepareEvent'](event, hint, currentScope, isolationScope);
expect(event.platform).toEqual('blargh');
});
test('adds server_name to event', () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN });
const client = new ServerRuntimeClient({ ...options, serverName: 'server' });
const event: Event = {};
const hint: EventHint = {};
client['_prepareEvent'](event, hint, currentScope, isolationScope);
expect(event.server_name).toEqual('server');
});
test('adds runtime context to event', () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN });
const client = new ServerRuntimeClient({ ...options, runtime: { name: 'edge' } });
const event: Event = {};
const hint: EventHint = {};
client['_prepareEvent'](event, hint, currentScope, isolationScope);
expect(event.contexts?.runtime).toEqual({
name: 'edge',
});
});
test("doesn't clobber existing runtime data", () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN });
const client = new ServerRuntimeClient({ ...options, runtime: { name: 'edge' } });
const event: Event = { contexts: { runtime: { name: 'foo', version: '1.2.3' } } };
const hint: EventHint = {};
client['_prepareEvent'](event, hint, currentScope, isolationScope);
expect(event.contexts?.runtime).toEqual({ name: 'foo', version: '1.2.3' });
expect(event.contexts?.runtime).not.toEqual({ name: 'edge' });
});
});
describe('captureCheckIn', () => {
it('sends a checkIn envelope', () => {
const options = getDefaultClientOptions({
dsn: PUBLIC_DSN,
serverName: 'bar',
release: '1.0.0',
environment: 'dev',
});
client = new ServerRuntimeClient(options);
const sendEnvelopeSpy = vi.spyOn(client, 'sendEnvelope');
const id = client.captureCheckIn(
{ monitorSlug: 'foo', status: 'in_progress' },
{
schedule: {
type: 'crontab',
value: '0 * * * *',
},
checkinMargin: 2,
maxRuntime: 12333,
timezone: 'Canada/Eastern',
failureIssueThreshold: 2,
recoveryThreshold: 3,
},
);
expect(sendEnvelopeSpy).toHaveBeenCalledTimes(1);
expect(sendEnvelopeSpy).toHaveBeenCalledWith([
expect.any(Object),
[
[
expect.any(Object),
{
check_in_id: id,
monitor_slug: 'foo',
status: 'in_progress',
release: '1.0.0',
environment: 'dev',
monitor_config: {
schedule: {
type: 'crontab',
value: '0 * * * *',
},
checkin_margin: 2,
max_runtime: 12333,
timezone: 'Canada/Eastern',
failure_issue_threshold: 2,
recovery_threshold: 3,
},
},
],
],
]);
client.captureCheckIn({ monitorSlug: 'foo', status: 'ok', duration: 1222, checkInId: id });
expect(sendEnvelopeSpy).toHaveBeenCalledTimes(2);
expect(sendEnvelopeSpy).toHaveBeenCalledWith([
expect.any(Object),
[
[
expect.any(Object),
{
check_in_id: id,
monitor_slug: 'foo',
duration: 1222,
status: 'ok',
release: '1.0.0',
environment: 'dev',
},
],
],
]);
});
it('does not send a checkIn envelope if disabled', () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN, serverName: 'bar', enabled: false });
client = new ServerRuntimeClient(options);
const sendEnvelopeSpy = vi.spyOn(client, 'sendEnvelope');
client.captureCheckIn({ monitorSlug: 'foo', status: 'in_progress' });
expect(sendEnvelopeSpy).toHaveBeenCalledTimes(0);
});
});
describe('captureException', () => {
it('sends an exception event with level error', () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN });
client = new ServerRuntimeClient(options);
const sendEnvelopeSpy = vi.spyOn(client, 'sendEnvelope');
client.captureException(new Error('foo'));
expect(sendEnvelopeSpy).toHaveBeenCalledTimes(1);
expect(sendEnvelopeSpy).toHaveBeenCalledWith([
expect.any(Object),
[
[
expect.any(Object),
expect.objectContaining({
level: 'error',
}),
],
],
]);
});
});
describe('captureMessage', () => {
it('sends a message event with level info', () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN });
client = new ServerRuntimeClient(options);
const sendEnvelopeSpy = vi.spyOn(client, 'sendEnvelope');
client.captureMessage('foo');
expect(sendEnvelopeSpy).toHaveBeenCalledTimes(1);
expect(sendEnvelopeSpy).toHaveBeenCalledWith([
expect.any(Object),
[
[
expect.any(Object),
expect.objectContaining({
level: 'info',
}),
],
],
]);
});
});
describe('user-agent header', () => {
it('sends user-agent header with SDK name and version', () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN });
// this is done in all `init` functions of the respective SDKs:
applySdkMetadata(options, 'core');
client = new ServerRuntimeClient(options);
expect(client.getOptions().transportOptions?.headers).toEqual({
'user-agent': 'sentry.javascript.core/0.0.0-unknown.0',
});
});
it('prefers user-passed headers (including user-agent)', () => {
const options = getDefaultClientOptions({
dsn: PUBLIC_DSN,
transportOptions: { headers: { 'x-custom-header': 'custom-value', 'user-agent': 'custom-user-agent' } },
});
applySdkMetadata(options, 'core');
client = new ServerRuntimeClient(options);
expect(client.getOptions().transportOptions?.headers).toEqual({
'user-agent': 'custom-user-agent',
'x-custom-header': 'custom-value',
});
});
});
describe('metrics processing', () => {
it('adds server.address attribute to metrics when serverName is set', () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN, serverName: 'my-server.example.com' });
client = new ServerRuntimeClient(options);
const scope = new Scope();
scope.setClient(client);
_INTERNAL_captureMetric({ type: 'counter', name: 'test.metric', value: 1 }, { scope });
const metricAttributes = _INTERNAL_getMetricBuffer(client)?.[0]?.attributes;
expect(metricAttributes).toEqual(
expect.objectContaining({
'server.address': {
value: 'my-server.example.com',
type: 'string',
},
}),
);
});
it('does not add server.address attribute when serverName is not set', () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN });
client = new ServerRuntimeClient(options);
const scope = new Scope();
scope.setClient(client);
_INTERNAL_captureMetric({ type: 'counter', name: 'test.metric', value: 1 }, { scope });
const metricAttributes = _INTERNAL_getMetricBuffer(client)?.[0]?.attributes;
expect(metricAttributes).not.toEqual(
expect.objectContaining({
'server.address': expect.anything(),
}),
);
});
it('does not overwrite existing server.address attribute', () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN, serverName: 'my-server.example.com' });
client = new ServerRuntimeClient(options);
const scope = new Scope();
scope.setClient(client);
_INTERNAL_captureMetric(
{
type: 'counter',
name: 'test.metric',
value: 1,
attributes: { 'server.address': 'existing-server.example.com' },
},
{ scope },
);
const metricAttributes = _INTERNAL_getMetricBuffer(client)?.[0]?.attributes;
expect(metricAttributes).toEqual(
expect.objectContaining({
'server.address': {
value: 'existing-server.example.com',
type: 'string',
},
}),
);
});
});
describe('dispose', () => {
it('resets _promiseBuffer to a new empty buffer instead of undefined', () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN });
client = new ServerRuntimeClient(options);
// Access the private _promiseBuffer before dispose
const originalBuffer = client['_promiseBuffer'];
expect(originalBuffer).toBeDefined();
client.dispose();
// After dispose, _promiseBuffer should still be defined (not undefined)
const bufferAfterDispose = client['_promiseBuffer'];
expect(bufferAfterDispose).toBeDefined();
expect(bufferAfterDispose).not.toBe(originalBuffer);
// Verify it's a fresh buffer with no pending items
expect(bufferAfterDispose.$).toEqual([]);
});
it('calls registered cleanup callbacks on dispose', () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN });
client = new ServerRuntimeClient(options);
const cleanup1 = vi.fn();
const cleanup2 = vi.fn();
const cleanup3 = vi.fn();
client.registerCleanup(cleanup1);
client.registerCleanup(cleanup2);
client.registerCleanup(cleanup3);
expect(cleanup1).not.toHaveBeenCalled();
expect(cleanup2).not.toHaveBeenCalled();
expect(cleanup3).not.toHaveBeenCalled();
client.dispose();
expect(cleanup1).toHaveBeenCalledTimes(1);
expect(cleanup2).toHaveBeenCalledTimes(1);
expect(cleanup3).toHaveBeenCalledTimes(1);
});
it('clears cleanup callbacks after dispose', () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN });
client = new ServerRuntimeClient(options);
const cleanup = vi.fn();
client.registerCleanup(cleanup);
client.dispose();
expect(cleanup).toHaveBeenCalledTimes(1);
// Calling dispose again should not call cleanup again
client.dispose();
expect(cleanup).toHaveBeenCalledTimes(1);
});
it('continues to call other cleanup callbacks if one throws', () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN });
client = new ServerRuntimeClient(options);
const cleanup1 = vi.fn();
const throwingCleanup = vi.fn(() => {
throw new Error('cleanup error');
});
const cleanup2 = vi.fn();
client.registerCleanup(cleanup1);
client.registerCleanup(throwingCleanup);
client.registerCleanup(cleanup2);
expect(() => client.dispose()).not.toThrow();
expect(cleanup1).toHaveBeenCalledTimes(1);
expect(throwingCleanup).toHaveBeenCalledTimes(1);
expect(cleanup2).toHaveBeenCalledTimes(1);
});
});
describe('registerCleanup', () => {
it('accepts cleanup functions', () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN });
client = new ServerRuntimeClient(options);
const cleanup = vi.fn();
expect(() => client.registerCleanup(cleanup)).not.toThrow();
});
it('can register multiple cleanup functions', () => {
const options = getDefaultClientOptions({ dsn: PUBLIC_DSN });
client = new ServerRuntimeClient(options);
const cleanups = Array.from({ length: 10 }, () => vi.fn());
cleanups.forEach(cleanup => client.registerCleanup(cleanup));
client.dispose();
cleanups.forEach(cleanup => {
expect(cleanup).toHaveBeenCalledTimes(1);
});
});
});
});