-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(error): add sentry reporter integrated with otl
refs #247
- Loading branch information
1 parent
8372f38
commit 0378ee4
Showing
7 changed files
with
173 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as GlobalSentry from "@sentry/node"; | ||
import { container, inject } from "tsyringe"; | ||
|
||
import { config } from "@src/core/config"; | ||
import packageJson from "../../../package.json"; | ||
|
||
export const sentryOptions = { | ||
dsn: config.SENTRY_DSN, | ||
environment: config.DEPLOYMENT_ENV || config.NODE_ENV, | ||
tracesSampleRate: config.SENTRY_TRACES_RATE, | ||
release: packageJson.version, | ||
enabled: config.SENTRY_ENABLED === "true" | ||
}; | ||
|
||
GlobalSentry.init({ | ||
...sentryOptions, | ||
serverName: config.SENTRY_SERVER_NAME, | ||
integrations: [new GlobalSentry.Integrations.Http({ tracing: true })] | ||
}); | ||
|
||
const SENTRY = "SENTRY"; | ||
|
||
container.register(SENTRY, { useValue: GlobalSentry }); | ||
|
||
export type Sentry = typeof GlobalSentry; | ||
export const InjectSentry = () => inject(SENTRY); | ||
export const getSentry = () => container.resolve<Sentry>(SENTRY); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
apps/api/src/core/services/sentry-event/sentry-event.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import * as Sentry from "@sentry/node"; | ||
import type { Event } from "@sentry/types"; | ||
import type { StackFrame } from "@sentry/types/types/stackframe"; | ||
import { singleton } from "tsyringe"; | ||
|
||
type ErrorType = Error & { | ||
status?: number; | ||
statusCode?: number; | ||
toJSON?: () => Record<string, any>; | ||
}; | ||
type ObjectErrorType = Record<string, any>; | ||
export type ErrorCatchType = ErrorType | ObjectErrorType | string | unknown; | ||
|
||
@singleton() | ||
export class SentryEventService { | ||
public toEvent(error: ErrorCatchType): Event { | ||
const reportableError = error && typeof error === "object" && "toJSON" in error && typeof error.toJSON === "function" ? error.toJSON() : error; | ||
|
||
if (reportableError instanceof Error) { | ||
return this.toEventFromError(reportableError); | ||
} | ||
|
||
if (typeof reportableError === "string") { | ||
return this.toEventFromMessage(reportableError); | ||
} | ||
|
||
if (typeof reportableError === "object" && reportableError !== null) { | ||
return this.toEventFromObject(reportableError); | ||
} | ||
|
||
return reportableError; | ||
} | ||
|
||
public toEventFromError(error: ErrorType): Event { | ||
return { | ||
message: error.message, | ||
exception: { | ||
values: [ | ||
{ | ||
type: error.constructor.name, | ||
value: error.message, | ||
stacktrace: this.getStackFrames(error) | ||
} | ||
] | ||
} | ||
}; | ||
} | ||
|
||
public toEventFromMessage(message: string): Event { | ||
return { | ||
message, | ||
exception: { | ||
values: [ | ||
{ | ||
type: message.constructor.name, | ||
value: message | ||
} | ||
] | ||
} | ||
}; | ||
} | ||
|
||
public toEventFromObject(payload: ObjectErrorType): Event { | ||
let message = typeof payload === "object" && (("message" in payload && payload.message) || ("title" in payload && payload.title)); | ||
|
||
if (!message) { | ||
message = JSON.stringify(payload); | ||
|
||
if (message.length > 100) { | ||
message = message.slice(0, 100) + "..."; | ||
} | ||
} | ||
|
||
return { | ||
message: message, | ||
extra: payload, | ||
exception: { | ||
values: [ | ||
{ | ||
type: payload.name || payload.constructor.name, | ||
value: message, | ||
stacktrace: this.getStackFrames(payload) | ||
} | ||
] | ||
} | ||
}; | ||
} | ||
|
||
private getStackFrames(payload?: { stack?: string }): { frames: StackFrame[] } | undefined { | ||
return payload?.stack ? { frames: Sentry.defaultStackParser(payload.stack) } : undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters