-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
145 lines (119 loc) · 3.74 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
module.exports = { getTransformStream };
const { Transform } = require("readable-stream");
const prettyFactory = require("pino-pretty");
const Sentry = require("@sentry/node");
const LEVEL_MAP = {
10: "trace",
20: "debug",
30: "info",
40: "warn",
50: "error",
60: "fatal",
};
/**
* Implements Probot's default logging formatting and error captionaing using Sentry.
*
* @param {import("./").Options} options
* @returns Transform
* @see https://getpino.io/#/docs/transports
*/
function getTransformStream(options = {}) {
const formattingEnabled = options.logFormat !== "json";
const levelAsString = options.logLevelInString;
const sentryEnabled = !!options.sentryDsn;
if (sentryEnabled) {
Sentry.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: [
// default pino keys
"time",
"pid",
"hostname",
// remove keys from pino-http
"req",
"res",
"responseTime",
].join(","),
errorProps: ["event", "status", "headers", "request", "sentryEventId"].join(
","
),
});
return new Transform({
objectMode: true,
transform(chunk, enc, cb) {
const line = chunk.toString().trim();
/* istanbul ignore if */
if (line === undefined) return cb();
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;
}
Sentry.withScope(function (scope) {
const sentryLevelName = data.level === 50 ? "error" : "fatal";
scope.setLevel(sentryLevelName);
for (const extra of ["event", "headers", "request", "status"]) {
if (!data[extra]) continue;
scope.setExtra(extra, data[extra]);
}
// set user id and username to installation ID and account login
if (data.event && data.event.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 } = {} } = {},
} = data.event.payload;
scope.setUser({
id: id,
username: account || organization || owner,
});
}
const sentryEventId = Sentry.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));
}
// istanbul ignore if
if (levelAsString) {
return cb(null, stringifyLogLevel(data));
}
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;
}