Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: upgrade pino #311

Merged
merged 28 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.nyc_output
.tap
coverage
node_modules
.tap
64 changes: 40 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { Transform } = require("node:stream");

const { prettyFactory } = require("pino-pretty");
const Sentry = require("@sentry/node");
const { init, withScope, captureException } = require("@sentry/node");

const LEVEL_MAP = {
10: "trace",
Expand All @@ -14,8 +14,27 @@ const LEVEL_MAP = {
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 captionaing using Sentry.
* Implements Probot's default logging formatting and error captioning using Sentry.
*
* @param {import("./").Options} options
* @returns Transform
Expand All @@ -28,7 +47,7 @@ function getTransformStream(options = {}) {
const sentryEnabled = !!options.sentryDsn;

if (sentryEnabled) {
Sentry.init({
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
Expand All @@ -37,19 +56,8 @@ function getTransformStream(options = {}) {
}

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(
",",
),
ignore: pinoIgnore,
errorProps: pinoErrorProps,
});

return new Transform({
Expand All @@ -76,18 +84,26 @@ function getTransformStream(options = {}) {
return;
}

Sentry.withScope(function (scope) {
withScope((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]);
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
if (data.event && data.event.payload) {
const payload = data.event?.payload || data.err?.event?.payload;
if (payload) {
const {
// When GitHub App is installed organization wide
installation: { id, account: { login: account } = {} } = {},
Expand All @@ -96,15 +112,15 @@ function getTransformStream(options = {}) {
organization: { login: organization } = {},
// When the repository belongs to a user
repository: { owner: { login: owner } = {} } = {},
} = data.event.payload;
} = payload;

scope.setUser({
id,
username: account || organization || owner,
});
}

const sentryEventId = Sentry.captureException(toSentryError(data));
const sentryEventId = captureException(toSentryError(data));

// reduce logging data and add reference to sentry event instead
if (data.event) {
Expand Down
137 changes: 73 additions & 64 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"license": "MIT",
"repository": "github:probot/pino",
"devDependencies": {
"@types/pino": "^7.0.5",
"pino": "^6.6.0",
"pino": "^9.3.2",
"prettier": "^3.4.2",
"tap": "^21.0.1"
},
Expand Down
Loading