-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
161 lines (135 loc) · 3.98 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { Transform } from "node:stream";
import { prettyFactory } from "pino-pretty";
import { init, withScope, captureException } from "@sentry/node";
const LEVEL_MAP = {
10: "trace",
20: "debug",
30: "info",
40: "warn",
50: "error",
60: "fatal",
};
const pinoIgnore = [
// default pino keys
"time",
"pid",
"hostname",
// remove keys from pino-http
"req",
"res",
"responseTime",
].join(",");
const pinoErrorProps = [
"event",
"status",
"headers",
"request",
"sentryEventId",
].join(",");
/**
* Implements Probot's default logging formatting and error captioning using Sentry.
*
* @param {import("./").Options} options
* @returns Transform
* @see https://getpino.io/#/docs/transports
*/
export function getTransformStream(options = {}) {
const formattingEnabled = options.logFormat !== "json";
const levelAsString = options.logLevelInString;
const sentryEnabled = !!options.sentryDsn;
if (sentryEnabled) {
init({
dsn: options.sentryDsn,
// See https://github.com/getsentry/sentry-javascript/issues/1964#issuecomment-688482615
// 6 is enough to serialize the deepest property across all GitHub Event payloads
normalizeDepth: 6,
});
}
const pretty = prettyFactory({
ignore: pinoIgnore,
errorProps: pinoErrorProps,
});
return new Transform({
objectMode: true,
transform(chunk, enc, cb) {
const line = chunk.toString().trim();
/* c8 ignore start */
if (line === undefined) return cb();
/* c8 ignore stop */
const data = sentryEnabled ? JSON.parse(line) : null;
if (!sentryEnabled || data.level < 50) {
if (formattingEnabled) {
return cb(null, pretty(line));
}
if (levelAsString) {
return cb(null, stringifyLogLevel(JSON.parse(line)));
}
cb(null, line + "\n");
return;
}
withScope((scope) => {
const sentryLevelName = data.level === 50 ? "error" : "fatal";
scope.setLevel(sentryLevelName);
if (data.event) {
scope.setExtra("event", data.event);
}
if (data.headers) {
scope.setExtra("headers", data.headers);
}
if (data.request) {
scope.setExtra("request", data.request);
}
if (data.status) {
scope.setExtra("status", data.status);
}
// set user id and username to installation ID and account login
const payload = data.event?.payload || data.err?.event?.payload;
if (payload) {
const {
// When GitHub App is installed organization wide
installation: { id, account: { login: account } = {} } = {},
// When the repository belongs to an organization
organization: { login: organization } = {},
// When the repository belongs to a user
repository: { owner: { login: owner } = {} } = {},
} = payload;
scope.setUser({
id,
username: account || organization || owner,
});
}
const sentryEventId = captureException(toSentryError(data));
// reduce logging data and add reference to sentry event instead
if (data.event) {
data.event = { id: data.event.id };
}
if (data.request) {
data.request = {
method: data.request.method,
url: data.request.url,
};
}
data.sentryEventId = sentryEventId;
if (formattingEnabled) {
return cb(null, pretty(data));
}
/* c8 ignore start */
if (levelAsString) {
return cb(null, stringifyLogLevel(data));
}
/* c8 ignore stop */
cb(null, JSON.stringify(data) + "\n");
});
},
});
}
function stringifyLogLevel(data) {
data.level = LEVEL_MAP[data.level];
return JSON.stringify(data) + "\n";
}
function toSentryError(data) {
const error = new Error(data.msg);
error.name = data.type;
error.stack = data.stack;
return error;
}