@loggerjs/node 0.5.6 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -26
- package/dist/bullmq-integration.cjs +2 -145
- package/dist/bullmq-integration.cjs.map +1 -1
- package/dist/bullmq-integration.js +1 -144
- package/dist/bullmq-integration.js.map +1 -1
- package/dist/chunks/node-destination-DvP8jBvm.cjs +254 -0
- package/dist/chunks/node-destination-DvP8jBvm.cjs.map +1 -0
- package/dist/chunks/node-destination-YnpYbctz.js +243 -0
- package/dist/chunks/node-destination-YnpYbctz.js.map +1 -0
- package/dist/context.cjs +1 -1
- package/dist/context.js +1 -1
- package/dist/diagnostics-channel-integration.cjs +1 -1
- package/dist/file-transport.cjs +3 -231
- package/dist/file-transport.cjs.map +1 -1
- package/dist/file-transport.js +3 -231
- package/dist/file-transport.js.map +1 -1
- package/dist/http-transport.cjs +25 -4
- package/dist/http-transport.cjs.map +1 -1
- package/dist/http-transport.d.ts +15 -0
- package/dist/http-transport.d.ts.map +1 -1
- package/dist/http-transport.js +25 -4
- package/dist/http-transport.js.map +1 -1
- package/dist/index.cjs +54 -2355
- package/dist/index.d.ts +211 -19
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +25 -2327
- package/dist/internal-types.d.ts +1 -0
- package/dist/internal-types.d.ts.map +1 -1
- package/dist/logger-diagnostics.cjs +4 -5
- package/dist/logger-diagnostics.cjs.map +1 -1
- package/dist/logger-diagnostics.d.ts +1 -1
- package/dist/logger-diagnostics.d.ts.map +1 -1
- package/dist/logger-diagnostics.js +1 -1
- package/dist/logger-diagnostics.js.map +1 -1
- package/dist/nest-integration.cjs +2 -127
- package/dist/nest-integration.cjs.map +1 -1
- package/dist/nest-integration.js +1 -126
- package/dist/nest-integration.js.map +1 -1
- package/dist/node-destination.d.ts.map +1 -1
- package/dist/payload-transforms.cjs +2 -2
- package/dist/payload-transforms.cjs.map +1 -1
- package/dist/payload-transforms.d.ts.map +1 -1
- package/dist/payload-transforms.js +1 -1
- package/dist/payload-transforms.js.map +1 -1
- package/dist/prisma-integration.cjs +2 -125
- package/dist/prisma-integration.cjs.map +1 -1
- package/dist/prisma-integration.js +1 -124
- package/dist/prisma-integration.js.map +1 -1
- package/dist/process-integration.cjs +20 -1
- package/dist/process-integration.cjs.map +1 -1
- package/dist/process-integration.d.ts +7 -0
- package/dist/process-integration.d.ts.map +1 -1
- package/dist/process-integration.js +20 -1
- package/dist/process-integration.js.map +1 -1
- package/dist/redis-integration.cjs +2 -125
- package/dist/redis-integration.cjs.map +1 -1
- package/dist/redis-integration.js +1 -124
- package/dist/redis-integration.js.map +1 -1
- package/dist/rotating-file-transport.cjs +24 -238
- package/dist/rotating-file-transport.cjs.map +1 -1
- package/dist/rotating-file-transport.d.ts.map +1 -1
- package/dist/rotating-file-transport.js +24 -238
- package/dist/rotating-file-transport.js.map +1 -1
- package/dist/stdout-transport.cjs +3 -162
- package/dist/stdout-transport.cjs.map +1 -1
- package/dist/stdout-transport.js +3 -162
- package/dist/stdout-transport.js.map +1 -1
- package/dist/syslog-transport.cjs +1 -1
- package/dist/syslog-transport.js +1 -1
- package/dist/worker-transport.cjs +5 -4
- package/dist/worker-transport.cjs.map +1 -1
- package/dist/worker-transport.d.ts.map +1 -1
- package/dist/worker-transport.js +3 -2
- package/dist/worker-transport.js.map +1 -1
- package/package.json +5 -2
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,2357 +1,58 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
const
|
|
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
|
-
else waiter.resolve();
|
|
57
|
-
};
|
|
58
|
-
const recordError = (error, operation) => {
|
|
59
|
-
if (ignoreEpipe && isEpipe(error)) {
|
|
60
|
-
closed = true;
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
lastError = normalizeError(error);
|
|
64
|
-
reportInternalError(lastError, operation);
|
|
65
|
-
};
|
|
66
|
-
const onStreamError = (error) => {
|
|
67
|
-
recordError(error, "stream-error");
|
|
68
|
-
settleWaitersIfIdle();
|
|
69
|
-
};
|
|
70
|
-
const offError = () => options.stream.off?.("error", onStreamError);
|
|
71
|
-
options.stream.on?.("error", onStreamError);
|
|
72
|
-
const waitForDrain = () => {
|
|
73
|
-
if (!options.stream.once) return;
|
|
74
|
-
pendingDrains += 1;
|
|
75
|
-
options.stream.once("drain", () => {
|
|
76
|
-
pendingDrains -= 1;
|
|
77
|
-
settleWaitersIfIdle();
|
|
78
|
-
});
|
|
79
|
-
};
|
|
80
|
-
const removePending = (payload) => {
|
|
81
|
-
const index = pendingPayloads.indexOf(payload);
|
|
82
|
-
if (index >= 0) pendingPayloads.splice(index, 1);
|
|
83
|
-
};
|
|
84
|
-
const writeNow = (payload) => {
|
|
85
|
-
if (closed) return;
|
|
86
|
-
pendingPayloads.push(payload);
|
|
87
|
-
pendingWrites += 1;
|
|
88
|
-
try {
|
|
89
|
-
if (options.stream.write(payload, (error) => {
|
|
90
|
-
removePending(payload);
|
|
91
|
-
pendingWrites -= 1;
|
|
92
|
-
if (error) recordError(error, options.reportOperation ?? "write");
|
|
93
|
-
settleWaitersIfIdle();
|
|
94
|
-
}) === false) waitForDrain();
|
|
95
|
-
} catch (error) {
|
|
96
|
-
removePending(payload);
|
|
97
|
-
pendingWrites -= 1;
|
|
98
|
-
recordError(error, options.reportOperation ?? "write");
|
|
99
|
-
settleWaitersIfIdle();
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
const flushBuffered = () => {
|
|
103
|
-
if (buffer.length === 0) return;
|
|
104
|
-
const payload = coalescePayloads(buffer.splice(0));
|
|
105
|
-
bufferedBytes = 0;
|
|
106
|
-
writeNow(payload);
|
|
107
|
-
};
|
|
108
|
-
const destination = {
|
|
109
|
-
write(payload, context) {
|
|
110
|
-
if (closed) return;
|
|
111
|
-
lastContext = context;
|
|
112
|
-
if (minLength > 0) {
|
|
113
|
-
buffer.push(payload);
|
|
114
|
-
bufferedBytes += byteLength(payload);
|
|
115
|
-
if (bufferedBytes >= minLength) flushBuffered();
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
writeNow(payload);
|
|
119
|
-
},
|
|
120
|
-
flush() {
|
|
121
|
-
flushBuffered();
|
|
122
|
-
if (pendingWrites + pendingDrains === 0) {
|
|
123
|
-
const error = lastError;
|
|
124
|
-
lastError = void 0;
|
|
125
|
-
if (error) return Promise.reject(error);
|
|
126
|
-
return Promise.resolve();
|
|
127
|
-
}
|
|
128
|
-
return new Promise((resolve, reject) => {
|
|
129
|
-
waiters.push({
|
|
130
|
-
resolve,
|
|
131
|
-
reject
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
},
|
|
135
|
-
flushSync() {
|
|
136
|
-
if (!options.syncWrite) {
|
|
137
|
-
flushBuffered();
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
for (const payload of buffer.splice(0)) options.syncWrite(payload);
|
|
141
|
-
bufferedBytes = 0;
|
|
142
|
-
for (const payload of pendingPayloads.splice(0)) options.syncWrite(payload);
|
|
143
|
-
},
|
|
144
|
-
close() {
|
|
145
|
-
return destination.flush().then(() => new Promise((resolve, reject) => {
|
|
146
|
-
if (!options.stream.end) {
|
|
147
|
-
closed = true;
|
|
148
|
-
offError();
|
|
149
|
-
resolve();
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
options.stream.end((error) => {
|
|
153
|
-
closed = true;
|
|
154
|
-
offError();
|
|
155
|
-
if (error && !(ignoreEpipe && isEpipe(error))) reject(error);
|
|
156
|
-
else resolve();
|
|
157
|
-
});
|
|
158
|
-
}), (error) => {
|
|
159
|
-
offError();
|
|
160
|
-
return Promise.reject(error);
|
|
161
|
-
});
|
|
162
|
-
},
|
|
163
|
-
closeSync() {
|
|
164
|
-
destination.flushSync();
|
|
165
|
-
closed = true;
|
|
166
|
-
offError();
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
return destination;
|
|
170
|
-
}
|
|
171
|
-
function createNodeFileDestination(options) {
|
|
172
|
-
const flags = options.flags ?? (options.append === false ? "w" : "a");
|
|
173
|
-
if (options.mkdir) (0, fs.mkdirSync)((0, path.dirname)(options.path), { recursive: true });
|
|
174
|
-
let fd;
|
|
175
|
-
const getFd = () => {
|
|
176
|
-
fd ??= (0, fs.openSync)(options.path, flags);
|
|
177
|
-
return fd;
|
|
178
|
-
};
|
|
179
|
-
const syncWrite = (payload) => {
|
|
180
|
-
if (typeof payload === "string") (0, fs.writeSync)(getFd(), payload);
|
|
181
|
-
else (0, fs.writeSync)(getFd(), payload);
|
|
182
|
-
};
|
|
183
|
-
if (options.sync) {
|
|
184
|
-
let closed = false;
|
|
185
|
-
const releaseSync = () => {
|
|
186
|
-
if (fd !== void 0) {
|
|
187
|
-
(0, fs.closeSync)(fd);
|
|
188
|
-
fd = void 0;
|
|
189
|
-
}
|
|
190
|
-
};
|
|
191
|
-
return {
|
|
192
|
-
write(payload) {
|
|
193
|
-
if (closed) return;
|
|
194
|
-
syncWrite(payload);
|
|
195
|
-
},
|
|
196
|
-
flush() {
|
|
197
|
-
return Promise.resolve();
|
|
198
|
-
},
|
|
199
|
-
flushSync() {},
|
|
200
|
-
close() {
|
|
201
|
-
releaseSync();
|
|
202
|
-
closed = true;
|
|
203
|
-
return Promise.resolve();
|
|
204
|
-
},
|
|
205
|
-
closeSync() {
|
|
206
|
-
releaseSync();
|
|
207
|
-
closed = true;
|
|
208
|
-
},
|
|
209
|
-
releaseSync
|
|
210
|
-
};
|
|
211
|
-
}
|
|
212
|
-
const stream = (0, fs.createWriteStream)(options.path, { flags });
|
|
213
|
-
const destination = createNodeStreamDestination({
|
|
214
|
-
name: options.name,
|
|
215
|
-
stream,
|
|
216
|
-
minLength: options.minLength,
|
|
217
|
-
syncWrite
|
|
218
|
-
});
|
|
219
|
-
const releaseSync = () => {
|
|
220
|
-
if (fd !== void 0) {
|
|
221
|
-
(0, fs.closeSync)(fd);
|
|
222
|
-
fd = void 0;
|
|
223
|
-
}
|
|
224
|
-
};
|
|
225
|
-
return {
|
|
226
|
-
...destination,
|
|
227
|
-
stream,
|
|
228
|
-
close() {
|
|
229
|
-
return destination.close().finally(releaseSync);
|
|
230
|
-
},
|
|
231
|
-
closeSync() {
|
|
232
|
-
destination.closeSync();
|
|
233
|
-
releaseSync();
|
|
234
|
-
},
|
|
235
|
-
releaseSync
|
|
236
|
-
};
|
|
237
|
-
}
|
|
238
|
-
//#endregion
|
|
239
|
-
//#region src/stdout-transport.ts
|
|
240
|
-
function stdoutTransport(options = {}) {
|
|
241
|
-
const codec = options.codec ?? (0, _loggerjs_core.ndjsonCodec)();
|
|
242
|
-
const stream = options.stream ?? process.stdout;
|
|
243
|
-
const transportName = options.name ?? "stdout";
|
|
244
|
-
const destination = createNodeStreamDestination({
|
|
245
|
-
name: transportName,
|
|
246
|
-
stream,
|
|
247
|
-
minLength: options.minLength,
|
|
248
|
-
ignoreEpipe: options.ignoreEpipe ?? true
|
|
249
|
-
});
|
|
250
|
-
return {
|
|
251
|
-
name: transportName,
|
|
252
|
-
minLevel: options.minLevel,
|
|
253
|
-
log(event, context) {
|
|
254
|
-
if (options.minLevel !== void 0 && event.level < (0, _loggerjs_core.toLevelValue)(options.minLevel)) return;
|
|
255
|
-
destination.write(codec.encode(event), context);
|
|
256
|
-
},
|
|
257
|
-
flush: destination.flush,
|
|
258
|
-
flushSync: destination.flushSync,
|
|
259
|
-
close: destination.close
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
function stderrTransport(options = {}) {
|
|
263
|
-
return stdoutTransport({
|
|
264
|
-
...options,
|
|
265
|
-
name: options.name ?? "stderr",
|
|
266
|
-
stream: process.stderr
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
//#endregion
|
|
270
|
-
//#region src/file-transport.ts
|
|
271
|
-
function fileTransport(options) {
|
|
272
|
-
const codec = options.codec ?? (0, _loggerjs_core.ndjsonCodec)();
|
|
273
|
-
const destination = createNodeFileDestination({
|
|
274
|
-
name: options.name ?? "file",
|
|
275
|
-
path: options.path,
|
|
276
|
-
flags: options.flags,
|
|
277
|
-
append: options.append,
|
|
278
|
-
mkdir: options.mkdir,
|
|
279
|
-
sync: options.sync,
|
|
280
|
-
minLength: options.minLength
|
|
281
|
-
});
|
|
282
|
-
return {
|
|
283
|
-
name: options.name ?? "file",
|
|
284
|
-
minLevel: options.minLevel,
|
|
285
|
-
stream: destination.stream,
|
|
286
|
-
log(event, context) {
|
|
287
|
-
if (options.minLevel !== void 0 && event.level < (0, _loggerjs_core.toLevelValue)(options.minLevel)) return;
|
|
288
|
-
destination.write(codec.encode(event), context);
|
|
289
|
-
},
|
|
290
|
-
flush: destination.flush,
|
|
291
|
-
flushSync: destination.flushSync,
|
|
292
|
-
close: destination.close
|
|
293
|
-
};
|
|
294
|
-
}
|
|
295
|
-
//#endregion
|
|
296
|
-
//#region src/rotating-file-transport.ts
|
|
297
|
-
const textEncoder = new TextEncoder();
|
|
298
|
-
function payloadBytes(payload) {
|
|
299
|
-
return typeof payload === "string" ? textEncoder.encode(payload).byteLength : payload.byteLength;
|
|
300
|
-
}
|
|
301
|
-
function normalizePositiveInteger(value, fallback) {
|
|
302
|
-
if (value === void 0 || !Number.isFinite(value) || value <= 0) return fallback;
|
|
303
|
-
return Math.floor(value);
|
|
304
|
-
}
|
|
305
|
-
function normalizeNonNegativeInteger(value, fallback) {
|
|
306
|
-
if (value === void 0 || !Number.isFinite(value) || value < 0) return fallback;
|
|
307
|
-
return Math.floor(value);
|
|
308
|
-
}
|
|
309
|
-
function defaultArchivePath(path, index) {
|
|
310
|
-
return `${path}.${index}`;
|
|
311
|
-
}
|
|
312
|
-
function fileSize(path) {
|
|
313
|
-
try {
|
|
314
|
-
return (0, fs.statSync)(path).size;
|
|
315
|
-
} catch {
|
|
316
|
-
return 0;
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
function rotatingFileTransport(options) {
|
|
320
|
-
const codec = options.codec ?? (0, _loggerjs_core.ndjsonCodec)();
|
|
321
|
-
const maxBytes = normalizePositiveInteger(options.maxBytes, 10 * 1024 * 1024);
|
|
322
|
-
const maxFiles = normalizeNonNegativeInteger(options.maxFiles, 5);
|
|
323
|
-
const archivePath = options.archivePath ?? defaultArchivePath;
|
|
324
|
-
const destination = createNodeFileDestination({
|
|
325
|
-
name: options.name ?? "rotating-file",
|
|
326
|
-
path: options.path,
|
|
327
|
-
flags: options.flags,
|
|
328
|
-
append: options.append,
|
|
329
|
-
mkdir: options.mkdir,
|
|
330
|
-
sync: true
|
|
331
|
-
});
|
|
332
|
-
let bytes = fileSize(options.path);
|
|
333
|
-
const rotate = () => {
|
|
334
|
-
destination.releaseSync?.();
|
|
335
|
-
if (maxFiles === 0) {
|
|
336
|
-
if ((0, fs.existsSync)(options.path)) (0, fs.unlinkSync)(options.path);
|
|
337
|
-
bytes = 0;
|
|
338
|
-
return;
|
|
339
|
-
}
|
|
340
|
-
const oldest = archivePath(options.path, maxFiles);
|
|
341
|
-
if ((0, fs.existsSync)(oldest)) (0, fs.unlinkSync)(oldest);
|
|
342
|
-
for (let index = maxFiles - 1; index >= 1; index -= 1) {
|
|
343
|
-
const source = archivePath(options.path, index);
|
|
344
|
-
if ((0, fs.existsSync)(source)) (0, fs.renameSync)(source, archivePath(options.path, index + 1));
|
|
345
|
-
}
|
|
346
|
-
if ((0, fs.existsSync)(options.path)) (0, fs.renameSync)(options.path, archivePath(options.path, 1));
|
|
347
|
-
bytes = 0;
|
|
348
|
-
};
|
|
349
|
-
const writePayload = (payload) => {
|
|
350
|
-
const size = payloadBytes(payload);
|
|
351
|
-
if (bytes > 0 && bytes + size > maxBytes) rotate();
|
|
352
|
-
destination.write(payload);
|
|
353
|
-
bytes += size;
|
|
354
|
-
};
|
|
355
|
-
return {
|
|
356
|
-
name: options.name ?? "rotating-file",
|
|
357
|
-
minLevel: options.minLevel,
|
|
358
|
-
log(event) {
|
|
359
|
-
if (options.minLevel !== void 0 && event.level < (0, _loggerjs_core.toLevelValue)(options.minLevel)) return;
|
|
360
|
-
writePayload(codec.encode(event));
|
|
361
|
-
},
|
|
362
|
-
logBatch(events) {
|
|
363
|
-
for (const event of events) {
|
|
364
|
-
if (options.minLevel !== void 0 && event.level < (0, _loggerjs_core.toLevelValue)(options.minLevel)) continue;
|
|
365
|
-
writePayload(codec.encode(event));
|
|
366
|
-
}
|
|
367
|
-
},
|
|
368
|
-
flush() {
|
|
369
|
-
return destination.flush();
|
|
370
|
-
},
|
|
371
|
-
flushSync: destination.flushSync,
|
|
372
|
-
close() {
|
|
373
|
-
return destination.close();
|
|
374
|
-
},
|
|
375
|
-
rotate,
|
|
376
|
-
currentBytes: () => bytes
|
|
377
|
-
};
|
|
378
|
-
}
|
|
379
|
-
//#endregion
|
|
380
|
-
//#region src/http-transport.ts
|
|
381
|
-
function payloadToBody(payload) {
|
|
382
|
-
if (typeof payload === "string") return payload;
|
|
383
|
-
return Uint8Array.from(payload);
|
|
384
|
-
}
|
|
385
|
-
function nodeHttpTransport(options) {
|
|
386
|
-
const codec = options.codec ?? (0, _loggerjs_core.safeJsonCodec)();
|
|
387
|
-
const fetchFn = options.fetchFn ?? globalThis.fetch?.bind(globalThis);
|
|
388
|
-
return (0, _loggerjs_core.batchTransport)({
|
|
389
|
-
name: options.name ?? "node-http-inner",
|
|
390
|
-
minLevel: options.minLevel,
|
|
391
|
-
async logBatch(events) {
|
|
392
|
-
if (!fetchFn) throw new Error("fetch is not available. Use Node.js 18+ or pass fetchFn.");
|
|
393
|
-
const transformed = await (0, _loggerjs_core.applyPayloadTransforms)(codec.encode(events), {
|
|
394
|
-
contentType: codec.contentType,
|
|
395
|
-
events,
|
|
396
|
-
transport: options.name ?? "node-http"
|
|
397
|
-
}, options.transformPayload);
|
|
398
|
-
const response = await fetchFn(options.url, {
|
|
399
|
-
method: options.method ?? "POST",
|
|
400
|
-
headers: {
|
|
401
|
-
"content-type": transformed.contentType,
|
|
402
|
-
...transformed.headers,
|
|
403
|
-
...options.headers
|
|
404
|
-
},
|
|
405
|
-
body: payloadToBody(transformed.payload)
|
|
406
|
-
});
|
|
407
|
-
if (!response.ok) throw new Error(`nodeHttpTransport failed with status ${response.status}`);
|
|
408
|
-
}
|
|
409
|
-
}, {
|
|
410
|
-
name: options.name ?? "node-http",
|
|
411
|
-
maxRecords: options.maxRecords,
|
|
412
|
-
maxBatchSize: options.maxBatchSize,
|
|
413
|
-
maxBytes: options.maxBytes,
|
|
414
|
-
maxWaitMs: options.maxWaitMs,
|
|
415
|
-
flushIntervalMs: options.flushIntervalMs,
|
|
416
|
-
concurrency: options.concurrency,
|
|
417
|
-
maxQueueSize: options.maxQueueSize,
|
|
418
|
-
dropPolicy: options.dropPolicy,
|
|
419
|
-
estimateEventBytes: options.estimateEventBytes,
|
|
420
|
-
maxRetries: options.maxRetries,
|
|
421
|
-
retryBaseDelayMs: options.retryBaseDelayMs,
|
|
422
|
-
retryMaxDelayMs: options.retryMaxDelayMs,
|
|
423
|
-
random: options.random,
|
|
424
|
-
circuitBreakerFailureThreshold: options.circuitBreakerFailureThreshold,
|
|
425
|
-
circuitBreakerResetMs: options.circuitBreakerResetMs,
|
|
426
|
-
onDrop: options.onDrop
|
|
427
|
-
});
|
|
428
|
-
}
|
|
429
|
-
//#endregion
|
|
430
|
-
//#region src/payload-transforms.ts
|
|
431
|
-
const contentEncodingByFormat = {
|
|
432
|
-
gzip: "gzip",
|
|
433
|
-
brotli: "br",
|
|
434
|
-
deflate: "deflate"
|
|
435
|
-
};
|
|
436
|
-
function compressPayload(payload, format, options) {
|
|
437
|
-
const input = (0, _loggerjs_core.encodedPayloadToUint8Array)(payload);
|
|
438
|
-
if (format === "brotli") return Uint8Array.from((0, node_zlib.brotliCompressSync)(input, options.brotliOptions));
|
|
439
|
-
if (format === "deflate") return Uint8Array.from((0, node_zlib.deflateSync)(input, options.deflateOptions));
|
|
440
|
-
return Uint8Array.from((0, node_zlib.gzipSync)(input, options.gzipOptions));
|
|
441
|
-
}
|
|
442
|
-
function nodeCompressionPayloadTransform(options = {}) {
|
|
443
|
-
const format = options.format ?? "gzip";
|
|
444
|
-
return (payload) => ({
|
|
445
|
-
payload: compressPayload(payload, format, options),
|
|
446
|
-
headers: {
|
|
447
|
-
"content-encoding": contentEncodingByFormat[format],
|
|
448
|
-
...options.headers
|
|
449
|
-
}
|
|
450
|
-
});
|
|
451
|
-
}
|
|
452
|
-
//#endregion
|
|
453
|
-
//#region src/syslog-transport.ts
|
|
454
|
-
const severityByLevel = {
|
|
455
|
-
trace: 7,
|
|
456
|
-
debug: 7,
|
|
457
|
-
info: 6,
|
|
458
|
-
warn: 4,
|
|
459
|
-
error: 3,
|
|
460
|
-
fatal: 2
|
|
461
|
-
};
|
|
462
|
-
function defaultUdpSocketFactory(protocol) {
|
|
463
|
-
return (0, dgram.createSocket)(protocol);
|
|
464
|
-
}
|
|
465
|
-
function defaultTcpSocketFactory(options) {
|
|
466
|
-
return (0, net.createConnection)(options);
|
|
467
|
-
}
|
|
468
|
-
function headerValue$4(value, fallback, maxLength) {
|
|
469
|
-
const text = String(value ?? fallback).trim().replace(/\s+/g, "_");
|
|
470
|
-
if (!text) return fallback;
|
|
471
|
-
return text.length > maxLength ? text.slice(0, maxLength) : text;
|
|
472
|
-
}
|
|
473
|
-
function timestamp(time) {
|
|
474
|
-
const value = new Date(time).toISOString();
|
|
475
|
-
return value === "Invalid Date" ? "-" : value;
|
|
476
|
-
}
|
|
477
|
-
function valueFor(value, event, fallback) {
|
|
478
|
-
return typeof value === "function" ? value(event) : value ?? fallback;
|
|
479
|
-
}
|
|
480
|
-
function defaultMessage(event) {
|
|
481
|
-
const extra = {};
|
|
482
|
-
if (event.data !== void 0) extra.data = event.data;
|
|
483
|
-
if (event.context !== void 0) extra.context = event.context;
|
|
484
|
-
if (event.error !== void 0) extra.error = event.error;
|
|
485
|
-
if (Object.keys(extra).length === 0) return event.message;
|
|
486
|
-
return `${event.message} ${(0, _loggerjs_core.safeJsonStringify)(extra)}`;
|
|
487
|
-
}
|
|
488
|
-
function formatSyslogMessage(event, options = {}) {
|
|
489
|
-
const facility = Math.max(0, Math.min(23, Math.trunc(options.facility ?? 1)));
|
|
490
|
-
const severity = severityByLevel[event.levelName] ?? 6;
|
|
491
|
-
const priority = facility * 8 + severity;
|
|
492
|
-
const host = headerValue$4(options.hostname, (0, os.hostname)(), 255);
|
|
493
|
-
const appName = headerValue$4(valueFor(options.appName, event, event.logger), "-", 48);
|
|
494
|
-
const procId = headerValue$4(valueFor(options.procId, event, "-"), "-", 128);
|
|
495
|
-
const msgId = headerValue$4(valueFor(options.msgId, event, event.type ?? event.levelName), "-", 32);
|
|
496
|
-
const structuredData = valueFor(options.structuredData, event, "-") || "-";
|
|
497
|
-
const message = options.formatMessage ? options.formatMessage(event) : defaultMessage(event);
|
|
498
|
-
return `<${priority}>1 ${timestamp(event.time)} ${host} ${appName} ${procId} ${msgId} ${structuredData} ${message}`;
|
|
499
|
-
}
|
|
500
|
-
function nodeSyslogTransport(options = {}) {
|
|
501
|
-
const protocol = options.protocol ?? "udp4";
|
|
502
|
-
const host = options.host ?? "127.0.0.1";
|
|
503
|
-
const port = options.port ?? 514;
|
|
504
|
-
const transportName = options.name ?? "syslog";
|
|
505
|
-
const unref = options.unref ?? true;
|
|
506
|
-
const tcpFraming = options.tcpFraming ?? "newline";
|
|
507
|
-
const udpSocketFactory = options.udpSocketFactory ?? defaultUdpSocketFactory;
|
|
508
|
-
const tcpSocketFactory = options.tcpSocketFactory ?? defaultTcpSocketFactory;
|
|
509
|
-
let udpSocket;
|
|
510
|
-
let tcpSocket;
|
|
511
|
-
let lastContext;
|
|
512
|
-
const reportError = (error, context, operation) => {
|
|
513
|
-
try {
|
|
514
|
-
options.onError?.(error, { operation });
|
|
515
|
-
} catch (onErrorError) {
|
|
516
|
-
context?.reportInternalError(onErrorError, {
|
|
517
|
-
operation: "on-error",
|
|
518
|
-
phase: "transport",
|
|
519
|
-
transport: transportName
|
|
520
|
-
});
|
|
521
|
-
}
|
|
522
|
-
context?.reportInternalError(error, {
|
|
523
|
-
operation,
|
|
524
|
-
phase: "transport",
|
|
525
|
-
transport: transportName
|
|
526
|
-
});
|
|
527
|
-
};
|
|
528
|
-
const getUdpSocket = (context) => {
|
|
529
|
-
if (udpSocket) return udpSocket;
|
|
530
|
-
const socket = udpSocketFactory(protocol === "udp6" ? "udp6" : "udp4");
|
|
531
|
-
socket.on?.("error", (error) => reportError(error, lastContext ?? context, "socket-error"));
|
|
532
|
-
if (unref) socket.unref?.();
|
|
533
|
-
udpSocket = socket;
|
|
534
|
-
return socket;
|
|
535
|
-
};
|
|
536
|
-
const getTcpSocket = (context) => {
|
|
537
|
-
if (tcpSocket) return tcpSocket;
|
|
538
|
-
const socket = tcpSocketFactory({
|
|
539
|
-
host,
|
|
540
|
-
port
|
|
541
|
-
});
|
|
542
|
-
socket.on?.("error", (error) => reportError(error, lastContext ?? context, "socket-error"));
|
|
543
|
-
if (unref) socket.unref?.();
|
|
544
|
-
tcpSocket = socket;
|
|
545
|
-
return socket;
|
|
546
|
-
};
|
|
547
|
-
const send = (message, context) => {
|
|
548
|
-
lastContext = context;
|
|
549
|
-
if (protocol === "tcp") {
|
|
550
|
-
const bytes = new TextEncoder().encode(message).byteLength;
|
|
551
|
-
const payload = tcpFraming === "octet-counting" ? `${bytes} ${message}` : `${message}\n`;
|
|
552
|
-
getTcpSocket(context).write(payload, (error) => {
|
|
553
|
-
if (error) reportError(error, context, "send");
|
|
554
|
-
});
|
|
555
|
-
return;
|
|
556
|
-
}
|
|
557
|
-
getUdpSocket(context).send(message, port, host, (error) => {
|
|
558
|
-
if (error) reportError(error, context, "send");
|
|
559
|
-
});
|
|
560
|
-
};
|
|
561
|
-
return {
|
|
562
|
-
name: transportName,
|
|
563
|
-
minLevel: options.minLevel,
|
|
564
|
-
log(event, context) {
|
|
565
|
-
send(formatSyslogMessage(event, options), context);
|
|
566
|
-
},
|
|
567
|
-
logBatch(events, context) {
|
|
568
|
-
for (const event of events) send(formatSyslogMessage(event, options), context);
|
|
569
|
-
},
|
|
570
|
-
async close() {
|
|
571
|
-
udpSocket?.close?.();
|
|
572
|
-
udpSocket = void 0;
|
|
573
|
-
tcpSocket?.end?.();
|
|
574
|
-
tcpSocket = void 0;
|
|
575
|
-
}
|
|
576
|
-
};
|
|
577
|
-
}
|
|
578
|
-
//#endregion
|
|
579
|
-
//#region src/worker-transport.ts
|
|
580
|
-
function createDefaultWorker(options) {
|
|
581
|
-
if (!options.workerScript) throw new Error("workerTransport requires worker, workerFactory, or workerScript.");
|
|
582
|
-
return new node_worker_threads.Worker(options.workerScript, options.workerOptions);
|
|
583
|
-
}
|
|
584
|
-
function payloadToBytes(payload) {
|
|
585
|
-
if (typeof payload !== "string") return payload;
|
|
586
|
-
return new TextEncoder().encode(payload);
|
|
587
|
-
}
|
|
588
|
-
function transferablePayload(payload) {
|
|
589
|
-
const copy = Uint8Array.from(payload);
|
|
590
|
-
return {
|
|
591
|
-
payload: copy,
|
|
592
|
-
transferList: [copy.buffer]
|
|
593
|
-
};
|
|
594
|
-
}
|
|
595
|
-
function normalizePositiveTimeout(value) {
|
|
596
|
-
if (value === void 0 || !Number.isFinite(value) || value <= 0) return 0;
|
|
597
|
-
return Math.floor(value);
|
|
598
|
-
}
|
|
599
|
-
function protocolMessage(value) {
|
|
600
|
-
if (!value || typeof value !== "object") return void 0;
|
|
601
|
-
const message = value;
|
|
602
|
-
if (message.type === "loggerjs:ready") return { type: "loggerjs:ready" };
|
|
603
|
-
if (message.type === "loggerjs:batch:ack") {
|
|
604
|
-
const id = value.id;
|
|
605
|
-
if (typeof id === "number") return {
|
|
606
|
-
type: "loggerjs:batch:ack",
|
|
607
|
-
id
|
|
608
|
-
};
|
|
609
|
-
}
|
|
610
|
-
if (message.type === "loggerjs:error") {
|
|
611
|
-
const item = value;
|
|
612
|
-
return {
|
|
613
|
-
type: "loggerjs:error",
|
|
614
|
-
error: item.error,
|
|
615
|
-
message: typeof item.message === "string" ? item.message : void 0
|
|
616
|
-
};
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
function dropBatch(events, reason) {
|
|
620
|
-
(0, _loggerjs_core.incrementLoggerMetaCounter)("transport.dropped", events.length);
|
|
621
|
-
(0, _loggerjs_core.incrementLoggerMetaCounter)(`transport.dropped.${reason}`, events.length);
|
|
622
|
-
}
|
|
623
|
-
function workerTransport(options = {}) {
|
|
624
|
-
const codec = options.codec ?? (0, _loggerjs_core.safeJsonCodec)();
|
|
625
|
-
const transportName = options.name ?? "worker";
|
|
626
|
-
const fallback = options.fallback;
|
|
627
|
-
const readyTimeoutMs = normalizePositiveTimeout(options.readyTimeoutMs);
|
|
628
|
-
const ackTimeoutMs = normalizePositiveTimeout(options.ackTimeoutMs);
|
|
629
|
-
const autoEnd = options.autoEnd ?? true;
|
|
630
|
-
let worker;
|
|
631
|
-
let failed = false;
|
|
632
|
-
let failedError;
|
|
633
|
-
let lastContext;
|
|
634
|
-
let ready = readyTimeoutMs === 0;
|
|
635
|
-
let readyPromise;
|
|
636
|
-
let readyTimer;
|
|
637
|
-
let readyResolve;
|
|
638
|
-
let readyReject;
|
|
639
|
-
let nextBatchId = 1;
|
|
640
|
-
const pendingBatches = /* @__PURE__ */ new Map();
|
|
641
|
-
const setPendingGauge = () => {
|
|
642
|
-
(0, _loggerjs_core.setLoggerMetaGauge)(`transport.queue.depth.${transportName}`, pendingBatches.size);
|
|
643
|
-
};
|
|
644
|
-
const setReadyGauge = (value) => {
|
|
645
|
-
(0, _loggerjs_core.setLoggerMetaGauge)(`transport.ready.${transportName}`, value);
|
|
646
|
-
};
|
|
647
|
-
const reportInternalError = (error, operation) => {
|
|
648
|
-
lastContext?.reportInternalError(error, {
|
|
649
|
-
phase: "transport",
|
|
650
|
-
transport: transportName,
|
|
651
|
-
operation
|
|
652
|
-
});
|
|
653
|
-
};
|
|
654
|
-
const markFailed = (error, operation) => {
|
|
655
|
-
if (failed) return;
|
|
656
|
-
failed = true;
|
|
657
|
-
failedError = error instanceof Error ? error : new Error(String(error));
|
|
658
|
-
setReadyGauge(0);
|
|
659
|
-
(0, _loggerjs_core.incrementLoggerMetaCounter)("transport.worker.failed");
|
|
660
|
-
(0, _loggerjs_core.emitLoggerDiagnostic)({
|
|
661
|
-
stage: "worker",
|
|
662
|
-
phase: "error",
|
|
663
|
-
transport: transportName,
|
|
664
|
-
operation,
|
|
665
|
-
error
|
|
666
|
-
});
|
|
667
|
-
reportInternalError(error, operation);
|
|
668
|
-
readyReject?.(failedError);
|
|
669
|
-
failPendingBatches(operation);
|
|
670
|
-
};
|
|
671
|
-
const onWorkerError = (error) => markFailed(error, "worker-error");
|
|
672
|
-
const onWorkerExit = (code) => {
|
|
673
|
-
if (typeof code === "number" && code !== 0) markFailed(code, "worker-exit");
|
|
674
|
-
};
|
|
675
|
-
const onWorkerMessage = (value) => {
|
|
676
|
-
const message = protocolMessage(value);
|
|
677
|
-
if (!message) return;
|
|
678
|
-
if (message.type === "loggerjs:ready") {
|
|
679
|
-
ready = true;
|
|
680
|
-
setReadyGauge(1);
|
|
681
|
-
(0, _loggerjs_core.emitLoggerDiagnostic)({
|
|
682
|
-
stage: "worker",
|
|
683
|
-
phase: "end",
|
|
684
|
-
transport: transportName,
|
|
685
|
-
operation: "ready"
|
|
686
|
-
});
|
|
687
|
-
if (readyTimer) clearTimeout(readyTimer);
|
|
688
|
-
readyResolve?.();
|
|
689
|
-
return;
|
|
690
|
-
}
|
|
691
|
-
if (message.type === "loggerjs:batch:ack") {
|
|
692
|
-
const pending = pendingBatches.get(message.id);
|
|
693
|
-
if (!pending) return;
|
|
694
|
-
pendingBatches.delete(message.id);
|
|
695
|
-
setPendingGauge();
|
|
696
|
-
if (pending.timer) clearTimeout(pending.timer);
|
|
697
|
-
pending.resolve();
|
|
698
|
-
(0, _loggerjs_core.incrementLoggerMetaCounter)("transport.worker.ack");
|
|
699
|
-
return;
|
|
700
|
-
}
|
|
701
|
-
markFailed(message.error ?? new Error(message.message ?? "Worker reported an error"), "worker-message");
|
|
702
|
-
};
|
|
703
|
-
const fallbackOrDropBatch = async (events, context, reason) => {
|
|
704
|
-
if (fallback) {
|
|
705
|
-
await fallbackBatch(events, context);
|
|
706
|
-
return;
|
|
707
|
-
}
|
|
708
|
-
dropBatch(events, reason);
|
|
709
|
-
};
|
|
710
|
-
const failPendingBatch = (pending, reason) => {
|
|
711
|
-
pendingBatches.delete(pending.id);
|
|
712
|
-
setPendingGauge();
|
|
713
|
-
if (pending.timer) clearTimeout(pending.timer);
|
|
714
|
-
(0, _loggerjs_core.incrementLoggerMetaCounter)("transport.worker.pending-dropped", pending.events.length);
|
|
715
|
-
fallbackOrDropBatch(pending.events, pending.context, reason).finally(pending.resolve);
|
|
716
|
-
};
|
|
717
|
-
function failPendingBatches(reason) {
|
|
718
|
-
for (const pending of Array.from(pendingBatches.values())) failPendingBatch(pending, reason);
|
|
719
|
-
}
|
|
720
|
-
const waitForReady = async () => {
|
|
721
|
-
if (ready) return;
|
|
722
|
-
readyPromise ??= new Promise((resolve, reject) => {
|
|
723
|
-
readyResolve = resolve;
|
|
724
|
-
readyReject = reject;
|
|
725
|
-
readyTimer = setTimeout(() => {
|
|
726
|
-
reject(/* @__PURE__ */ new Error(`workerTransport ready timeout after ${readyTimeoutMs}ms`));
|
|
727
|
-
}, readyTimeoutMs);
|
|
728
|
-
});
|
|
729
|
-
await readyPromise;
|
|
730
|
-
};
|
|
731
|
-
const getWorker = () => {
|
|
732
|
-
if (failed) return void 0;
|
|
733
|
-
if (worker) return worker;
|
|
734
|
-
try {
|
|
735
|
-
worker = options.worker ?? options.workerFactory?.() ?? createDefaultWorker(options);
|
|
736
|
-
worker.on?.("error", onWorkerError);
|
|
737
|
-
worker.on?.("exit", onWorkerExit);
|
|
738
|
-
worker.on?.("message", onWorkerMessage);
|
|
739
|
-
if (ready) setReadyGauge(1);
|
|
740
|
-
return worker;
|
|
741
|
-
} catch (error) {
|
|
742
|
-
markFailed(error, "create-worker");
|
|
743
|
-
return;
|
|
744
|
-
}
|
|
745
|
-
};
|
|
746
|
-
const fallbackBatch = async (events, context) => {
|
|
747
|
-
if (fallback?.logBatch) {
|
|
748
|
-
await fallback.logBatch(events, context);
|
|
749
|
-
return;
|
|
750
|
-
}
|
|
751
|
-
if (fallback?.log) for (const event of events) await fallback.log(event, context);
|
|
752
|
-
};
|
|
753
|
-
const postBatch = async (events, context) => {
|
|
754
|
-
lastContext = context;
|
|
755
|
-
const currentWorker = getWorker();
|
|
756
|
-
if (!currentWorker) {
|
|
757
|
-
await fallbackOrDropBatch(events, context, "worker-unavailable");
|
|
758
|
-
return;
|
|
759
|
-
}
|
|
760
|
-
try {
|
|
761
|
-
await waitForReady();
|
|
762
|
-
} catch (error) {
|
|
763
|
-
markFailed(error, "worker-ready-timeout");
|
|
764
|
-
await fallbackOrDropBatch(events, context, "worker-ready-timeout");
|
|
765
|
-
return;
|
|
766
|
-
}
|
|
767
|
-
const encoded = payloadToBytes(codec.encode(events));
|
|
768
|
-
const transferable = options.transferBuffers === false ? {
|
|
769
|
-
payload: encoded,
|
|
770
|
-
transferList: void 0
|
|
771
|
-
} : transferablePayload(encoded);
|
|
772
|
-
const message = {
|
|
773
|
-
type: "loggerjs:batch",
|
|
774
|
-
codec: codec.name,
|
|
775
|
-
contentType: codec.contentType,
|
|
776
|
-
count: events.length,
|
|
777
|
-
payload: transferable.payload
|
|
778
|
-
};
|
|
779
|
-
let pending;
|
|
780
|
-
if (ackTimeoutMs > 0) {
|
|
781
|
-
const id = nextBatchId;
|
|
782
|
-
nextBatchId += 1;
|
|
783
|
-
message.id = id;
|
|
784
|
-
pending = {
|
|
785
|
-
id,
|
|
786
|
-
events,
|
|
787
|
-
context,
|
|
788
|
-
resolve: () => {}
|
|
789
|
-
};
|
|
790
|
-
pending.timer = setTimeout(() => {
|
|
791
|
-
if (!pending) return;
|
|
792
|
-
markFailed(/* @__PURE__ */ new Error(`workerTransport ack timeout after ${ackTimeoutMs}ms`), "worker-ack-timeout");
|
|
793
|
-
}, ackTimeoutMs);
|
|
794
|
-
pendingBatches.set(id, pending);
|
|
795
|
-
setPendingGauge();
|
|
796
|
-
}
|
|
797
|
-
try {
|
|
798
|
-
currentWorker.postMessage(message, transferable.transferList);
|
|
799
|
-
} catch (error) {
|
|
800
|
-
markFailed(error, "post-message");
|
|
801
|
-
if (!pending) await fallbackOrDropBatch(events, context, "worker-post-message");
|
|
802
|
-
}
|
|
803
|
-
};
|
|
804
|
-
const transport = {
|
|
805
|
-
name: transportName,
|
|
806
|
-
minLevel: options.minLevel,
|
|
807
|
-
async ready() {
|
|
808
|
-
if (!getWorker()) {
|
|
809
|
-
if (fallback) {
|
|
810
|
-
await fallback.ready?.();
|
|
811
|
-
return;
|
|
812
|
-
}
|
|
813
|
-
throw failedError ?? /* @__PURE__ */ new Error("workerTransport is unavailable");
|
|
814
|
-
}
|
|
815
|
-
try {
|
|
816
|
-
await waitForReady();
|
|
817
|
-
} catch (error) {
|
|
818
|
-
markFailed(error, "worker-ready-timeout");
|
|
819
|
-
if (fallback) {
|
|
820
|
-
await fallback.ready?.();
|
|
821
|
-
return;
|
|
822
|
-
}
|
|
823
|
-
throw error;
|
|
824
|
-
}
|
|
825
|
-
},
|
|
826
|
-
log(event, context) {
|
|
827
|
-
if (options.minLevel !== void 0 && event.level < (0, _loggerjs_core.toLevelValue)(options.minLevel)) return;
|
|
828
|
-
return postBatch([event], context);
|
|
829
|
-
},
|
|
830
|
-
logBatch(events, context) {
|
|
831
|
-
return postBatch(events, context);
|
|
832
|
-
},
|
|
833
|
-
async flush() {
|
|
834
|
-
await Promise.all(Array.from(pendingBatches.values(), (pending) => new Promise((resolve) => {
|
|
835
|
-
const originalResolve = pending.resolve;
|
|
836
|
-
pending.resolve = () => {
|
|
837
|
-
originalResolve();
|
|
838
|
-
resolve();
|
|
839
|
-
};
|
|
840
|
-
})));
|
|
841
|
-
await fallback?.flush?.();
|
|
842
|
-
},
|
|
843
|
-
async close() {
|
|
844
|
-
await transport.flush?.();
|
|
845
|
-
worker?.off?.("error", onWorkerError);
|
|
846
|
-
worker?.off?.("exit", onWorkerExit);
|
|
847
|
-
worker?.off?.("message", onWorkerMessage);
|
|
848
|
-
await fallback?.close?.();
|
|
849
|
-
if (autoEnd) await worker?.terminate?.();
|
|
850
|
-
if (readyTimer) clearTimeout(readyTimer);
|
|
851
|
-
readyReject = void 0;
|
|
852
|
-
readyResolve = void 0;
|
|
853
|
-
}
|
|
854
|
-
};
|
|
855
|
-
return transport;
|
|
856
|
-
}
|
|
857
|
-
//#endregion
|
|
858
|
-
//#region src/context.ts
|
|
859
|
-
function mergeContext(...items) {
|
|
860
|
-
const out = {};
|
|
861
|
-
for (const item of items) {
|
|
862
|
-
if (!item) continue;
|
|
863
|
-
Object.assign(out, item);
|
|
864
|
-
}
|
|
865
|
-
return (0, _loggerjs_core.createBoundContext)(out) ?? void 0;
|
|
866
|
-
}
|
|
867
|
-
function createAsyncLocalStorageContextManager(storage = new node_async_hooks.AsyncLocalStorage()) {
|
|
868
|
-
return {
|
|
869
|
-
get() {
|
|
870
|
-
return storage.getStore();
|
|
871
|
-
},
|
|
872
|
-
with(context, fn) {
|
|
873
|
-
return storage.run(mergeContext(storage.getStore(), context) ?? Object.freeze({}), fn);
|
|
874
|
-
},
|
|
875
|
-
disable() {
|
|
876
|
-
storage.disable();
|
|
877
|
-
}
|
|
878
|
-
};
|
|
879
|
-
}
|
|
880
|
-
function installAsyncLocalStorageContext(manager = createAsyncLocalStorageContextManager()) {
|
|
881
|
-
(0, _loggerjs_core.setContextManager)(manager);
|
|
882
|
-
return () => {
|
|
883
|
-
(0, _loggerjs_core.resetContextManager)();
|
|
884
|
-
manager.disable();
|
|
885
|
-
};
|
|
886
|
-
}
|
|
887
|
-
//#endregion
|
|
888
|
-
//#region src/queue-integration.ts
|
|
889
|
-
const defaultMethods$1 = [
|
|
890
|
-
"add",
|
|
891
|
-
"ack",
|
|
892
|
-
"consume",
|
|
893
|
-
"dequeue",
|
|
894
|
-
"nack",
|
|
895
|
-
"process",
|
|
896
|
-
"publish",
|
|
897
|
-
"receive",
|
|
898
|
-
"send",
|
|
899
|
-
"sendToQueue"
|
|
900
|
-
];
|
|
901
|
-
const defaultRandom$6 = () => Math.random();
|
|
902
|
-
function defaultLevel$6(_durationMs, error) {
|
|
903
|
-
return error ? "error" : "debug";
|
|
904
|
-
}
|
|
905
|
-
function isPromiseLike$2(value) {
|
|
906
|
-
return typeof value === "object" && value !== null && typeof value.then === "function";
|
|
907
|
-
}
|
|
908
|
-
function defaultOperation$1(method) {
|
|
909
|
-
switch (method) {
|
|
910
|
-
case "add":
|
|
911
|
-
case "publish":
|
|
912
|
-
case "send":
|
|
913
|
-
case "sendToQueue": return "publish";
|
|
914
|
-
case "consume":
|
|
915
|
-
case "dequeue":
|
|
916
|
-
case "process":
|
|
917
|
-
case "receive": return "consume";
|
|
918
|
-
case "ack": return "ack";
|
|
919
|
-
case "nack": return "nack";
|
|
920
|
-
default: return "other";
|
|
921
|
-
}
|
|
922
|
-
}
|
|
923
|
-
function defaultPayload(args) {
|
|
924
|
-
return typeof args[0] === "string" && args.length > 1 ? args[1] : args[0];
|
|
925
|
-
}
|
|
926
|
-
function defaultMessageId(args) {
|
|
927
|
-
const payload = defaultPayload(args);
|
|
928
|
-
if (typeof payload !== "object" || payload === null) return void 0;
|
|
929
|
-
const value = payload.messageId ?? payload.id;
|
|
930
|
-
return value === void 0 ? void 0 : String(value);
|
|
931
|
-
}
|
|
932
|
-
function payloadFor(args, method, options) {
|
|
933
|
-
if (!options.capturePayload) return void 0;
|
|
934
|
-
const payload = options.getPayload?.(args, method) ?? defaultPayload(args);
|
|
935
|
-
return payload === void 0 ? void 0 : (0, _loggerjs_core.normalizeValue)(payload, {
|
|
936
|
-
maxDepth: 4,
|
|
937
|
-
maxObjectKeys: 80
|
|
938
|
-
});
|
|
939
|
-
}
|
|
940
|
-
function queueIntegration(options = {}) {
|
|
941
|
-
const targets = options.targets ?? (options.client ? [{
|
|
942
|
-
client: options.client,
|
|
943
|
-
name: options.name,
|
|
944
|
-
system: options.system,
|
|
945
|
-
queueName: options.queueName,
|
|
946
|
-
methods: options.methods
|
|
947
|
-
}] : []);
|
|
948
|
-
const captureAll = options.captureAll ?? options.captureSuccessful ?? false;
|
|
949
|
-
const sampleRate = options.sampleRate ?? 1;
|
|
950
|
-
const random = options.random ?? defaultRandom$6;
|
|
951
|
-
const levelFor = options.level ?? defaultLevel$6;
|
|
952
|
-
const shouldCapture = (durationMs, error) => {
|
|
953
|
-
if (error) return true;
|
|
954
|
-
if (options.minDurationMs !== void 0 && durationMs >= options.minDurationMs) return true;
|
|
955
|
-
if (!captureAll) return false;
|
|
956
|
-
return sampleRate >= 1 || random() < sampleRate;
|
|
957
|
-
};
|
|
958
|
-
return {
|
|
959
|
-
name: "queue",
|
|
960
|
-
setup(api) {
|
|
961
|
-
const capture = api.guard((input) => api.capture(input));
|
|
962
|
-
const disposers = [];
|
|
963
|
-
let disposed = false;
|
|
964
|
-
const captureResult = (info, started, args, error) => {
|
|
965
|
-
const durationMs = Date.now() - started;
|
|
966
|
-
if (disposed || !shouldCapture(durationMs, error)) return;
|
|
967
|
-
capture({
|
|
968
|
-
level: levelFor(durationMs, error, info),
|
|
969
|
-
message: error ? `Queue error ${info.operation} ${info.queueName ?? info.method}` : `Queue ${info.operation} ${info.queueName ?? info.method}`,
|
|
970
|
-
error,
|
|
971
|
-
props: { queue: {
|
|
972
|
-
kind: info.target,
|
|
973
|
-
system: info.system,
|
|
974
|
-
queueName: info.queueName,
|
|
975
|
-
method: info.method,
|
|
976
|
-
operation: info.operation,
|
|
977
|
-
durationMs,
|
|
978
|
-
messageId: options.getMessageId?.(args, info.method) ?? defaultMessageId(args),
|
|
979
|
-
payload: payloadFor(args, info.method, options)
|
|
980
|
-
} }
|
|
981
|
-
});
|
|
982
|
-
};
|
|
983
|
-
for (const target of targets) {
|
|
984
|
-
const methods = target.methods ?? options.methods ?? defaultMethods$1;
|
|
985
|
-
for (const method of methods) {
|
|
986
|
-
const original = target.client[method];
|
|
987
|
-
if (typeof original !== "function") continue;
|
|
988
|
-
const originalMethod = original;
|
|
989
|
-
const targetName = target.name ?? options.name ?? "queue";
|
|
990
|
-
const system = target.system ?? options.system;
|
|
991
|
-
target.client[method] = function wrappedQueueMethod(...args) {
|
|
992
|
-
const started = Date.now();
|
|
993
|
-
const operation = options.getOperation?.(method, args) ?? defaultOperation$1(method);
|
|
994
|
-
const info = {
|
|
995
|
-
target: targetName,
|
|
996
|
-
system,
|
|
997
|
-
queueName: options.getQueueName?.(args, method) ?? target.queueName ?? options.queueName ?? (typeof args[0] === "string" ? args[0] : void 0),
|
|
998
|
-
method,
|
|
999
|
-
operation
|
|
1000
|
-
};
|
|
1001
|
-
try {
|
|
1002
|
-
const result = originalMethod.apply(this, args);
|
|
1003
|
-
if (isPromiseLike$2(result)) return result.then((value) => {
|
|
1004
|
-
captureResult(info, started, args, void 0);
|
|
1005
|
-
return value;
|
|
1006
|
-
}, (error) => {
|
|
1007
|
-
captureResult(info, started, args, error);
|
|
1008
|
-
throw error;
|
|
1009
|
-
});
|
|
1010
|
-
captureResult(info, started, args, void 0);
|
|
1011
|
-
return result;
|
|
1012
|
-
} catch (error) {
|
|
1013
|
-
captureResult(info, started, args, error);
|
|
1014
|
-
throw error;
|
|
1015
|
-
}
|
|
1016
|
-
};
|
|
1017
|
-
disposers.push(() => {
|
|
1018
|
-
target.client[method] = originalMethod;
|
|
1019
|
-
});
|
|
1020
|
-
}
|
|
1021
|
-
}
|
|
1022
|
-
return () => {
|
|
1023
|
-
if (disposed) return;
|
|
1024
|
-
disposed = true;
|
|
1025
|
-
for (const dispose of disposers) dispose();
|
|
1026
|
-
};
|
|
1027
|
-
}
|
|
1028
|
-
};
|
|
1029
|
-
}
|
|
1030
|
-
//#endregion
|
|
1031
|
-
//#region src/bullmq-integration.ts
|
|
1032
|
-
const bullMqMethods = [
|
|
1033
|
-
"add",
|
|
1034
|
-
"addBulk",
|
|
1035
|
-
"process"
|
|
1036
|
-
];
|
|
1037
|
-
function queueName(args, fallback) {
|
|
1038
|
-
const first = args[0];
|
|
1039
|
-
return typeof first === "string" ? first : fallback;
|
|
1040
|
-
}
|
|
1041
|
-
function operationFor(method) {
|
|
1042
|
-
if (method === "process") return "consume";
|
|
1043
|
-
if (method === "add" || method === "addBulk") return "publish";
|
|
1044
|
-
return "other";
|
|
1045
|
-
}
|
|
1046
|
-
function bullMqIntegration(options) {
|
|
1047
|
-
return queueIntegration({
|
|
1048
|
-
...options,
|
|
1049
|
-
name: options.name ?? "bullmq",
|
|
1050
|
-
system: "bullmq",
|
|
1051
|
-
methods: options.methods ?? bullMqMethods,
|
|
1052
|
-
getOperation: options.getOperation ?? operationFor,
|
|
1053
|
-
queueName: options.queueName ?? options.client.name,
|
|
1054
|
-
getQueueName: (args) => queueName(args, options.queueName ?? options.client.name)
|
|
1055
|
-
});
|
|
1056
|
-
}
|
|
1057
|
-
//#endregion
|
|
1058
|
-
//#region src/cli-integration.ts
|
|
1059
|
-
const defaultSignals = ["SIGINT", "SIGTERM"];
|
|
1060
|
-
function defaultProcess() {
|
|
1061
|
-
return globalThis.process;
|
|
1062
|
-
}
|
|
1063
|
-
function defaultSanitizeArg(arg) {
|
|
1064
|
-
if (/^(--?[^=\s]*(token|password|passwd|secret|key)[^=\s]*=)/i.test(arg)) return arg.replace(/=.*/, "=[redacted]");
|
|
1065
|
-
return arg;
|
|
1066
|
-
}
|
|
1067
|
-
function defaultCommand(argv) {
|
|
1068
|
-
return argv[1] ?? argv[0];
|
|
1069
|
-
}
|
|
1070
|
-
function levelFor$2(level, event, code) {
|
|
1071
|
-
if (typeof level === "function") return level(event, code);
|
|
1072
|
-
if (level) return level;
|
|
1073
|
-
if (event === "signal") return "warn";
|
|
1074
|
-
if (event === "exit" && Number(code ?? 0) !== 0) return "error";
|
|
1075
|
-
return "info";
|
|
1076
|
-
}
|
|
1077
|
-
function pickEnv(env, names) {
|
|
1078
|
-
if (!env || !names || names.length === 0) return void 0;
|
|
1079
|
-
const out = {};
|
|
1080
|
-
for (const name of names) {
|
|
1081
|
-
const value = env[name];
|
|
1082
|
-
if (value !== void 0) out[name] = value;
|
|
1083
|
-
}
|
|
1084
|
-
return Object.keys(out).length > 0 ? out : void 0;
|
|
1085
|
-
}
|
|
1086
|
-
function removeListener$1(processLike, event, listener) {
|
|
1087
|
-
if (processLike.off) {
|
|
1088
|
-
processLike.off(event, listener);
|
|
1089
|
-
return;
|
|
1090
|
-
}
|
|
1091
|
-
processLike.removeListener?.(event, listener);
|
|
1092
|
-
}
|
|
1093
|
-
function captureCliIntegration(options = {}) {
|
|
1094
|
-
const name = options.name ?? "cli";
|
|
1095
|
-
const captureStart = options.captureStart ?? true;
|
|
1096
|
-
const captureExit = options.captureExit ?? true;
|
|
1097
|
-
const captureSignals = options.captureSignals ?? true;
|
|
1098
|
-
const captureCwd = options.captureCwd ?? true;
|
|
1099
|
-
const signals = options.signals ?? defaultSignals;
|
|
1100
|
-
const sanitizeArg = options.sanitizeArg ?? defaultSanitizeArg;
|
|
1101
|
-
const commandFor = options.getCommand ?? defaultCommand;
|
|
1102
|
-
return {
|
|
1103
|
-
name: "capture-cli",
|
|
1104
|
-
setup(api) {
|
|
1105
|
-
const processLike = options.process ?? defaultProcess();
|
|
1106
|
-
if (!processLike) return;
|
|
1107
|
-
const argv = processLike.argv ?? [];
|
|
1108
|
-
const command = commandFor(argv);
|
|
1109
|
-
const sanitizedArgv = argv.map((arg, index) => sanitizeArg(arg, index, argv));
|
|
1110
|
-
const disposers = [];
|
|
1111
|
-
let disposed = false;
|
|
1112
|
-
const capture = api.guard((input) => {
|
|
1113
|
-
if (!disposed) api.capture(input);
|
|
1114
|
-
});
|
|
1115
|
-
const cliBase = {
|
|
1116
|
-
kind: name,
|
|
1117
|
-
command,
|
|
1118
|
-
argv: sanitizedArgv,
|
|
1119
|
-
cwd: captureCwd ? processLike.cwd?.() : void 0,
|
|
1120
|
-
env: pickEnv(processLike.env, options.captureEnv)
|
|
1121
|
-
};
|
|
1122
|
-
if (captureStart) capture({
|
|
1123
|
-
level: levelFor$2(options.level, "start"),
|
|
1124
|
-
message: `CLI start${command ? ` ${command}` : ""}`,
|
|
1125
|
-
props: { cli: {
|
|
1126
|
-
...cliBase,
|
|
1127
|
-
lifecycle: "start"
|
|
1128
|
-
} }
|
|
1129
|
-
});
|
|
1130
|
-
if (captureExit && processLike.on) {
|
|
1131
|
-
const onExit = (code) => {
|
|
1132
|
-
const exitCode = code ?? processLike.exitCode ?? 0;
|
|
1133
|
-
capture({
|
|
1134
|
-
level: levelFor$2(options.level, "exit", exitCode),
|
|
1135
|
-
message: `CLI exit ${String(exitCode)}`,
|
|
1136
|
-
props: { cli: {
|
|
1137
|
-
...cliBase,
|
|
1138
|
-
lifecycle: "exit",
|
|
1139
|
-
exitCode
|
|
1140
|
-
} }
|
|
1141
|
-
});
|
|
1142
|
-
};
|
|
1143
|
-
processLike.on("exit", onExit);
|
|
1144
|
-
disposers.push(() => removeListener$1(processLike, "exit", onExit));
|
|
1145
|
-
}
|
|
1146
|
-
if (captureSignals && processLike.on) for (const signal of signals) {
|
|
1147
|
-
const onSignal = () => {
|
|
1148
|
-
capture({
|
|
1149
|
-
level: levelFor$2(options.level, "signal", signal),
|
|
1150
|
-
message: `CLI signal ${signal}`,
|
|
1151
|
-
props: { cli: {
|
|
1152
|
-
...cliBase,
|
|
1153
|
-
lifecycle: "signal",
|
|
1154
|
-
signal
|
|
1155
|
-
} }
|
|
1156
|
-
});
|
|
1157
|
-
};
|
|
1158
|
-
processLike.on(signal, onSignal);
|
|
1159
|
-
disposers.push(() => removeListener$1(processLike, signal, onSignal));
|
|
1160
|
-
}
|
|
1161
|
-
return () => {
|
|
1162
|
-
if (disposed) return;
|
|
1163
|
-
disposed = true;
|
|
1164
|
-
for (const dispose of disposers) dispose();
|
|
1165
|
-
};
|
|
1166
|
-
}
|
|
1167
|
-
};
|
|
1168
|
-
}
|
|
1169
|
-
//#endregion
|
|
1170
|
-
//#region src/database-integration.ts
|
|
1171
|
-
const defaultMethods = [
|
|
1172
|
-
"query",
|
|
1173
|
-
"execute",
|
|
1174
|
-
"run",
|
|
1175
|
-
"all",
|
|
1176
|
-
"get"
|
|
1177
|
-
];
|
|
1178
|
-
const defaultRandom$5 = () => Math.random();
|
|
1179
|
-
function defaultLevel$5(_durationMs, error) {
|
|
1180
|
-
return error ? "error" : "debug";
|
|
1181
|
-
}
|
|
1182
|
-
function isPromiseLike$1(value) {
|
|
1183
|
-
return typeof value === "object" && value !== null && typeof value.then === "function";
|
|
1184
|
-
}
|
|
1185
|
-
function defaultStatement(args) {
|
|
1186
|
-
const first = args[0];
|
|
1187
|
-
if (typeof first === "string") return first;
|
|
1188
|
-
if (typeof first !== "object" || first === null) return void 0;
|
|
1189
|
-
const input = first;
|
|
1190
|
-
if (typeof input.sql === "string") return input.sql;
|
|
1191
|
-
if (typeof input.text === "string") return input.text;
|
|
1192
|
-
if (typeof input.query === "string") return input.query;
|
|
1193
|
-
}
|
|
1194
|
-
function statementFor(args, method, options) {
|
|
1195
|
-
const statement = options.getStatement?.(args, method) ?? defaultStatement(args);
|
|
1196
|
-
return statement && options.sanitizeStatement ? options.sanitizeStatement(statement) : statement;
|
|
1197
|
-
}
|
|
1198
|
-
function parametersFor(args, captureParameters) {
|
|
1199
|
-
if (!captureParameters) return void 0;
|
|
1200
|
-
const first = args[0];
|
|
1201
|
-
const parameters = args[1] ?? (typeof first === "object" && first ? first.values ?? first.params ?? first.parameters : void 0);
|
|
1202
|
-
return parameters === void 0 ? void 0 : (0, _loggerjs_core.normalizeValue)(parameters, {
|
|
1203
|
-
maxDepth: 4,
|
|
1204
|
-
maxObjectKeys: 80
|
|
1205
|
-
});
|
|
1206
|
-
}
|
|
1207
|
-
function databaseIntegration(options = {}) {
|
|
1208
|
-
const targets = options.targets ?? (options.client ? [{
|
|
1209
|
-
client: options.client,
|
|
1210
|
-
name: options.name,
|
|
1211
|
-
system: options.system,
|
|
1212
|
-
methods: options.methods
|
|
1213
|
-
}] : []);
|
|
1214
|
-
const captureAll = options.captureAll ?? options.captureSuccessful ?? false;
|
|
1215
|
-
const sampleRate = options.sampleRate ?? 1;
|
|
1216
|
-
const random = options.random ?? defaultRandom$5;
|
|
1217
|
-
const levelFor = options.level ?? defaultLevel$5;
|
|
1218
|
-
const shouldCapture = (durationMs, error) => {
|
|
1219
|
-
if (error) return true;
|
|
1220
|
-
if (options.minDurationMs !== void 0 && durationMs >= options.minDurationMs) return true;
|
|
1221
|
-
if (!captureAll) return false;
|
|
1222
|
-
return sampleRate >= 1 || random() < sampleRate;
|
|
1223
|
-
};
|
|
1224
|
-
return {
|
|
1225
|
-
name: "database",
|
|
1226
|
-
setup(api) {
|
|
1227
|
-
const capture = api.guard((input) => api.capture(input));
|
|
1228
|
-
const disposers = [];
|
|
1229
|
-
let disposed = false;
|
|
1230
|
-
const captureResult = (info, started, args, error) => {
|
|
1231
|
-
const durationMs = Date.now() - started;
|
|
1232
|
-
if (disposed || !shouldCapture(durationMs, error)) return;
|
|
1233
|
-
capture({
|
|
1234
|
-
level: levelFor(durationMs, error, info),
|
|
1235
|
-
message: error ? `Database error ${info.method}${info.statement ? ` ${info.statement}` : ""}` : `Database ${info.method}${info.statement ? ` ${info.statement}` : ""}`,
|
|
1236
|
-
error,
|
|
1237
|
-
props: { db: {
|
|
1238
|
-
kind: info.target,
|
|
1239
|
-
system: info.system,
|
|
1240
|
-
method: info.method,
|
|
1241
|
-
statement: info.statement,
|
|
1242
|
-
durationMs,
|
|
1243
|
-
parameters: parametersFor(args, options.captureParameters ?? false)
|
|
1244
|
-
} }
|
|
1245
|
-
});
|
|
1246
|
-
};
|
|
1247
|
-
for (const target of targets) {
|
|
1248
|
-
const methods = target.methods ?? options.methods ?? defaultMethods;
|
|
1249
|
-
for (const method of methods) {
|
|
1250
|
-
const original = target.client[method];
|
|
1251
|
-
if (typeof original !== "function") continue;
|
|
1252
|
-
const originalMethod = original;
|
|
1253
|
-
const targetName = target.name ?? options.name ?? "database";
|
|
1254
|
-
const system = target.system ?? options.system;
|
|
1255
|
-
target.client[method] = function wrappedDatabaseMethod(...args) {
|
|
1256
|
-
const started = Date.now();
|
|
1257
|
-
const info = {
|
|
1258
|
-
target: targetName,
|
|
1259
|
-
system,
|
|
1260
|
-
method,
|
|
1261
|
-
statement: statementFor(args, method, options)
|
|
1262
|
-
};
|
|
1263
|
-
try {
|
|
1264
|
-
const result = originalMethod.apply(this, args);
|
|
1265
|
-
if (isPromiseLike$1(result)) return result.then((value) => {
|
|
1266
|
-
captureResult(info, started, args, void 0);
|
|
1267
|
-
return value;
|
|
1268
|
-
}, (error) => {
|
|
1269
|
-
captureResult(info, started, args, error);
|
|
1270
|
-
throw error;
|
|
1271
|
-
});
|
|
1272
|
-
captureResult(info, started, args, void 0);
|
|
1273
|
-
return result;
|
|
1274
|
-
} catch (error) {
|
|
1275
|
-
captureResult(info, started, args, error);
|
|
1276
|
-
throw error;
|
|
1277
|
-
}
|
|
1278
|
-
};
|
|
1279
|
-
disposers.push(() => {
|
|
1280
|
-
target.client[method] = originalMethod;
|
|
1281
|
-
});
|
|
1282
|
-
}
|
|
1283
|
-
}
|
|
1284
|
-
return () => {
|
|
1285
|
-
if (disposed) return;
|
|
1286
|
-
disposed = true;
|
|
1287
|
-
for (const dispose of disposers) dispose();
|
|
1288
|
-
};
|
|
1289
|
-
}
|
|
1290
|
-
};
|
|
1291
|
-
}
|
|
1292
|
-
//#endregion
|
|
1293
|
-
//#region src/express-integration.ts
|
|
1294
|
-
const defaultRandom$4 = () => Math.random();
|
|
1295
|
-
function defaultSanitizeUrl$3(url) {
|
|
1296
|
-
const queryIndex = url.indexOf("?");
|
|
1297
|
-
const hashIndex = url.indexOf("#");
|
|
1298
|
-
const end = Math.min(queryIndex >= 0 ? queryIndex : url.length, hashIndex >= 0 ? hashIndex : url.length);
|
|
1299
|
-
return url.slice(0, end);
|
|
1300
|
-
}
|
|
1301
|
-
function defaultLevel$4(status, _req, _res, aborted) {
|
|
1302
|
-
if (aborted) return "warn";
|
|
1303
|
-
if (status >= 500) return "error";
|
|
1304
|
-
if (status >= 400) return "warn";
|
|
1305
|
-
return "info";
|
|
1306
|
-
}
|
|
1307
|
-
function requestUrl(req) {
|
|
1308
|
-
return req.originalUrl ?? req.url ?? req.path ?? "/";
|
|
1309
|
-
}
|
|
1310
|
-
function headerValue$3(value) {
|
|
1311
|
-
if (typeof value === "number") return String(value);
|
|
1312
|
-
return value;
|
|
1313
|
-
}
|
|
1314
|
-
function pickRequestHeaders$1(req, names) {
|
|
1315
|
-
if (!names || names.length === 0 || !req.headers) return void 0;
|
|
1316
|
-
const out = {};
|
|
1317
|
-
for (const name of names) {
|
|
1318
|
-
const value = req.headers[name.toLowerCase()] ?? req.headers[name];
|
|
1319
|
-
if (value !== void 0) out[name.toLowerCase()] = value;
|
|
1320
|
-
}
|
|
1321
|
-
return Object.keys(out).length > 0 ? out : void 0;
|
|
1322
|
-
}
|
|
1323
|
-
function pickResponseHeaders$1(res, names) {
|
|
1324
|
-
if (!names || names.length === 0 || !res.getHeader) return void 0;
|
|
1325
|
-
const out = {};
|
|
1326
|
-
for (const name of names) {
|
|
1327
|
-
const value = headerValue$3(res.getHeader(name));
|
|
1328
|
-
if (value !== void 0) out[name.toLowerCase()] = value;
|
|
1329
|
-
}
|
|
1330
|
-
return Object.keys(out).length > 0 ? out : void 0;
|
|
1331
|
-
}
|
|
1332
|
-
function defaultRoute$1(req) {
|
|
1333
|
-
const path = req.route?.path;
|
|
1334
|
-
return typeof path === "string" ? path : void 0;
|
|
1335
|
-
}
|
|
1336
|
-
function removeListener(res, event, listener) {
|
|
1337
|
-
if (res.off) {
|
|
1338
|
-
res.off(event, listener);
|
|
1339
|
-
return;
|
|
1340
|
-
}
|
|
1341
|
-
res.removeListener?.(event, listener);
|
|
1342
|
-
}
|
|
1343
|
-
function onResponseDone(res, event, listener) {
|
|
1344
|
-
if (res.once) {
|
|
1345
|
-
res.once(event, listener);
|
|
1346
|
-
return;
|
|
1347
|
-
}
|
|
1348
|
-
res.on?.(event, listener);
|
|
1349
|
-
}
|
|
1350
|
-
function expressIntegration(logger, options = {}) {
|
|
1351
|
-
const name = options.name ?? "express";
|
|
1352
|
-
const minStatus = options.minStatus ?? 400;
|
|
1353
|
-
const captureAll = options.captureAll ?? options.captureSuccessful ?? false;
|
|
1354
|
-
const captureAborted = options.captureAborted ?? true;
|
|
1355
|
-
const sampleRate = options.sampleRate ?? 1;
|
|
1356
|
-
const random = options.random ?? defaultRandom$4;
|
|
1357
|
-
const sanitizeUrl = options.sanitizeUrl ?? defaultSanitizeUrl$3;
|
|
1358
|
-
const levelFor = options.level ?? defaultLevel$4;
|
|
1359
|
-
const routeFor = options.getRoute ?? defaultRoute$1;
|
|
1360
|
-
const bindContext = options.bindContext ?? true;
|
|
1361
|
-
return (req, res, next) => {
|
|
1362
|
-
const started = Date.now();
|
|
1363
|
-
const method = (req.method ?? "GET").toUpperCase();
|
|
1364
|
-
const url = sanitizeUrl(requestUrl(req));
|
|
1365
|
-
const requestId = options.getRequestId?.(req, res);
|
|
1366
|
-
let completed = false;
|
|
1367
|
-
const context = {
|
|
1368
|
-
httpMethod: method,
|
|
1369
|
-
httpUrl: url,
|
|
1370
|
-
requestId,
|
|
1371
|
-
...options.context?.(req, res)
|
|
1372
|
-
};
|
|
1373
|
-
const shouldCapture = (status, aborted) => {
|
|
1374
|
-
if (aborted && captureAborted) return true;
|
|
1375
|
-
if (status >= minStatus) return true;
|
|
1376
|
-
if (!captureAll) return false;
|
|
1377
|
-
return sampleRate >= 1 || random() < sampleRate;
|
|
1378
|
-
};
|
|
1379
|
-
const cleanup = () => {
|
|
1380
|
-
removeListener(res, "finish", onFinish);
|
|
1381
|
-
removeListener(res, "close", onClose);
|
|
1382
|
-
};
|
|
1383
|
-
const logRequest = (event) => {
|
|
1384
|
-
if (completed) return;
|
|
1385
|
-
completed = true;
|
|
1386
|
-
cleanup();
|
|
1387
|
-
const status = res.statusCode ?? 0;
|
|
1388
|
-
const aborted = event === "close" && res.writableEnded !== true;
|
|
1389
|
-
if (!shouldCapture(status, aborted)) return;
|
|
1390
|
-
const http = {
|
|
1391
|
-
kind: name,
|
|
1392
|
-
framework: "express",
|
|
1393
|
-
method,
|
|
1394
|
-
url,
|
|
1395
|
-
route: routeFor(req),
|
|
1396
|
-
status,
|
|
1397
|
-
durationMs: Date.now() - started,
|
|
1398
|
-
aborted,
|
|
1399
|
-
requestId,
|
|
1400
|
-
remoteAddress: req.ip ?? req.socket?.remoteAddress,
|
|
1401
|
-
requestHeaders: pickRequestHeaders$1(req, options.captureRequestHeaders),
|
|
1402
|
-
responseHeaders: pickResponseHeaders$1(res, options.captureResponseHeaders)
|
|
1403
|
-
};
|
|
1404
|
-
logger.log(levelFor(status, req, res, aborted), `Express ${status} ${method} ${url}`, { http });
|
|
1405
|
-
};
|
|
1406
|
-
const onFinish = () => logRequest("finish");
|
|
1407
|
-
const onClose = () => logRequest("close");
|
|
1408
|
-
onResponseDone(res, "finish", onFinish);
|
|
1409
|
-
onResponseDone(res, "close", onClose);
|
|
1410
|
-
if (bindContext) {
|
|
1411
|
-
(0, _loggerjs_core.withContext)(context, () => next());
|
|
1412
|
-
return;
|
|
1413
|
-
}
|
|
1414
|
-
next();
|
|
1415
|
-
};
|
|
1416
|
-
}
|
|
1417
|
-
//#endregion
|
|
1418
|
-
//#region src/fastify-integration.ts
|
|
1419
|
-
const defaultRandom$3 = () => Math.random();
|
|
1420
|
-
function defaultSanitizeUrl$2(url) {
|
|
1421
|
-
const queryIndex = url.indexOf("?");
|
|
1422
|
-
const hashIndex = url.indexOf("#");
|
|
1423
|
-
const end = Math.min(queryIndex >= 0 ? queryIndex : url.length, hashIndex >= 0 ? hashIndex : url.length);
|
|
1424
|
-
return url.slice(0, end);
|
|
1425
|
-
}
|
|
1426
|
-
function defaultLevel$3(status, _request, _reply, error) {
|
|
1427
|
-
if (error || status >= 500) return "error";
|
|
1428
|
-
if (status >= 400) return "warn";
|
|
1429
|
-
return "info";
|
|
1430
|
-
}
|
|
1431
|
-
function headerValue$2(value) {
|
|
1432
|
-
if (typeof value === "number") return String(value);
|
|
1433
|
-
return value;
|
|
1434
|
-
}
|
|
1435
|
-
function pickRequestHeaders(request, names) {
|
|
1436
|
-
if (!names || names.length === 0 || !request.headers) return void 0;
|
|
1437
|
-
const out = {};
|
|
1438
|
-
for (const name of names) {
|
|
1439
|
-
const value = request.headers[name.toLowerCase()] ?? request.headers[name];
|
|
1440
|
-
if (value !== void 0) out[name.toLowerCase()] = value;
|
|
1441
|
-
}
|
|
1442
|
-
return Object.keys(out).length > 0 ? out : void 0;
|
|
1443
|
-
}
|
|
1444
|
-
function pickResponseHeaders(reply, names) {
|
|
1445
|
-
if (!names || names.length === 0 || !reply.getHeader) return void 0;
|
|
1446
|
-
const out = {};
|
|
1447
|
-
for (const name of names) {
|
|
1448
|
-
const value = headerValue$2(reply.getHeader(name));
|
|
1449
|
-
if (value !== void 0) out[name.toLowerCase()] = value;
|
|
1450
|
-
}
|
|
1451
|
-
return Object.keys(out).length > 0 ? out : void 0;
|
|
1452
|
-
}
|
|
1453
|
-
function defaultRoute(request) {
|
|
1454
|
-
return request.routeOptions?.url ?? request.routerPath;
|
|
1455
|
-
}
|
|
1456
|
-
function fastifyIntegration(logger, options = {}) {
|
|
1457
|
-
const name = options.name ?? "fastify";
|
|
1458
|
-
const minStatus = options.minStatus ?? 400;
|
|
1459
|
-
const captureAll = options.captureAll ?? options.captureSuccessful ?? false;
|
|
1460
|
-
const sampleRate = options.sampleRate ?? 1;
|
|
1461
|
-
const random = options.random ?? defaultRandom$3;
|
|
1462
|
-
const sanitizeUrl = options.sanitizeUrl ?? defaultSanitizeUrl$2;
|
|
1463
|
-
const levelFor = options.level ?? defaultLevel$3;
|
|
1464
|
-
const routeFor = options.getRoute ?? defaultRoute;
|
|
1465
|
-
const bindContext = options.bindContext ?? true;
|
|
1466
|
-
const states = /* @__PURE__ */ new WeakMap();
|
|
1467
|
-
const shouldCapture = (status, error) => {
|
|
1468
|
-
if (error) return true;
|
|
1469
|
-
if (status >= minStatus) return true;
|
|
1470
|
-
if (!captureAll) return false;
|
|
1471
|
-
return sampleRate >= 1 || random() < sampleRate;
|
|
1472
|
-
};
|
|
1473
|
-
return (instance, _pluginOptions, done) => {
|
|
1474
|
-
instance.addHook("onRequest", (request, reply, next) => {
|
|
1475
|
-
const method = (request.method ?? "GET").toUpperCase();
|
|
1476
|
-
const url = sanitizeUrl(request.url ?? "/");
|
|
1477
|
-
const requestId = options.getRequestId?.(request, reply) ?? request.id;
|
|
1478
|
-
states.set(request, { started: Date.now() });
|
|
1479
|
-
const context = {
|
|
1480
|
-
httpMethod: method,
|
|
1481
|
-
httpUrl: url,
|
|
1482
|
-
requestId,
|
|
1483
|
-
...options.context?.(request, reply)
|
|
1484
|
-
};
|
|
1485
|
-
if (bindContext) {
|
|
1486
|
-
(0, _loggerjs_core.withContext)(context, () => next());
|
|
1487
|
-
return;
|
|
1488
|
-
}
|
|
1489
|
-
next();
|
|
1490
|
-
});
|
|
1491
|
-
instance.addHook("onError", (request, _reply, error, next) => {
|
|
1492
|
-
const state = states.get(request);
|
|
1493
|
-
if (state) state.error = error;
|
|
1494
|
-
else states.set(request, {
|
|
1495
|
-
started: Date.now(),
|
|
1496
|
-
error
|
|
1497
|
-
});
|
|
1498
|
-
next();
|
|
1499
|
-
});
|
|
1500
|
-
instance.addHook("onResponse", (request, reply, next) => {
|
|
1501
|
-
const state = states.get(request) ?? { started: Date.now() };
|
|
1502
|
-
states.delete(request);
|
|
1503
|
-
const status = reply.statusCode ?? 0;
|
|
1504
|
-
const error = state.error;
|
|
1505
|
-
if (!shouldCapture(status, error)) {
|
|
1506
|
-
next();
|
|
1507
|
-
return;
|
|
1508
|
-
}
|
|
1509
|
-
const method = (request.method ?? "GET").toUpperCase();
|
|
1510
|
-
const url = sanitizeUrl(request.url ?? "/");
|
|
1511
|
-
const requestId = options.getRequestId?.(request, reply) ?? request.id;
|
|
1512
|
-
const data = { http: {
|
|
1513
|
-
kind: name,
|
|
1514
|
-
framework: "fastify",
|
|
1515
|
-
method,
|
|
1516
|
-
url,
|
|
1517
|
-
route: routeFor(request),
|
|
1518
|
-
status,
|
|
1519
|
-
durationMs: Date.now() - state.started,
|
|
1520
|
-
requestId,
|
|
1521
|
-
remoteAddress: request.ip,
|
|
1522
|
-
requestHeaders: pickRequestHeaders(request, options.captureRequestHeaders),
|
|
1523
|
-
responseHeaders: pickResponseHeaders(reply, options.captureResponseHeaders)
|
|
1524
|
-
} };
|
|
1525
|
-
if (error) data.error = error;
|
|
1526
|
-
logger.log(levelFor(status, request, reply, error), `Fastify ${status} ${method} ${url}`, { ...data });
|
|
1527
|
-
next();
|
|
1528
|
-
});
|
|
1529
|
-
done?.();
|
|
1530
|
-
};
|
|
1531
|
-
}
|
|
1532
|
-
//#endregion
|
|
1533
|
-
//#region src/hapi-integration.ts
|
|
1534
|
-
function responseStatus(response) {
|
|
1535
|
-
if (!response) return 200;
|
|
1536
|
-
if ("statusCode" in response && typeof response.statusCode === "number") return response.statusCode;
|
|
1537
|
-
if ("output" in response && typeof response.output?.statusCode === "number") return response.output.statusCode;
|
|
1538
|
-
return 200;
|
|
1539
|
-
}
|
|
1540
|
-
function levelFor$1(status) {
|
|
1541
|
-
if (status >= 500) return "error";
|
|
1542
|
-
if (status >= 400) return "warn";
|
|
1543
|
-
return "info";
|
|
1544
|
-
}
|
|
1545
|
-
function hapiIntegration(logger, options = {}) {
|
|
1546
|
-
const minStatus = options.minStatus ?? 400;
|
|
1547
|
-
const captureAll = options.captureAll ?? false;
|
|
1548
|
-
const bindContext = options.bindContext ?? true;
|
|
1549
|
-
const getLevel = options.level ?? levelFor$1;
|
|
1550
|
-
return {
|
|
1551
|
-
name: "loggerjs-hapi",
|
|
1552
|
-
register(server) {
|
|
1553
|
-
server.ext("onRequest", (request, h) => {
|
|
1554
|
-
const context = {
|
|
1555
|
-
httpMethod: (request.method ?? "GET").toUpperCase(),
|
|
1556
|
-
httpUrl: request.url?.pathname ?? request.path ?? "/",
|
|
1557
|
-
requestId: options.getRequestId?.(request) ?? request.info?.id,
|
|
1558
|
-
...options.context?.(request)
|
|
1559
|
-
};
|
|
1560
|
-
request.app = {
|
|
1561
|
-
...request.app,
|
|
1562
|
-
loggerjsStartedAt: Date.now()
|
|
1563
|
-
};
|
|
1564
|
-
if (bindContext) return (0, _loggerjs_core.withContext)(context, () => h.continue);
|
|
1565
|
-
return h.continue;
|
|
1566
|
-
});
|
|
1567
|
-
server.events?.on?.("response", (request) => {
|
|
1568
|
-
const status = responseStatus(request.response);
|
|
1569
|
-
if (!captureAll && status < minStatus) return;
|
|
1570
|
-
const method = (request.method ?? "GET").toUpperCase();
|
|
1571
|
-
const url = request.url?.pathname ?? request.path ?? "/";
|
|
1572
|
-
const requestId = options.getRequestId?.(request) ?? request.info?.id;
|
|
1573
|
-
const started = request.app?.loggerjsStartedAt;
|
|
1574
|
-
logger.log(getLevel(status, request), `Hapi ${status} ${method} ${url}`, { http: {
|
|
1575
|
-
framework: "hapi",
|
|
1576
|
-
kind: "hapi",
|
|
1577
|
-
method,
|
|
1578
|
-
url,
|
|
1579
|
-
status,
|
|
1580
|
-
durationMs: started ? Date.now() - started : void 0,
|
|
1581
|
-
requestId,
|
|
1582
|
-
remoteAddress: request.info?.remoteAddress
|
|
1583
|
-
} });
|
|
1584
|
-
});
|
|
1585
|
-
}
|
|
1586
|
-
};
|
|
1587
|
-
}
|
|
1588
|
-
//#endregion
|
|
1589
|
-
//#region src/koa-integration.ts
|
|
1590
|
-
function sanitizeUrl(url) {
|
|
1591
|
-
return url.split(/[?#]/, 1)[0] || "/";
|
|
1592
|
-
}
|
|
1593
|
-
function levelFor(status, _ctx, error) {
|
|
1594
|
-
if (error || status >= 500) return "error";
|
|
1595
|
-
if (status >= 400) return "warn";
|
|
1596
|
-
return "info";
|
|
1597
|
-
}
|
|
1598
|
-
function koaIntegration(logger, options = {}) {
|
|
1599
|
-
const minStatus = options.minStatus ?? 400;
|
|
1600
|
-
const captureAll = options.captureAll ?? false;
|
|
1601
|
-
const bindContext = options.bindContext ?? true;
|
|
1602
|
-
const sanitize = options.sanitizeUrl ?? sanitizeUrl;
|
|
1603
|
-
const getLevel = options.level ?? levelFor;
|
|
1604
|
-
return async (ctx, next) => {
|
|
1605
|
-
const started = Date.now();
|
|
1606
|
-
const method = (ctx.method ?? "GET").toUpperCase();
|
|
1607
|
-
const url = sanitize(ctx.originalUrl ?? ctx.url ?? ctx.path ?? "/");
|
|
1608
|
-
const requestId = options.getRequestId?.(ctx) ?? ctx.state?.requestId;
|
|
1609
|
-
const context = {
|
|
1610
|
-
httpMethod: method,
|
|
1611
|
-
httpUrl: url,
|
|
1612
|
-
requestId,
|
|
1613
|
-
...options.context?.(ctx)
|
|
1614
|
-
};
|
|
1615
|
-
let error;
|
|
1616
|
-
try {
|
|
1617
|
-
if (bindContext) await (0, _loggerjs_core.withContext)(context, next);
|
|
1618
|
-
else await next();
|
|
1619
|
-
} catch (caught) {
|
|
1620
|
-
error = caught;
|
|
1621
|
-
throw caught;
|
|
1622
|
-
} finally {
|
|
1623
|
-
const status = ctx.status ?? (error ? 500 : 200);
|
|
1624
|
-
if (error || captureAll || status >= minStatus) logger.log(getLevel(status, ctx, error), `Koa ${status} ${method} ${url}`, {
|
|
1625
|
-
http: {
|
|
1626
|
-
framework: "koa",
|
|
1627
|
-
kind: "koa",
|
|
1628
|
-
method,
|
|
1629
|
-
url,
|
|
1630
|
-
status,
|
|
1631
|
-
durationMs: Date.now() - started,
|
|
1632
|
-
requestId,
|
|
1633
|
-
remoteAddress: ctx.ip
|
|
1634
|
-
},
|
|
1635
|
-
error
|
|
1636
|
-
});
|
|
1637
|
-
}
|
|
1638
|
-
};
|
|
1639
|
-
}
|
|
1640
|
-
//#endregion
|
|
1641
|
-
//#region src/nest-integration.ts
|
|
1642
|
-
/**
|
|
1643
|
-
* Express-compatible Nest middleware adapter. It observes the same request and
|
|
1644
|
-
* response completion surface as `expressIntegration`; it does not hook Nest
|
|
1645
|
-
* exception filters, interceptors, guards, or the original thrown `Error`.
|
|
1646
|
-
*/
|
|
1647
|
-
function nestMiddlewareIntegration(logger, options = {}) {
|
|
1648
|
-
return expressIntegration(logger, {
|
|
1649
|
-
name: "nestjs",
|
|
1650
|
-
...options
|
|
1651
|
-
});
|
|
1652
|
-
}
|
|
1653
|
-
//#endregion
|
|
1654
|
-
//#region src/node-fetch-integration.ts
|
|
1655
|
-
const defaultRandom$2 = () => Math.random();
|
|
1656
|
-
function defaultSanitizeUrl$1(url) {
|
|
1657
|
-
const queryIndex = url.indexOf("?");
|
|
1658
|
-
const hashIndex = url.indexOf("#");
|
|
1659
|
-
const end = Math.min(queryIndex >= 0 ? queryIndex : url.length, hashIndex >= 0 ? hashIndex : url.length);
|
|
1660
|
-
return url.slice(0, end);
|
|
1661
|
-
}
|
|
1662
|
-
function defaultLevel$2(status, error) {
|
|
1663
|
-
if (error || status === void 0 || status >= 500) return "error";
|
|
1664
|
-
if (status >= 400) return "warn";
|
|
1665
|
-
return "info";
|
|
1666
|
-
}
|
|
1667
|
-
function isUrlLike$1(value) {
|
|
1668
|
-
return typeof URL !== "undefined" && value instanceof URL;
|
|
1669
|
-
}
|
|
1670
|
-
function isObject(value) {
|
|
1671
|
-
return typeof value === "object" && value !== null;
|
|
1672
|
-
}
|
|
1673
|
-
function headerValue$1(headers, name) {
|
|
1674
|
-
if (!isObject(headers)) return void 0;
|
|
1675
|
-
if (typeof headers.get === "function") return headers.get?.(name) ?? void 0;
|
|
1676
|
-
const value = headers[name.toLowerCase()] ?? headers[name];
|
|
1677
|
-
return value === void 0 ? void 0 : String(value);
|
|
1678
|
-
}
|
|
1679
|
-
function pickHeaders$1(headers, names) {
|
|
1680
|
-
if (!names || names.length === 0) return void 0;
|
|
1681
|
-
const out = {};
|
|
1682
|
-
for (const name of names) {
|
|
1683
|
-
const lowerName = name.toLowerCase();
|
|
1684
|
-
const value = headerValue$1(headers, lowerName) ?? headerValue$1(headers, name);
|
|
1685
|
-
if (value !== void 0) out[lowerName] = value;
|
|
1686
|
-
}
|
|
1687
|
-
return Object.keys(out).length > 0 ? out : void 0;
|
|
1688
|
-
}
|
|
1689
|
-
function requestInfo$1(input, init, sanitizeUrl, captureRequestHeaders) {
|
|
1690
|
-
const request = isObject(input) && !isUrlLike$1(input) ? input : void 0;
|
|
1691
|
-
return {
|
|
1692
|
-
method: (init?.method ?? request?.method ?? "GET").toUpperCase(),
|
|
1693
|
-
url: sanitizeUrl(isUrlLike$1(input) ? input.href : typeof input === "string" ? input : request?.url ?? "/"),
|
|
1694
|
-
requestHeaders: pickHeaders$1(init?.headers ?? request?.headers, captureRequestHeaders)
|
|
1695
|
-
};
|
|
1696
|
-
}
|
|
1697
|
-
function statusMessage$1(status) {
|
|
1698
|
-
return status === void 0 ? "unknown" : String(status);
|
|
1699
|
-
}
|
|
1700
|
-
function nodeFetchIntegration(options = {}) {
|
|
1701
|
-
const name = options.name ?? "node-fetch";
|
|
1702
|
-
const minStatus = options.minStatus ?? 400;
|
|
1703
|
-
const captureAll = options.captureAll ?? options.captureSuccessful ?? false;
|
|
1704
|
-
const sampleRate = options.sampleRate ?? 1;
|
|
1705
|
-
const random = options.random ?? defaultRandom$2;
|
|
1706
|
-
const sanitizeUrl = options.sanitizeUrl ?? defaultSanitizeUrl$1;
|
|
1707
|
-
const levelFor = options.level ?? defaultLevel$2;
|
|
1708
|
-
const shouldCapture = (status, error) => {
|
|
1709
|
-
if (error) return true;
|
|
1710
|
-
if (status !== void 0 && status >= minStatus) return true;
|
|
1711
|
-
if (!captureAll) return false;
|
|
1712
|
-
return sampleRate >= 1 || random() < sampleRate;
|
|
1713
|
-
};
|
|
1714
|
-
return {
|
|
1715
|
-
name: "node-fetch",
|
|
1716
|
-
setup(api) {
|
|
1717
|
-
const target = options.target ?? globalThis;
|
|
1718
|
-
if (!target.fetch) return;
|
|
1719
|
-
const originalFetch = target.fetch;
|
|
1720
|
-
const capture = api.guard((input) => api.capture(input));
|
|
1721
|
-
let disposed = false;
|
|
1722
|
-
const captureResult = (info, durationMs, status, error, responseHeaders) => {
|
|
1723
|
-
if (disposed || !shouldCapture(status, error)) return;
|
|
1724
|
-
capture({
|
|
1725
|
-
level: levelFor(status, error, info),
|
|
1726
|
-
message: error ? `Fetch error ${info.method} ${info.url}` : `Fetch ${statusMessage$1(status)} ${info.method} ${info.url}`,
|
|
1727
|
-
error,
|
|
1728
|
-
props: { http: {
|
|
1729
|
-
kind: name,
|
|
1730
|
-
runtime: "node",
|
|
1731
|
-
instrument: "fetch",
|
|
1732
|
-
direction: "outgoing",
|
|
1733
|
-
method: info.method,
|
|
1734
|
-
url: info.url,
|
|
1735
|
-
status,
|
|
1736
|
-
durationMs,
|
|
1737
|
-
requestHeaders: info.requestHeaders,
|
|
1738
|
-
responseHeaders
|
|
1739
|
-
} }
|
|
1740
|
-
});
|
|
1741
|
-
};
|
|
1742
|
-
target.fetch = async function patchedFetch(input, init) {
|
|
1743
|
-
const started = Date.now();
|
|
1744
|
-
const info = requestInfo$1(input, init, sanitizeUrl, options.captureRequestHeaders);
|
|
1745
|
-
try {
|
|
1746
|
-
const response = await originalFetch.call(this, input, init);
|
|
1747
|
-
captureResult(info, Date.now() - started, response.status, void 0, pickHeaders$1(response.headers, options.captureResponseHeaders));
|
|
1748
|
-
return response;
|
|
1749
|
-
} catch (error) {
|
|
1750
|
-
captureResult(info, Date.now() - started, void 0, error);
|
|
1751
|
-
throw error;
|
|
1752
|
-
}
|
|
1753
|
-
};
|
|
1754
|
-
return () => {
|
|
1755
|
-
if (disposed) return;
|
|
1756
|
-
disposed = true;
|
|
1757
|
-
target.fetch = originalFetch;
|
|
1758
|
-
};
|
|
1759
|
-
}
|
|
1760
|
-
};
|
|
1761
|
-
}
|
|
1762
|
-
//#endregion
|
|
1763
|
-
//#region src/node-http-client-integration.ts
|
|
1764
|
-
const requireBuiltin$2 = (0, node_module.createRequire)(require("url").pathToFileURL(__filename).href);
|
|
1765
|
-
const defaultRandom$1 = () => Math.random();
|
|
1766
|
-
let cachedHttpModule;
|
|
1767
|
-
let cachedHttpsModule;
|
|
1768
|
-
function loadBuiltin(name, fallback) {
|
|
1769
|
-
try {
|
|
1770
|
-
return requireBuiltin$2(name);
|
|
1771
|
-
} catch {
|
|
1772
|
-
try {
|
|
1773
|
-
return requireBuiltin$2(fallback);
|
|
1774
|
-
} catch {
|
|
1775
|
-
return null;
|
|
1776
|
-
}
|
|
1777
|
-
}
|
|
1778
|
-
}
|
|
1779
|
-
function loadHttpModule() {
|
|
1780
|
-
if (cachedHttpModule !== void 0) return cachedHttpModule;
|
|
1781
|
-
cachedHttpModule = loadBuiltin("node:http", "http");
|
|
1782
|
-
return cachedHttpModule;
|
|
1783
|
-
}
|
|
1784
|
-
function loadHttpsModule() {
|
|
1785
|
-
if (cachedHttpsModule !== void 0) return cachedHttpsModule;
|
|
1786
|
-
cachedHttpsModule = loadBuiltin("node:https", "https");
|
|
1787
|
-
return cachedHttpsModule;
|
|
1788
|
-
}
|
|
1789
|
-
function defaultSanitizeUrl(url) {
|
|
1790
|
-
const queryIndex = url.indexOf("?");
|
|
1791
|
-
const hashIndex = url.indexOf("#");
|
|
1792
|
-
const end = Math.min(queryIndex >= 0 ? queryIndex : url.length, hashIndex >= 0 ? hashIndex : url.length);
|
|
1793
|
-
return url.slice(0, end);
|
|
1794
|
-
}
|
|
1795
|
-
function defaultLevel$1(status, error) {
|
|
1796
|
-
if (error || status === void 0 || status >= 500) return "error";
|
|
1797
|
-
if (status >= 400) return "warn";
|
|
1798
|
-
return "info";
|
|
1799
|
-
}
|
|
1800
|
-
function isUrlLike(value) {
|
|
1801
|
-
return typeof URL !== "undefined" && value instanceof URL;
|
|
1802
|
-
}
|
|
1803
|
-
function isOptionsObject(value) {
|
|
1804
|
-
return typeof value === "object" && value !== null && !isUrlLike(value);
|
|
1805
|
-
}
|
|
1806
|
-
function headerValue(value) {
|
|
1807
|
-
if (value === void 0) return void 0;
|
|
1808
|
-
if (Array.isArray(value)) return value.map(String);
|
|
1809
|
-
return String(value);
|
|
1810
|
-
}
|
|
1811
|
-
function pickHeaders(headers, names) {
|
|
1812
|
-
if (!names || names.length === 0 || !isOptionsObject(headers)) return void 0;
|
|
1813
|
-
const out = {};
|
|
1814
|
-
for (const name of names) {
|
|
1815
|
-
const lowerName = name.toLowerCase();
|
|
1816
|
-
const value = headerValue(headers[lowerName] ?? headers[name]);
|
|
1817
|
-
if (value !== void 0) out[lowerName] = value;
|
|
1818
|
-
}
|
|
1819
|
-
return Object.keys(out).length > 0 ? out : void 0;
|
|
1820
|
-
}
|
|
1821
|
-
function maybeUrl(value) {
|
|
1822
|
-
if (isUrlLike(value)) return value;
|
|
1823
|
-
if (typeof value !== "string") return void 0;
|
|
1824
|
-
try {
|
|
1825
|
-
return new URL(value);
|
|
1826
|
-
} catch {
|
|
1827
|
-
return;
|
|
1828
|
-
}
|
|
1829
|
-
}
|
|
1830
|
-
function pathFromUrl(url) {
|
|
1831
|
-
if (!url) return "/";
|
|
1832
|
-
return `${url.pathname || "/"}${url.search}`;
|
|
1833
|
-
}
|
|
1834
|
-
function optionString(options, key) {
|
|
1835
|
-
const value = options[key];
|
|
1836
|
-
if (value === void 0) return void 0;
|
|
1837
|
-
return String(value);
|
|
1838
|
-
}
|
|
1839
|
-
function requestInfo(args, defaultProtocol, sanitizeUrl, captureRequestHeaders) {
|
|
1840
|
-
const first = args[0];
|
|
1841
|
-
const second = args[1];
|
|
1842
|
-
const parsed = maybeUrl(first);
|
|
1843
|
-
const options = isOptionsObject(first) ? first : isOptionsObject(second) ? second : void 0;
|
|
1844
|
-
const protocol = optionString(options ?? {}, "protocol") ?? parsed?.protocol ?? defaultProtocol;
|
|
1845
|
-
const method = (optionString(options ?? {}, "method") ?? "GET").toUpperCase();
|
|
1846
|
-
const host = optionString(options ?? {}, "host") ?? optionString(options ?? {}, "hostname") ?? parsed?.host;
|
|
1847
|
-
const port = optionString(options ?? {}, "port");
|
|
1848
|
-
const path = optionString(options ?? {}, "path") ?? pathFromUrl(parsed);
|
|
1849
|
-
const requestHeaders = pickHeaders(options?.headers, captureRequestHeaders);
|
|
1850
|
-
return {
|
|
1851
|
-
protocol,
|
|
1852
|
-
method,
|
|
1853
|
-
url: sanitizeUrl(parsed?.href ?? (host ? `${protocol}//${host}${port ? `:${port}` : ""}${path}` : path)),
|
|
1854
|
-
requestHeaders
|
|
1855
|
-
};
|
|
1856
|
-
}
|
|
1857
|
-
function listenOnce(request, event, listener) {
|
|
1858
|
-
let active = true;
|
|
1859
|
-
const wrapped = (...args) => {
|
|
1860
|
-
if (!active) return;
|
|
1861
|
-
active = false;
|
|
1862
|
-
cleanup();
|
|
1863
|
-
listener(...args);
|
|
1864
|
-
};
|
|
1865
|
-
const cleanup = () => {
|
|
1866
|
-
request.off?.(event, wrapped);
|
|
1867
|
-
request.removeListener?.(event, wrapped);
|
|
1868
|
-
};
|
|
1869
|
-
if (request.once) request.once(event, wrapped);
|
|
1870
|
-
else request.on?.(event, wrapped);
|
|
1871
|
-
}
|
|
1872
|
-
function statusMessage(status) {
|
|
1873
|
-
return status === void 0 ? "unknown" : String(status);
|
|
1874
|
-
}
|
|
1875
|
-
function nodeHttpClientIntegration(options = {}) {
|
|
1876
|
-
const name = options.name ?? "node-http-client";
|
|
1877
|
-
const minStatus = options.minStatus ?? 400;
|
|
1878
|
-
const captureAll = options.captureAll ?? options.captureSuccessful ?? false;
|
|
1879
|
-
const sampleRate = options.sampleRate ?? 1;
|
|
1880
|
-
const random = options.random ?? defaultRandom$1;
|
|
1881
|
-
const sanitizeUrl = options.sanitizeUrl ?? defaultSanitizeUrl;
|
|
1882
|
-
const levelFor = options.level ?? defaultLevel$1;
|
|
1883
|
-
const shouldCapture = (status, error) => {
|
|
1884
|
-
if (error) return true;
|
|
1885
|
-
if (status !== void 0 && status >= minStatus) return true;
|
|
1886
|
-
if (!captureAll) return false;
|
|
1887
|
-
return sampleRate >= 1 || random() < sampleRate;
|
|
1888
|
-
};
|
|
1889
|
-
return {
|
|
1890
|
-
name: "node-http-client",
|
|
1891
|
-
setup(api) {
|
|
1892
|
-
const httpModule = options.httpModule === void 0 ? loadHttpModule() : options.httpModule;
|
|
1893
|
-
const httpsModule = options.httpsModule === void 0 ? loadHttpsModule() : options.httpsModule;
|
|
1894
|
-
const capture = api.guard((input) => api.capture(input));
|
|
1895
|
-
const disposers = [];
|
|
1896
|
-
let disposed = false;
|
|
1897
|
-
const captureResult = (info, status, error, responseHeaders) => {
|
|
1898
|
-
if (disposed || !shouldCapture(status, error)) return;
|
|
1899
|
-
capture({
|
|
1900
|
-
level: levelFor(status, error, info),
|
|
1901
|
-
message: error ? `HTTP error ${info.method} ${info.url}` : `HTTP ${statusMessage(status)} ${info.method} ${info.url}`,
|
|
1902
|
-
error,
|
|
1903
|
-
props: { http: {
|
|
1904
|
-
kind: name,
|
|
1905
|
-
runtime: "node",
|
|
1906
|
-
direction: "outgoing",
|
|
1907
|
-
protocol: info.protocol,
|
|
1908
|
-
method: info.method,
|
|
1909
|
-
url: info.url,
|
|
1910
|
-
status,
|
|
1911
|
-
requestHeaders: info.requestHeaders,
|
|
1912
|
-
responseHeaders
|
|
1913
|
-
} }
|
|
1914
|
-
});
|
|
1915
|
-
};
|
|
1916
|
-
const patchModule = (module, defaultProtocol) => {
|
|
1917
|
-
if (!module?.request) return;
|
|
1918
|
-
const originalRequest = module.request;
|
|
1919
|
-
const originalGet = module.get;
|
|
1920
|
-
module.request = function patchedRequest(...args) {
|
|
1921
|
-
const info = requestInfo(args, defaultProtocol, sanitizeUrl, options.captureRequestHeaders);
|
|
1922
|
-
const request = originalRequest.apply(this, args);
|
|
1923
|
-
listenOnce(request, "response", (message) => {
|
|
1924
|
-
const response = message;
|
|
1925
|
-
captureResult(info, response.statusCode, void 0, pickHeaders(response.headers, options.captureResponseHeaders));
|
|
1926
|
-
});
|
|
1927
|
-
listenOnce(request, "error", (error) => captureResult(info, void 0, error));
|
|
1928
|
-
return request;
|
|
1929
|
-
};
|
|
1930
|
-
if (originalGet) module.get = function patchedGet(...args) {
|
|
1931
|
-
const request = module.request?.apply(this, args) ?? originalGet.apply(this, args);
|
|
1932
|
-
request.end?.();
|
|
1933
|
-
return request;
|
|
1934
|
-
};
|
|
1935
|
-
disposers.push(() => {
|
|
1936
|
-
module.request = originalRequest;
|
|
1937
|
-
if (originalGet) module.get = originalGet;
|
|
1938
|
-
});
|
|
1939
|
-
};
|
|
1940
|
-
patchModule(httpModule, "http:");
|
|
1941
|
-
patchModule(httpsModule, "https:");
|
|
1942
|
-
return () => {
|
|
1943
|
-
if (disposed) return;
|
|
1944
|
-
disposed = true;
|
|
1945
|
-
for (const dispose of disposers) dispose();
|
|
1946
|
-
};
|
|
1947
|
-
}
|
|
1948
|
-
};
|
|
1949
|
-
}
|
|
1950
|
-
//#endregion
|
|
1951
|
-
//#region src/process-integration.ts
|
|
1952
|
-
const signalExitCodes = {
|
|
1953
|
-
SIGINT: 130,
|
|
1954
|
-
SIGTERM: 143
|
|
1955
|
-
};
|
|
1956
|
-
function captureProcessIntegration(options = {}) {
|
|
1957
|
-
const uncaughtException = options.uncaughtException ?? true;
|
|
1958
|
-
const unhandledRejection = options.unhandledRejection ?? true;
|
|
1959
|
-
const warning = options.warning ?? true;
|
|
1960
|
-
const beforeExitFlush = options.beforeExitFlush ?? true;
|
|
1961
|
-
const exitFlush = options.exitFlush ?? true;
|
|
1962
|
-
const signalFlush = options.signalFlush ?? true;
|
|
1963
|
-
const signals = options.signals ?? ["SIGTERM"];
|
|
1964
|
-
const exitOnSignal = options.exitOnSignal ?? true;
|
|
1965
|
-
const exitOnUncaught = options.exitOnUncaught ?? true;
|
|
1966
|
-
const flushTimeoutMs = options.flushTimeoutMs ?? 250;
|
|
1967
|
-
const exitFn = options.exitFn ?? ((code) => process.exit(code));
|
|
1968
|
-
return {
|
|
1969
|
-
name: "capture-process",
|
|
1970
|
-
setup(api) {
|
|
1971
|
-
const disposers = [];
|
|
1972
|
-
let disposed = false;
|
|
1973
|
-
const capture = api.guard((input) => api.capture(input));
|
|
1974
|
-
const flushSync = () => {
|
|
1975
|
-
try {
|
|
1976
|
-
api.flushSync?.();
|
|
1977
|
-
} catch {}
|
|
1978
|
-
};
|
|
1979
|
-
const flushBounded = async () => {
|
|
1980
|
-
let timer;
|
|
1981
|
-
try {
|
|
1982
|
-
await Promise.race([api.flush(), new Promise((resolve) => {
|
|
1983
|
-
timer = setTimeout(resolve, flushTimeoutMs);
|
|
1984
|
-
})]);
|
|
1985
|
-
} catch {} finally {
|
|
1986
|
-
if (timer) clearTimeout(timer);
|
|
1987
|
-
}
|
|
1988
|
-
};
|
|
1989
|
-
const flushBestEffort = () => {
|
|
1990
|
-
flushSync();
|
|
1991
|
-
flushBounded();
|
|
1992
|
-
};
|
|
1993
|
-
if (uncaughtException) {
|
|
1994
|
-
const onUncaughtException = (error) => {
|
|
1995
|
-
capture({
|
|
1996
|
-
level: "fatal",
|
|
1997
|
-
message: error.message,
|
|
1998
|
-
error,
|
|
1999
|
-
props: { process: { kind: "uncaughtException" } }
|
|
2000
|
-
});
|
|
2001
|
-
flushSync();
|
|
2002
|
-
const flushed = flushBounded();
|
|
2003
|
-
if (exitOnUncaught) flushed.finally(() => exitFn(1));
|
|
2004
|
-
};
|
|
2005
|
-
process.on("uncaughtException", onUncaughtException);
|
|
2006
|
-
disposers.push(() => process.off("uncaughtException", onUncaughtException));
|
|
2007
|
-
}
|
|
2008
|
-
if (unhandledRejection) {
|
|
2009
|
-
const onUnhandledRejection = (reason) => {
|
|
2010
|
-
capture({
|
|
2011
|
-
level: "error",
|
|
2012
|
-
message: reason instanceof Error ? reason.message : "Unhandled promise rejection",
|
|
2013
|
-
error: reason,
|
|
2014
|
-
props: {
|
|
2015
|
-
process: { kind: "unhandledRejection" },
|
|
2016
|
-
reason: (0, _loggerjs_core.normalizeValue)(reason, { maxDepth: 5 })
|
|
2017
|
-
}
|
|
2018
|
-
});
|
|
2019
|
-
flushBestEffort();
|
|
2020
|
-
};
|
|
2021
|
-
process.on("unhandledRejection", onUnhandledRejection);
|
|
2022
|
-
disposers.push(() => process.off("unhandledRejection", onUnhandledRejection));
|
|
2023
|
-
}
|
|
2024
|
-
if (warning) {
|
|
2025
|
-
const onWarning = (warn) => {
|
|
2026
|
-
capture({
|
|
2027
|
-
level: "warn",
|
|
2028
|
-
message: warn.message,
|
|
2029
|
-
error: warn,
|
|
2030
|
-
props: { process: {
|
|
2031
|
-
kind: "warning",
|
|
2032
|
-
name: warn.name,
|
|
2033
|
-
code: warn.code
|
|
2034
|
-
} }
|
|
2035
|
-
});
|
|
2036
|
-
};
|
|
2037
|
-
process.on("warning", onWarning);
|
|
2038
|
-
disposers.push(() => process.off("warning", onWarning));
|
|
2039
|
-
}
|
|
2040
|
-
if (beforeExitFlush) {
|
|
2041
|
-
const onBeforeExit = () => {
|
|
2042
|
-
flushBounded();
|
|
2043
|
-
};
|
|
2044
|
-
process.on("beforeExit", onBeforeExit);
|
|
2045
|
-
disposers.push(() => process.off("beforeExit", onBeforeExit));
|
|
2046
|
-
}
|
|
2047
|
-
if (exitFlush) {
|
|
2048
|
-
const onExit = () => {
|
|
2049
|
-
flushSync();
|
|
2050
|
-
};
|
|
2051
|
-
process.on("exit", onExit);
|
|
2052
|
-
disposers.push(() => process.off("exit", onExit));
|
|
2053
|
-
}
|
|
2054
|
-
if (signalFlush) for (const signal of signals) {
|
|
2055
|
-
const onSignal = () => {
|
|
2056
|
-
capture({
|
|
2057
|
-
level: "fatal",
|
|
2058
|
-
message: `Process signal ${signal}`,
|
|
2059
|
-
props: { process: {
|
|
2060
|
-
kind: "signal",
|
|
2061
|
-
signal
|
|
2062
|
-
} }
|
|
2063
|
-
});
|
|
2064
|
-
flushSync();
|
|
2065
|
-
const flushed = flushBounded();
|
|
2066
|
-
if (exitOnSignal) flushed.finally(() => exitFn(signalExitCodes[signal] ?? 1));
|
|
2067
|
-
};
|
|
2068
|
-
process.on(signal, onSignal);
|
|
2069
|
-
disposers.push(() => process.off(signal, onSignal));
|
|
2070
|
-
}
|
|
2071
|
-
return () => {
|
|
2072
|
-
if (disposed) return;
|
|
2073
|
-
disposed = true;
|
|
2074
|
-
for (const dispose of disposers) dispose();
|
|
2075
|
-
};
|
|
2076
|
-
}
|
|
2077
|
-
};
|
|
2078
|
-
}
|
|
2079
|
-
//#endregion
|
|
2080
|
-
//#region src/prisma-integration.ts
|
|
2081
|
-
const prismaMethods = [
|
|
2082
|
-
"$executeRaw",
|
|
2083
|
-
"$executeRawUnsafe",
|
|
2084
|
-
"$queryRaw",
|
|
2085
|
-
"$queryRawUnsafe"
|
|
2086
|
-
];
|
|
2087
|
-
function prismaStatement(args) {
|
|
2088
|
-
const first = args[0];
|
|
2089
|
-
if (typeof first === "string") return first;
|
|
2090
|
-
if (Array.isArray(first) && "raw" in first) return first.raw?.join("?");
|
|
2091
|
-
}
|
|
2092
|
-
function prismaIntegration(options) {
|
|
2093
|
-
return databaseIntegration({
|
|
2094
|
-
...options,
|
|
2095
|
-
name: options.name ?? "prisma",
|
|
2096
|
-
system: "prisma",
|
|
2097
|
-
methods: prismaMethods,
|
|
2098
|
-
getStatement: prismaStatement
|
|
2099
|
-
});
|
|
2100
|
-
}
|
|
2101
|
-
//#endregion
|
|
2102
|
-
//#region src/redis-integration.ts
|
|
2103
|
-
const redisMethods = [
|
|
2104
|
-
"del",
|
|
2105
|
-
"get",
|
|
2106
|
-
"hget",
|
|
2107
|
-
"hset",
|
|
2108
|
-
"lpush",
|
|
2109
|
-
"publish",
|
|
2110
|
-
"rpush",
|
|
2111
|
-
"set",
|
|
2112
|
-
"xadd"
|
|
2113
|
-
];
|
|
2114
|
-
function redisStatement(args, method) {
|
|
2115
|
-
const key = args[0] === void 0 ? "" : ` ${String(args[0])}`;
|
|
2116
|
-
return `${method.toUpperCase()}${key}`;
|
|
2117
|
-
}
|
|
2118
|
-
function redisIntegration(options) {
|
|
2119
|
-
return databaseIntegration({
|
|
2120
|
-
...options,
|
|
2121
|
-
name: options.name ?? "redis",
|
|
2122
|
-
system: "redis",
|
|
2123
|
-
methods: options.methods ?? redisMethods,
|
|
2124
|
-
getStatement: redisStatement
|
|
2125
|
-
});
|
|
2126
|
-
}
|
|
2127
|
-
//#endregion
|
|
2128
|
-
//#region src/serverless-integration.ts
|
|
2129
|
-
const defaultRandom = () => Math.random();
|
|
2130
|
-
let coldStart = true;
|
|
2131
|
-
function isPromiseLike(value) {
|
|
2132
|
-
return typeof value === "object" && value !== null && typeof value.then === "function";
|
|
2133
|
-
}
|
|
2134
|
-
function defaultLevel(error) {
|
|
2135
|
-
return error ? "error" : "info";
|
|
2136
|
-
}
|
|
2137
|
-
function defaultRequestId(event, context) {
|
|
2138
|
-
const ctx = context;
|
|
2139
|
-
const evt = event;
|
|
2140
|
-
return ctx?.awsRequestId ?? evt?.requestContext?.requestId ?? evt?.headers?.["x-request-id"] ?? evt?.headers?.["X-Request-Id"];
|
|
2141
|
-
}
|
|
2142
|
-
function defaultOperation(event, context) {
|
|
2143
|
-
const ctx = context;
|
|
2144
|
-
const evt = event;
|
|
2145
|
-
return evt?.routeKey ?? evt?.requestContext?.routeKey ?? (evt?.requestContext?.http?.method && evt.requestContext.http.path ? `${evt.requestContext.http.method} ${evt.requestContext.http.path}` : void 0) ?? (evt?.httpMethod && (evt.rawPath ?? evt.path) ? `${evt.httpMethod} ${evt.rawPath ?? evt.path}` : void 0) ?? ctx?.functionName;
|
|
2146
|
-
}
|
|
2147
|
-
function normalizeUnknown(value) {
|
|
2148
|
-
return (0, _loggerjs_core.normalizeValue)(value, {
|
|
2149
|
-
maxDepth: 5,
|
|
2150
|
-
maxObjectKeys: 120
|
|
2151
|
-
});
|
|
2152
|
-
}
|
|
2153
|
-
function serverlessIntegration(logger, handler, options = {}) {
|
|
2154
|
-
const name = options.name ?? "serverless";
|
|
2155
|
-
const platform = options.platform ?? "serverless";
|
|
2156
|
-
const captureSuccessful = options.captureSuccessful ?? options.captureAll ?? true;
|
|
2157
|
-
const sampleRate = options.sampleRate ?? 1;
|
|
2158
|
-
const random = options.random ?? defaultRandom;
|
|
2159
|
-
const bindContext = options.bindContext ?? true;
|
|
2160
|
-
const levelFor = options.level ?? defaultLevel;
|
|
2161
|
-
return (event, context, callback) => {
|
|
2162
|
-
const started = Date.now();
|
|
2163
|
-
const currentColdStart = coldStart;
|
|
2164
|
-
coldStart = false;
|
|
2165
|
-
const requestId = options.getRequestId?.(event, context) ?? defaultRequestId(event, context);
|
|
2166
|
-
const operation = options.getOperation?.(event, context) ?? defaultOperation(event, context);
|
|
2167
|
-
let captured = false;
|
|
2168
|
-
const shouldCapture = (error) => {
|
|
2169
|
-
if (error) return true;
|
|
2170
|
-
if (!captureSuccessful) return false;
|
|
2171
|
-
return sampleRate >= 1 || random() < sampleRate;
|
|
2172
|
-
};
|
|
2173
|
-
const capture = (error, result) => {
|
|
2174
|
-
if (captured || !shouldCapture(error)) return;
|
|
2175
|
-
captured = true;
|
|
2176
|
-
const durationMs = Date.now() - started;
|
|
2177
|
-
const info = {
|
|
2178
|
-
context,
|
|
2179
|
-
event,
|
|
2180
|
-
operation,
|
|
2181
|
-
requestId
|
|
2182
|
-
};
|
|
2183
|
-
logger.log(levelFor(error, durationMs, info), `Serverless ${operation ?? name}`, {
|
|
2184
|
-
error,
|
|
2185
|
-
serverless: {
|
|
2186
|
-
kind: name,
|
|
2187
|
-
platform,
|
|
2188
|
-
operation,
|
|
2189
|
-
requestId,
|
|
2190
|
-
coldStart: currentColdStart,
|
|
2191
|
-
durationMs,
|
|
2192
|
-
event: options.captureEvent ? options.normalizeEvent?.(event) ?? normalizeUnknown(event) : void 0,
|
|
2193
|
-
result: options.captureResult && result !== void 0 ? options.normalizeResult?.(result) ?? normalizeUnknown(result) : void 0
|
|
2194
|
-
}
|
|
2195
|
-
});
|
|
2196
|
-
};
|
|
2197
|
-
const wrappedCallback = callback ? (error, result) => {
|
|
2198
|
-
capture(error, result);
|
|
2199
|
-
callback(error, result);
|
|
2200
|
-
} : void 0;
|
|
2201
|
-
const invoke = () => {
|
|
2202
|
-
try {
|
|
2203
|
-
const result = handler(event, context, wrappedCallback);
|
|
2204
|
-
if (isPromiseLike(result)) return result.then((value) => {
|
|
2205
|
-
capture(void 0, value);
|
|
2206
|
-
return value;
|
|
2207
|
-
}, (error) => {
|
|
2208
|
-
capture(error);
|
|
2209
|
-
throw error;
|
|
2210
|
-
});
|
|
2211
|
-
if (result !== void 0 || !wrappedCallback) capture(void 0, result);
|
|
2212
|
-
return result;
|
|
2213
|
-
} catch (error) {
|
|
2214
|
-
capture(error);
|
|
2215
|
-
throw error;
|
|
2216
|
-
}
|
|
2217
|
-
};
|
|
2218
|
-
if (!bindContext) return invoke();
|
|
2219
|
-
return (0, _loggerjs_core.withContext)({
|
|
2220
|
-
functionName: context?.functionName,
|
|
2221
|
-
requestId,
|
|
2222
|
-
serverlessPlatform: platform
|
|
2223
|
-
}, invoke);
|
|
2224
|
-
};
|
|
2225
|
-
}
|
|
2226
|
-
//#endregion
|
|
2227
|
-
//#region src/diagnostics-channel-integration.ts
|
|
2228
|
-
const defaultChannels = [
|
|
2229
|
-
"http.client.request.start",
|
|
2230
|
-
"http.client.response.finish",
|
|
2231
|
-
"http.server.request.start",
|
|
2232
|
-
"http.server.response.finish",
|
|
2233
|
-
"undici:request:create",
|
|
2234
|
-
"undici:request:headers",
|
|
2235
|
-
"undici:request:trailers",
|
|
2236
|
-
"undici:request:error"
|
|
2237
|
-
];
|
|
2238
|
-
const requireBuiltin$1 = (0, node_module.createRequire)(require("url").pathToFileURL(__filename).href);
|
|
2239
|
-
let cachedDiagnosticsChannel$1;
|
|
2240
|
-
function diagnosticsChannelIntegration(options = {}) {
|
|
2241
|
-
const channels = options.channels ?? defaultChannels;
|
|
2242
|
-
const captureMessage = options.captureMessage ?? false;
|
|
2243
|
-
return {
|
|
2244
|
-
name: "diagnostics-channel",
|
|
2245
|
-
setup(api) {
|
|
2246
|
-
const diagnosticsChannel = options.diagnosticsChannel === void 0 ? loadDiagnosticsChannel$1() : options.diagnosticsChannel;
|
|
2247
|
-
if (!diagnosticsChannel) return;
|
|
2248
|
-
const disposers = [];
|
|
2249
|
-
let disposed = false;
|
|
2250
|
-
const capture = api.guard((input) => api.capture(input));
|
|
2251
|
-
for (const channel of channels) {
|
|
2252
|
-
const listener = (message, name) => {
|
|
2253
|
-
capture({
|
|
2254
|
-
level: name.endsWith(":error") ? "error" : "debug",
|
|
2255
|
-
message: `diagnostics_channel ${name}`,
|
|
2256
|
-
props: { diagnostics: {
|
|
2257
|
-
channel: name,
|
|
2258
|
-
message: captureMessage ? (0, _loggerjs_core.normalizeValue)(message, { maxDepth: 4 }) : void 0
|
|
2259
|
-
} }
|
|
2260
|
-
});
|
|
2261
|
-
};
|
|
2262
|
-
diagnosticsChannel.subscribe(channel, listener);
|
|
2263
|
-
disposers.push(() => diagnosticsChannel.unsubscribe?.(channel, listener));
|
|
2264
|
-
}
|
|
2265
|
-
return () => {
|
|
2266
|
-
if (disposed) return;
|
|
2267
|
-
disposed = true;
|
|
2268
|
-
for (const dispose of disposers) dispose();
|
|
2269
|
-
};
|
|
2270
|
-
}
|
|
2271
|
-
};
|
|
2272
|
-
}
|
|
2273
|
-
function loadDiagnosticsChannel$1() {
|
|
2274
|
-
if (cachedDiagnosticsChannel$1 !== void 0) return cachedDiagnosticsChannel$1;
|
|
2275
|
-
try {
|
|
2276
|
-
cachedDiagnosticsChannel$1 = requireBuiltin$1("node:diagnostics_channel");
|
|
2277
|
-
} catch {
|
|
2278
|
-
try {
|
|
2279
|
-
cachedDiagnosticsChannel$1 = requireBuiltin$1("diagnostics_channel");
|
|
2280
|
-
} catch {
|
|
2281
|
-
cachedDiagnosticsChannel$1 = null;
|
|
2282
|
-
}
|
|
2283
|
-
}
|
|
2284
|
-
return cachedDiagnosticsChannel$1;
|
|
2285
|
-
}
|
|
2286
|
-
//#endregion
|
|
2287
|
-
//#region src/logger-diagnostics.ts
|
|
2288
|
-
const requireBuiltin = (0, node_module.createRequire)(require("url").pathToFileURL(__filename).href);
|
|
2289
|
-
let cachedDiagnosticsChannel;
|
|
2290
|
-
function loadDiagnosticsChannel() {
|
|
2291
|
-
if (cachedDiagnosticsChannel !== void 0) return cachedDiagnosticsChannel;
|
|
2292
|
-
try {
|
|
2293
|
-
cachedDiagnosticsChannel = requireBuiltin("node:diagnostics_channel");
|
|
2294
|
-
} catch {
|
|
2295
|
-
try {
|
|
2296
|
-
cachedDiagnosticsChannel = requireBuiltin("diagnostics_channel");
|
|
2297
|
-
} catch {
|
|
2298
|
-
cachedDiagnosticsChannel = null;
|
|
2299
|
-
}
|
|
2300
|
-
}
|
|
2301
|
-
return cachedDiagnosticsChannel;
|
|
2302
|
-
}
|
|
2303
|
-
function installLoggerDiagnosticsChannel(options = {}) {
|
|
2304
|
-
const diagnosticsChannel = options.diagnosticsChannel === void 0 ? loadDiagnosticsChannel() : options.diagnosticsChannel;
|
|
2305
|
-
if (!diagnosticsChannel) return () => {};
|
|
2306
|
-
const prefix = options.prefix ?? "loggerjs";
|
|
2307
|
-
const channels = /* @__PURE__ */ new Map();
|
|
2308
|
-
const channelFor = (stage) => {
|
|
2309
|
-
const name = `${prefix}.${stage}`;
|
|
2310
|
-
let channel = channels.get(name);
|
|
2311
|
-
if (!channel) {
|
|
2312
|
-
channel = diagnosticsChannel.channel(name);
|
|
2313
|
-
channels.set(name, channel);
|
|
2314
|
-
}
|
|
2315
|
-
return channel;
|
|
2316
|
-
};
|
|
2317
|
-
const sink = (event) => {
|
|
2318
|
-
channelFor(event.stage).publish(event);
|
|
2319
|
-
};
|
|
2320
|
-
sink.enabled = (stage) => channelFor(stage).hasSubscribers !== false;
|
|
2321
|
-
const previous = (0, _loggerjs_core.setLoggerDiagnosticSink)(sink);
|
|
2322
|
-
return () => {
|
|
2323
|
-
(0, _loggerjs_core.setLoggerDiagnosticSink)(previous);
|
|
2324
|
-
};
|
|
2325
|
-
}
|
|
2326
|
-
//#endregion
|
|
2327
|
-
exports.bullMqIntegration = bullMqIntegration;
|
|
2328
|
-
exports.captureCliIntegration = captureCliIntegration;
|
|
2329
|
-
exports.captureProcessIntegration = captureProcessIntegration;
|
|
2330
|
-
exports.createAsyncLocalStorageContextManager = createAsyncLocalStorageContextManager;
|
|
2331
|
-
exports.databaseIntegration = databaseIntegration;
|
|
2332
|
-
exports.diagnosticsChannelIntegration = diagnosticsChannelIntegration;
|
|
2333
|
-
exports.expressIntegration = expressIntegration;
|
|
2334
|
-
exports.fastifyIntegration = fastifyIntegration;
|
|
2335
|
-
exports.fileTransport = fileTransport;
|
|
2336
|
-
exports.formatSyslogMessage = formatSyslogMessage;
|
|
2337
|
-
exports.hapiIntegration = hapiIntegration;
|
|
2338
|
-
exports.installAsyncLocalStorageContext = installAsyncLocalStorageContext;
|
|
2339
|
-
exports.installLoggerDiagnosticsChannel = installLoggerDiagnosticsChannel;
|
|
2340
|
-
exports.koaIntegration = koaIntegration;
|
|
2341
|
-
exports.nestMiddlewareIntegration = nestMiddlewareIntegration;
|
|
2342
|
-
exports.nodeCompressionPayloadTransform = nodeCompressionPayloadTransform;
|
|
2343
|
-
exports.nodeFetchIntegration = nodeFetchIntegration;
|
|
2344
|
-
exports.nodeHttpClientIntegration = nodeHttpClientIntegration;
|
|
2345
|
-
exports.nodeHttpTransport = nodeHttpTransport;
|
|
2346
|
-
exports.nodeSyslogTransport = nodeSyslogTransport;
|
|
2347
|
-
exports.prismaIntegration = prismaIntegration;
|
|
2348
|
-
exports.queueIntegration = queueIntegration;
|
|
2349
|
-
exports.redisIntegration = redisIntegration;
|
|
2350
|
-
exports.rotatingFileTransport = rotatingFileTransport;
|
|
2351
|
-
exports.serverlessIntegration = serverlessIntegration;
|
|
2352
|
-
exports.stderrTransport = stderrTransport;
|
|
2353
|
-
exports.stdoutTransport = stdoutTransport;
|
|
2354
|
-
exports.workerTransport = workerTransport;
|
|
2
|
+
const require_stdout_transport = require("./stdout-transport.cjs");
|
|
3
|
+
const require_file_transport = require("./file-transport.cjs");
|
|
4
|
+
const require_rotating_file_transport = require("./rotating-file-transport.cjs");
|
|
5
|
+
const require_http_transport = require("./http-transport.cjs");
|
|
6
|
+
const require_payload_transforms = require("./payload-transforms.cjs");
|
|
7
|
+
const require_syslog_transport = require("./syslog-transport.cjs");
|
|
8
|
+
const require_worker_transport = require("./worker-transport.cjs");
|
|
9
|
+
const require_context = require("./context.cjs");
|
|
10
|
+
const require_queue_integration = require("./queue-integration.cjs");
|
|
11
|
+
const require_bullmq_integration = require("./bullmq-integration.cjs");
|
|
12
|
+
const require_cli_integration = require("./cli-integration.cjs");
|
|
13
|
+
const require_database_integration = require("./database-integration.cjs");
|
|
14
|
+
const require_express_integration = require("./express-integration.cjs");
|
|
15
|
+
const require_fastify_integration = require("./fastify-integration.cjs");
|
|
16
|
+
const require_hapi_integration = require("./hapi-integration.cjs");
|
|
17
|
+
const require_koa_integration = require("./koa-integration.cjs");
|
|
18
|
+
const require_nest_integration = require("./nest-integration.cjs");
|
|
19
|
+
const require_node_fetch_integration = require("./node-fetch-integration.cjs");
|
|
20
|
+
const require_node_http_client_integration = require("./node-http-client-integration.cjs");
|
|
21
|
+
const require_process_integration = require("./process-integration.cjs");
|
|
22
|
+
const require_prisma_integration = require("./prisma-integration.cjs");
|
|
23
|
+
const require_redis_integration = require("./redis-integration.cjs");
|
|
24
|
+
const require_serverless_integration = require("./serverless-integration.cjs");
|
|
25
|
+
const require_diagnostics_channel_integration = require("./diagnostics-channel-integration.cjs");
|
|
26
|
+
const require_logger_diagnostics = require("./logger-diagnostics.cjs");
|
|
27
|
+
exports.bullMqIntegration = require_bullmq_integration.bullMqIntegration;
|
|
28
|
+
exports.captureCliIntegration = require_cli_integration.captureCliIntegration;
|
|
29
|
+
exports.captureProcessIntegration = require_process_integration.captureProcessIntegration;
|
|
30
|
+
exports.createAsyncLocalStorageContextManager = require_context.createAsyncLocalStorageContextManager;
|
|
31
|
+
exports.databaseIntegration = require_database_integration.databaseIntegration;
|
|
32
|
+
exports.diagnosticsChannelIntegration = require_diagnostics_channel_integration.diagnosticsChannelIntegration;
|
|
33
|
+
exports.expressIntegration = require_express_integration.expressIntegration;
|
|
34
|
+
exports.fastifyIntegration = require_fastify_integration.fastifyIntegration;
|
|
35
|
+
exports.fileTransport = require_file_transport.fileTransport;
|
|
36
|
+
exports.formatSyslogMessage = require_syslog_transport.formatSyslogMessage;
|
|
37
|
+
exports.hapiIntegration = require_hapi_integration.hapiIntegration;
|
|
38
|
+
exports.installAsyncLocalStorageContext = require_context.installAsyncLocalStorageContext;
|
|
39
|
+
exports.installLoggerDiagnosticsChannel = require_logger_diagnostics.installLoggerDiagnosticsChannel;
|
|
40
|
+
exports.koaIntegration = require_koa_integration.koaIntegration;
|
|
41
|
+
exports.nestMiddlewareIntegration = require_nest_integration.nestMiddlewareIntegration;
|
|
42
|
+
exports.nodeCompressionPayloadTransform = require_payload_transforms.nodeCompressionPayloadTransform;
|
|
43
|
+
exports.nodeFetchIntegration = require_node_fetch_integration.nodeFetchIntegration;
|
|
44
|
+
exports.nodeHttpClientIntegration = require_node_http_client_integration.nodeHttpClientIntegration;
|
|
45
|
+
exports.nodeHttpTransport = require_http_transport.nodeHttpTransport;
|
|
46
|
+
exports.nodeSyslogTransport = require_syslog_transport.nodeSyslogTransport;
|
|
47
|
+
exports.prismaIntegration = require_prisma_integration.prismaIntegration;
|
|
48
|
+
exports.queueIntegration = require_queue_integration.queueIntegration;
|
|
49
|
+
exports.redisIntegration = require_redis_integration.redisIntegration;
|
|
50
|
+
exports.rotatingFileTransport = require_rotating_file_transport.rotatingFileTransport;
|
|
51
|
+
exports.serverlessIntegration = require_serverless_integration.serverlessIntegration;
|
|
52
|
+
exports.stderrTransport = require_stdout_transport.stderrTransport;
|
|
53
|
+
exports.stdoutTransport = require_stdout_transport.stdoutTransport;
|
|
54
|
+
exports.workerTransport = require_worker_transport.workerTransport;
|
|
55
|
+
var _loggerjs_core = require("@loggerjs/core");
|
|
2355
56
|
Object.keys(_loggerjs_core).forEach(function(k) {
|
|
2356
57
|
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
2357
58
|
enumerable: true,
|
|
@@ -2360,5 +61,3 @@ Object.keys(_loggerjs_core).forEach(function(k) {
|
|
|
2360
61
|
}
|
|
2361
62
|
});
|
|
2362
63
|
});
|
|
2363
|
-
|
|
2364
|
-
//# sourceMappingURL=index.cjs.map
|