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

Add support for pre-configured Sentry instance #67

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
21 changes: 13 additions & 8 deletions src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ const SeverityIota = {
} as const;

export interface PinoSentryOptions extends Sentry.NodeOptions {
// Instance of Sentry, otherwise Sentry.init() is called
sentry?: typeof Sentry;
/** Minimum level for a log to be reported to Sentry from pino-sentry */
level?: keyof typeof SeverityIota;
messageAttributeKey?: string;
Expand All @@ -75,6 +77,7 @@ function get(data: any, path: string) {
}

export class PinoSentryTransport {
public readonly sentry: typeof Sentry;
// Default minimum log level to `debug`
minimumLogLevel: ValueOf<typeof SeverityIota> = SeverityIota[Severity.Debug];
messageAttributeKey = 'msg';
Expand All @@ -85,17 +88,19 @@ export class PinoSentryTransport {
decorateScope = (_data: Record<string, unknown>, _scope: Sentry.Scope) => {/**/};

public constructor(options?: PinoSentryOptions) {
Sentry.init(this.validateOptions(options || {}));
const { sentry, ...initOptions} = this.validateOptions(options || {});
if (sentry) {
this.sentry = sentry;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about the options passed to this function? call init again?

} else {
Sentry.init(initOptions);
this.sentry = Sentry;
}
}

public getLogSeverity(level: keyof typeof SEVERITIES_MAP): Severity {
return SEVERITIES_MAP[level] || Severity.Info;
}

public get sentry() {
return Sentry;
}

public transformer(): stream.Transform {
return through.obj((chunk: any, _enc: any, cb: any) => {
this.prepareAndGo(chunk, cb);
Expand Down Expand Up @@ -142,7 +147,7 @@ export class PinoSentryTransport {
const message: any & Error = get(chunk, this.messageAttributeKey);
const stack = get(chunk, this.stackAttributeKey) || '';

const scope = new Sentry.Scope();
const scope = new this.sentry.Scope();
this.decorateScope(chunk, scope);

scope.setLevel(severity as any);
Expand All @@ -163,11 +168,11 @@ export class PinoSentryTransport {
if (this.isSentryException(severity)) {
const error = message instanceof Error ? message : new ExtendedError({ message, stack });

Sentry.captureException(error, scope);
this.sentry.captureException(error, scope);
setImmediate(cb);
} else {
// Capturing Messages
Sentry.captureMessage(message, scope);
this.sentry.captureMessage(message, scope);
setImmediate(cb);
}
}
Expand Down
Loading