-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentry.js
47 lines (41 loc) · 973 Bytes
/
sentry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { captureEvent, captureException } from "@sentry/core";
export class SentryWriteStream extends WritableStream {
/**
* @type {NodeJS.Timeout | undefined}
*/
timer = undefined;
/**
* @type {import("./log.js").LogObject[]}
*/
batch = [];
async flush() {
for (const msg of this.batch) {
if (msg.level === "error") {
const err = msg.args.find((arg) => arg instanceof Error);
if (err) {
captureException(err);
} else {
captureEvent({
message: msg.message,
extra: msg,
});
}
}
}
this.batch.length = 0;
}
constructor() {
super({
/**
* @param {import("./log.js").LogObject} msg
*/
write: async (msg) => {
this.batch.push(msg);
if (this.timer !== undefined) {
clearTimeout(this.timer);
}
this.timer = setTimeout(() => this.flush(), 1000);
},
});
}
}