-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update sentry error handling (#27)
- Loading branch information
1 parent
52cf588
commit 5ba4bdb
Showing
9 changed files
with
1,643 additions
and
2,198 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
CORS=* | ||
SENTRY_DSN= | ||
KEYCLOAK_URL=https://keycloak.aam-digital.net | ||
KEYCLOAK_ADMIN=admin | ||
KEYCLOAK_PASSWORD=PASSWORD | ||
KEYCLOAK_PASSWORD=PASSWORD |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,8 @@ | ||
# Logger Configuration | ||
# values can be overwritten in .env file | ||
|
||
SENTRY: | ||
ENABLED: false | ||
INSTANCE_NAME: local-development # can be personalised in .env -> local-development-<your-name> | ||
ENVIRONMENT: local # local | development | production | ||
DSN: '' |
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,38 @@ | ||
import { readFileSync } from 'fs'; | ||
import * as yaml from 'js-yaml'; | ||
import { join } from 'path'; | ||
|
||
const CONFIG_FILENAME = 'app.yaml'; | ||
|
||
/** | ||
* loads local CONFIG_FILENAME file and provides them in NestJs Config Service | ||
* See: /src/config/app.yaml | ||
*/ | ||
export function AppConfiguration(): Record<string, string> { | ||
return flatten( | ||
yaml.load(readFileSync(join(__dirname, CONFIG_FILENAME), 'utf8')) as Record< | ||
string, | ||
string | ||
>, | ||
); | ||
} | ||
|
||
/** | ||
* Recursively create a flat key-value object where keys contain nested keys as prefixes | ||
*/ | ||
function flatten( | ||
obj: any, | ||
prefix = '', | ||
delimiter = '_', | ||
): Record<string, string> { | ||
return Object.keys(obj).reduce((acc: any, k: string) => { | ||
const pre = prefix.length ? prefix + delimiter : ''; | ||
|
||
if (typeof obj[k] === 'object') | ||
Object.assign(acc, flatten(obj[k], pre + k)); | ||
else { | ||
acc[pre + k] = obj[k]; | ||
} | ||
return acc; | ||
}, {}); | ||
} |
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,68 @@ | ||
import * as Sentry from '@sentry/node'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { ArgumentsHost, INestApplication } from '@nestjs/common'; | ||
import { BaseExceptionFilter, HttpAdapterHost } from '@nestjs/core'; | ||
|
||
export class SentryConfiguration { | ||
ENABLED = false; | ||
DSN = ''; | ||
INSTANCE_NAME = ''; | ||
ENVIRONMENT = ''; | ||
} | ||
|
||
function loadSentryConfiguration( | ||
configService: ConfigService, | ||
): SentryConfiguration { | ||
return { | ||
ENABLED: configService.getOrThrow('SENTRY_ENABLED'), | ||
DSN: configService.getOrThrow('SENTRY_DSN'), | ||
INSTANCE_NAME: configService.getOrThrow('SENTRY_INSTANCE_NAME'), | ||
ENVIRONMENT: configService.getOrThrow('SENTRY_ENVIRONMENT'), | ||
}; | ||
} | ||
|
||
export function configureSentry( | ||
app: INestApplication, | ||
configService: ConfigService, | ||
): void { | ||
const sentryConfiguration = loadSentryConfiguration(configService); | ||
if (sentryConfiguration.ENABLED) { | ||
configureLoggingSentry(app, sentryConfiguration); | ||
} | ||
} | ||
|
||
export class SentryFilter extends BaseExceptionFilter { | ||
catch(exception: unknown, host: ArgumentsHost) { | ||
Sentry.captureException(exception); | ||
super.catch(exception, host); | ||
} | ||
} | ||
|
||
function configureLoggingSentry( | ||
app: INestApplication, | ||
sentryConfiguration: SentryConfiguration, | ||
): void { | ||
Sentry.init({ | ||
debug: true, | ||
serverName: sentryConfiguration.INSTANCE_NAME, | ||
environment: sentryConfiguration.ENVIRONMENT, | ||
dsn: sentryConfiguration.DSN, | ||
integrations: [ | ||
// enable HTTP calls tracing | ||
new Sentry.Integrations.Console(), | ||
new Sentry.Integrations.Http({ tracing: true }), | ||
new Sentry.Integrations.Express(), | ||
], | ||
// Performance Monitoring | ||
tracesSampleRate: 1.0, // Capture 100% of the transactions | ||
// Set sampling rate for profiling - this is relative to tracesSampleRate | ||
profilesSampleRate: 1.0, | ||
}); | ||
|
||
app.use(Sentry.Handlers.errorHandler()); | ||
app.use(Sentry.Handlers.tracingHandler()); | ||
app.use(Sentry.Handlers.requestHandler()); | ||
|
||
const { httpAdapter } = app.get(HttpAdapterHost); | ||
app.useGlobalFilters(new SentryFilter(httpAdapter)); | ||
} |