-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(back): rework sentry integration
- Loading branch information
Showing
11 changed files
with
123 additions
and
158 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
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 |
---|---|---|
@@ -1,39 +1,35 @@ | ||
import * as Sentry from '@sentry/node'; | ||
import { | ||
CallHandler, | ||
ExecutionContext, | ||
Injectable, | ||
Logger, | ||
NestInterceptor | ||
} from '@nestjs/common'; | ||
import { Observable, tap } from 'rxjs'; | ||
import { Observable } from 'rxjs'; | ||
import { getIsolationScope } from '@sentry/node'; | ||
import { getDefaultIsolationScope } from '@sentry/core'; | ||
|
||
@Injectable() | ||
export class SentryInterceptor implements NestInterceptor { | ||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | ||
const request = context.switchToHttp().getRequest(); | ||
const { method, url } = request; | ||
if (getIsolationScope() === getDefaultIsolationScope()) { | ||
Logger.warn( | ||
'Isolation scope is still the default isolation scope, skipping setting transactionName.' | ||
); | ||
return next.handle(); | ||
} | ||
|
||
// Based on https://github.com/ericjeker/nestjs-sentry-example/blob/main/src/sentry/sentry.interceptor.ts, | ||
// but updated for Sentry 7, which majorly changed how transactions/spans are handled. | ||
return Sentry.startSpan( | ||
{ | ||
op: 'http.server', | ||
name: `${method} ${url}` | ||
}, | ||
(rootSpan: Sentry.Span) => | ||
Sentry.startSpan( | ||
{ | ||
op: 'http.handler', | ||
name: `${context.getClass().name}.${context.getHandler().name}` | ||
}, | ||
(span: Sentry.Span) => | ||
next.handle().pipe( | ||
tap(() => { | ||
span.end(); | ||
rootSpan.end(); | ||
}) | ||
) | ||
) | ||
); | ||
if (context.getType() !== 'http') { | ||
return next.handle(); | ||
} | ||
|
||
const req = context.switchToHttp().getRequest(); | ||
if (req.route) { | ||
getIsolationScope().setTransactionName( | ||
`${req.method?.toUpperCase() ?? 'UNKNOWN'} ${req.route.path}` | ||
); | ||
} | ||
|
||
return next.handle(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* eslint @typescript-eslint/naming-convention: 0 */ | ||
import * as Sentry from '@sentry/node'; | ||
import { | ||
NodeOptions, | ||
nestIntegration, | ||
prismaIntegration, | ||
spanToJSON, | ||
SEMANTIC_ATTRIBUTE_SENTRY_OP, | ||
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN | ||
} from '@sentry/node'; | ||
import { Integration } from '@sentry/types'; | ||
import { nodeProfilingIntegration } from '@sentry/profiling-node'; | ||
import { pino } from 'pino'; | ||
import { SentryInterceptor } from './app/interceptors/sentry.interceptor'; | ||
import { Environment } from './app/config'; | ||
|
||
// Sentry now has a dedicated Nest in integration but doesn't let us integrate | ||
// nicely with our exception filter, also it's in an alpha state. | ||
// Mostly following their approach from | ||
// https://github.com/getsentry/sentry-javascript/blob/develop/packages/node/src/integrations/tracing/nest.ts | ||
|
||
const dsn = process.env['SENTRY_DSN']; | ||
const enableTracing = process.env['SENTRY_ENABLE_TRACING'] === 'true'; | ||
const sampleRate = +process.env['SENTRY_TRACE_SAMPLE_RATE']; | ||
|
||
const integrations: Integration[] = [nestIntegration()]; | ||
|
||
const logger = pino(); | ||
|
||
if (process.env['SENTRY_TRACE_PRISMA'] === 'true') { | ||
integrations.push(prismaIntegration()); | ||
} | ||
|
||
//https://docs.sentry.io/platforms/javascript/guides/node/profiling/#runtime-flags | ||
if (process.env['SENTRY_ENABLE_NODE_PROFILING'] === 'true') { | ||
integrations.push(nodeProfilingIntegration()); | ||
} | ||
|
||
const opts: NodeOptions = { | ||
dsn, | ||
environment: process.env['SENTRY_ENV'], | ||
enableTracing, | ||
tracesSampleRate: sampleRate, | ||
profilesSampleRate: sampleRate, | ||
debug: false, | ||
integrations | ||
}; | ||
|
||
if (process.env['NODE_ENV'] === Environment.PRODUCTION && dsn) { | ||
logger.info('Initializing Sentry'); | ||
Sentry.init(opts); | ||
} | ||
|
||
export function setupNestInterceptor() { | ||
if (!Sentry.isInitialized() || !process.env['SENTRY_DSN']) return; | ||
|
||
const client = Sentry.getClient(); | ||
if (!client) return; | ||
|
||
client.on('spanStart', (span) => { | ||
const attributes = spanToJSON(span).data || {}; | ||
|
||
// this is one of: app_creation, request_context, handler | ||
const type = attributes['nestjs.type']; | ||
|
||
// If this is already set, or we have no nest.js span, no need to process again... | ||
if (attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] || !type) { | ||
return; | ||
} | ||
|
||
span.setAttributes({ | ||
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.otel.nestjs', | ||
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: `${type}.nestjs` | ||
}); | ||
}); | ||
|
||
return new SentryInterceptor(); | ||
} |
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