-
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
124 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,25 @@ | ||
import * as Sentry from '@sentry/node'; | ||
import { | ||
CallHandler, | ||
ExecutionContext, | ||
Injectable, | ||
NestInterceptor | ||
} from '@nestjs/common'; | ||
import { Observable, tap } from 'rxjs'; | ||
import { Observable } from 'rxjs'; | ||
import { getIsolationScope } from '@sentry/node'; | ||
|
||
@Injectable() | ||
export class SentryInterceptor implements NestInterceptor { | ||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | ||
const request = context.switchToHttp().getRequest(); | ||
const { method, url } = request; | ||
if (context.getType() !== 'http') { | ||
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(); | ||
}) | ||
) | ||
) | ||
); | ||
const req = context.switchToHttp().getRequest(); | ||
|
||
if (req.route) { | ||
getIsolationScope().setTransactionName( | ||
`${req.method?.toUpperCase() ?? 'UNKNOWN'} ${req.route.path}` | ||
); | ||
} | ||
} | ||
} |
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,89 @@ | ||
/* 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 { ConfigFactory, Environment } from './app/config'; | ||
import { SentryInterceptor } from './app/interceptors/sentry.interceptor'; | ||
|
||
// Sentry now has a dedicated Nest in integration but doesn't let us integrate | ||
// nicely with our exception handling, also is in an alpha state. Largely | ||
// their approach from https://github.com/getsentry/sentry-javascript/blob/develop/packages/node/src/integrations/tracing/nest.ts | ||
|
||
// Not gonna run validator here, Nest will do on startup and throw if fails, | ||
// not a big deal if something is invalid here, we'll see immediately. | ||
const config = ConfigFactory(); | ||
|
||
const logger = pino(); | ||
|
||
// Private DSN to Sentry, if missing (or we're not in prod env), Sentry isn't | ||
// initialized. | ||
const dsn = config.sentry.dsn; | ||
|
||
// Whether to enable SentryInterceptor. If enabled, we run a transaction | ||
// for the lifetime of tracesSampleRate * all HTTP requests. This | ||
// provides more detailed error | ||
const enableTracing = config.sentry.enableTracing; | ||
|
||
// Rate at which to sample traces. 0.0 - 1 | ||
const sampleRate = config.sentry.tracesSampleRate; | ||
|
||
const integrations: Integration[] = [nestIntegration()]; | ||
|
||
if (config.sentry.tracePrisma) { | ||
integrations.push(prismaIntegration()); | ||
} | ||
|
||
//https://docs.sentry.io/platforms/javascript/guides/node/profiling/#runtime-flags | ||
if (config.sentry.enableNodeProfiling) { | ||
integrations.push(nodeProfilingIntegration()); | ||
} | ||
|
||
const opts: NodeOptions = { | ||
dsn, | ||
environment: config.sentry.env, | ||
enableTracing, | ||
tracesSampleRate: sampleRate, | ||
profilesSampleRate: sampleRate, | ||
debug: false, | ||
integrations | ||
}; | ||
|
||
if (config.env === Environment.PRODUCTION && dsn) { | ||
logger.info('Initializing Sentry'); | ||
Sentry.init(opts); | ||
} | ||
|
||
export function setupNestInterceptor() { | ||
if (!Sentry.isInitialized() || !config.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