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 tap, pino-pretty and sentry #311

Open
wants to merge 17 commits into
base: beta
Choose a base branch
from
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.nyc_output
.tap
coverage
node_modules
node_modules

5 changes: 1 addition & 4 deletions .taprc
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
files:
- test/**/*.test.js

lines: 91
functions: 100
branches: 83
statements: 91
disable-coverage: true
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const log = pino(
{
name: "probot",
},
getTransformStream()
getTransformStream(),
);
```

Expand All @@ -46,7 +46,7 @@ const log = pino(
{
name: "probot",
},
transform
transform,
);
```

Expand Down
4 changes: 3 additions & 1 deletion cli.js → bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"use strict";

const pump = require("pump");
const split = require("split2");

const { getTransformStream } = require("./");
const { getTransformStream } = require("..");

const options = {
logFormat: process.env.LOG_FORMAT,
Expand Down
25 changes: 17 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { Transform } from "readable-stream";
import { Transform } from "node:stream";

export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal";
type getTransformStream = (options?: getTransformStream.Options) => Transform;

export type Options = {
logFormat?: "json" | "pretty";
logLevelInString?: boolean;
sentryDsn?: string;
};
declare namespace getTransformStream {
export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal";

export function getTransformStream(options?: Options): Transform;
export type Options = {
logFormat?: "json" | "pretty";
logLevelInString?: boolean;
sentryDsn?: string;
};

export const getTransformStream: getTransformStream
export { getTransformStream as default }
}

declare function getTransformStream(...params: Parameters<getTransformStream>): ReturnType<getTransformStream>

export = getTransformStream
79 changes: 50 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module.exports = { getTransformStream };
"use strict";

const { Transform } = require("node:stream");

const prettyFactory = require("pino-pretty");
const Sentry = require("@sentry/node");
const { prettyFactory } = require("pino-pretty");
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,28 +56,18 @@ 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({
objectMode: true,
transform(chunk, enc, cb) {
const line = chunk.toString().trim();

/* istanbul ignore if */
/* c8 ignore start */
if (line === undefined) return cb();
/* c8 ignore stop */

const data = sentryEnabled ? JSON.parse(line) : null;

Expand All @@ -75,18 +84,25 @@ 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) {
if (data.err.event.payload) {
const {
// When GitHub App is installed organization wide
installation: { id, account: { login: account } = {} } = {},
Expand All @@ -95,15 +111,15 @@ function getTransformStream(options = {}) {
organization: { login: organization } = {},
// When the repository belongs to a user
repository: { owner: { login: owner } = {} } = {},
} = data.event.payload;
} = data.err.event.payload;

scope.setUser({
id: id,
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 All @@ -121,10 +137,11 @@ function getTransformStream(options = {}) {
return cb(null, pretty(data));
}

// istanbul ignore if
/* c8 ignore start */
if (levelAsString) {
return cb(null, stringifyLogLevel(data));
}
/* c8 ignore stop */

cb(null, JSON.stringify(data) + "\n");
});
Expand All @@ -143,3 +160,7 @@ function toSentryError(data) {
error.stack = data.stack;
return error;
}

module.exports = getTransformStream;
module.exports.default = getTransformStream;
module.exports.getTransformStream = getTransformStream;
Loading
Loading