-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathindex.test.ts
281 lines (238 loc) · 9.35 KB
/
index.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
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
import { describe, expect, it, vi } from 'vitest';
import {
_INTERNAL_flushLogsBuffer,
_INTERNAL_getLogBuffer,
_INTERNAL_captureLog,
logAttributeToSerializedLogAttribute,
} from '../../../src/logs';
import { TestClient, getDefaultTestClientOptions } from '../../mocks/client';
import * as loggerModule from '../../../src/utils-hoist/logger';
import { Scope } from '../../../src';
import type { Log } from '../../../src/types-hoist/log';
const PUBLIC_DSN = 'https://username@domain/123';
describe('logAttributeToSerializedLogAttribute', () => {
it('serializes number values', () => {
const result = logAttributeToSerializedLogAttribute('count', 42);
expect(result).toEqual({
key: 'count',
value: { doubleValue: 42 },
});
});
it('serializes boolean values', () => {
const result = logAttributeToSerializedLogAttribute('enabled', true);
expect(result).toEqual({
key: 'enabled',
value: { boolValue: true },
});
});
it('serializes string values', () => {
const result = logAttributeToSerializedLogAttribute('username', 'john_doe');
expect(result).toEqual({
key: 'username',
value: { stringValue: 'john_doe' },
});
});
it('serializes object values as JSON strings', () => {
const obj = { name: 'John', age: 30 };
const result = logAttributeToSerializedLogAttribute('user', obj);
expect(result).toEqual({
key: 'user',
value: { stringValue: JSON.stringify(obj) },
});
});
it('serializes array values as JSON strings', () => {
const array = [1, 2, 3, 'test'];
const result = logAttributeToSerializedLogAttribute('items', array);
expect(result).toEqual({
key: 'items',
value: { stringValue: JSON.stringify(array) },
});
});
it('serializes undefined values as empty strings', () => {
const result = logAttributeToSerializedLogAttribute('missing', undefined);
expect(result).toEqual({
key: 'missing',
value: { stringValue: '' },
});
});
it('serializes null values as JSON strings', () => {
const result = logAttributeToSerializedLogAttribute('empty', null);
expect(result).toEqual({
key: 'empty',
value: { stringValue: 'null' },
});
});
});
describe('_INTERNAL_captureLog', () => {
it('captures and sends logs', () => {
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableLogs: true } });
const client = new TestClient(options);
_INTERNAL_captureLog({ level: 'info', message: 'test log message' }, client, undefined);
expect(_INTERNAL_getLogBuffer(client)).toHaveLength(1);
expect(_INTERNAL_getLogBuffer(client)?.[0]).toEqual(
expect.objectContaining({
severityText: 'info',
body: {
stringValue: 'test log message',
},
timeUnixNano: expect.any(String),
}),
);
});
it('does not capture logs when enableLogs experiment is not enabled', () => {
const logWarnSpy = vi.spyOn(loggerModule.logger, 'warn').mockImplementation(() => undefined);
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN });
const client = new TestClient(options);
_INTERNAL_captureLog({ level: 'info', message: 'test log message' }, client, undefined);
expect(logWarnSpy).toHaveBeenCalledWith('logging option not enabled, log will not be captured.');
expect(_INTERNAL_getLogBuffer(client)).toBeUndefined();
logWarnSpy.mockRestore();
});
it('includes trace context when available', () => {
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableLogs: true } });
const client = new TestClient(options);
const scope = new Scope();
scope.setPropagationContext({
traceId: '3d9355f71e9c444b81161599adac6e29',
sampleRand: 1,
});
_INTERNAL_captureLog({ level: 'error', message: 'test log with trace' }, client, scope);
expect(_INTERNAL_getLogBuffer(client)?.[0]).toEqual(
expect.objectContaining({
traceId: '3d9355f71e9c444b81161599adac6e29',
}),
);
});
it('includes release and environment in log attributes when available', () => {
const options = getDefaultTestClientOptions({
dsn: PUBLIC_DSN,
_experiments: { enableLogs: true },
release: '1.0.0',
environment: 'test',
});
const client = new TestClient(options);
_INTERNAL_captureLog({ level: 'info', message: 'test log with metadata' }, client, undefined);
const logAttributes = _INTERNAL_getLogBuffer(client)?.[0]?.attributes;
expect(logAttributes).toEqual(
expect.arrayContaining([
expect.objectContaining({ key: 'release', value: { stringValue: '1.0.0' } }),
expect.objectContaining({ key: 'environment', value: { stringValue: 'test' } }),
]),
);
});
it('includes custom attributes in log', () => {
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableLogs: true } });
const client = new TestClient(options);
_INTERNAL_captureLog(
{
level: 'info',
message: 'test log with custom attributes',
attributes: { userId: '123', component: 'auth' },
},
client,
undefined,
);
const logAttributes = _INTERNAL_getLogBuffer(client)?.[0]?.attributes;
expect(logAttributes).toEqual(
expect.arrayContaining([
expect.objectContaining({ key: 'userId', value: { stringValue: '123' } }),
expect.objectContaining({ key: 'component', value: { stringValue: 'auth' } }),
]),
);
});
it('flushes logs buffer when it reaches max size', () => {
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableLogs: true } });
const client = new TestClient(options);
// Fill the buffer to max size (100 is the MAX_LOG_BUFFER_SIZE constant in client.ts)
for (let i = 0; i < 100; i++) {
_INTERNAL_captureLog({ level: 'info', message: `log message ${i}` }, client, undefined);
}
expect(_INTERNAL_getLogBuffer(client)).toHaveLength(100);
// Add one more to trigger flush
_INTERNAL_captureLog({ level: 'info', message: 'trigger flush' }, client, undefined);
expect(_INTERNAL_getLogBuffer(client)).toEqual([]);
});
it('does not flush logs buffer when it is empty', () => {
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableLogs: true } });
const client = new TestClient(options);
const mockSendEnvelope = vi.spyOn(client as any, 'sendEnvelope').mockImplementation(() => {});
_INTERNAL_flushLogsBuffer(client);
expect(mockSendEnvelope).not.toHaveBeenCalled();
});
it('processes logs through beforeSendLog when provided', () => {
const beforeSendLog = vi.fn().mockImplementation(log => ({
...log,
message: `Modified: ${log.message}`,
attributes: { ...log.attributes, processed: true },
}));
const options = getDefaultTestClientOptions({
dsn: PUBLIC_DSN,
_experiments: { enableLogs: true, beforeSendLog },
});
const client = new TestClient(options);
_INTERNAL_captureLog(
{
level: 'info',
message: 'original message',
attributes: { original: true },
},
client,
undefined,
);
expect(beforeSendLog).toHaveBeenCalledWith({
level: 'info',
message: 'original message',
attributes: { original: true },
});
const logBuffer = _INTERNAL_getLogBuffer(client);
expect(logBuffer).toBeDefined();
expect(logBuffer?.[0]).toEqual(
expect.objectContaining({
body: {
stringValue: 'Modified: original message',
},
attributes: expect.arrayContaining([
expect.objectContaining({ key: 'processed', value: { boolValue: true } }),
expect.objectContaining({ key: 'original', value: { boolValue: true } }),
]),
}),
);
});
it('drops logs when beforeSendLog returns null', () => {
const beforeSendLog = vi.fn().mockReturnValue(null);
const recordDroppedEventSpy = vi.spyOn(TestClient.prototype, 'recordDroppedEvent');
const loggerWarnSpy = vi.spyOn(loggerModule.logger, 'warn').mockImplementation(() => undefined);
const options = getDefaultTestClientOptions({
dsn: PUBLIC_DSN,
_experiments: { enableLogs: true, beforeSendLog },
});
const client = new TestClient(options);
_INTERNAL_captureLog(
{
level: 'info',
message: 'test message',
},
client,
undefined,
);
expect(beforeSendLog).toHaveBeenCalled();
expect(recordDroppedEventSpy).toHaveBeenCalledWith('before_send', 'log_item', 1);
expect(loggerWarnSpy).toHaveBeenCalledWith('beforeSendLog returned null, log will not be captured.');
expect(_INTERNAL_getLogBuffer(client)).toBeUndefined();
recordDroppedEventSpy.mockRestore();
loggerWarnSpy.mockRestore();
});
it('emits beforeCaptureLog and afterCaptureLog events', () => {
const beforeCaptureLogSpy = vi.spyOn(TestClient.prototype, 'emit');
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableLogs: true } });
const client = new TestClient(options);
const log: Log = {
level: 'info',
message: 'test message',
};
_INTERNAL_captureLog(log, client, undefined);
expect(beforeCaptureLogSpy).toHaveBeenCalledWith('beforeCaptureLog', log);
expect(beforeCaptureLogSpy).toHaveBeenCalledWith('afterCaptureLog', log);
beforeCaptureLogSpy.mockRestore();
});
});