|
| 1 | +import { NestInstrumentation } from '@opentelemetry/instrumentation-nestjs-core'; |
| 2 | +import { |
| 3 | + SEMANTIC_ATTRIBUTE_SENTRY_OP, |
| 4 | + SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, |
| 5 | + captureException, |
| 6 | + defineIntegration, |
| 7 | + getClient, |
| 8 | + getDefaultIsolationScope, |
| 9 | + getIsolationScope, |
| 10 | + spanToJSON, |
| 11 | +} from '@sentry/core'; |
| 12 | +import type { IntegrationFn, Span } from '@sentry/types'; |
| 13 | +import { logger } from '@sentry/utils'; |
| 14 | +import { generateInstrumentOnce } from '../../../otel/instrument'; |
| 15 | +import { SentryNestInstrumentation } from './sentry-nest-instrumentation'; |
| 16 | +import type { MinimalNestJsApp, NestJsErrorFilter } from './types'; |
| 17 | + |
| 18 | +const INTEGRATION_NAME = 'Nest'; |
| 19 | + |
| 20 | +const instrumentNestCore = generateInstrumentOnce('Nest-Core', () => { |
| 21 | + return new NestInstrumentation(); |
| 22 | +}); |
| 23 | + |
| 24 | +const instrumentNestCommon = generateInstrumentOnce('Nest-Common', () => { |
| 25 | + return new SentryNestInstrumentation(); |
| 26 | +}); |
| 27 | + |
| 28 | +export const instrumentNest = Object.assign( |
| 29 | + (): void => { |
| 30 | + instrumentNestCore(); |
| 31 | + instrumentNestCommon(); |
| 32 | + }, |
| 33 | + { id: INTEGRATION_NAME }, |
| 34 | +); |
| 35 | + |
| 36 | +const _nestIntegration = (() => { |
| 37 | + return { |
| 38 | + name: INTEGRATION_NAME, |
| 39 | + setupOnce() { |
| 40 | + instrumentNest(); |
| 41 | + }, |
| 42 | + }; |
| 43 | +}) satisfies IntegrationFn; |
| 44 | + |
| 45 | +/** |
| 46 | + * Nest framework integration |
| 47 | + * |
| 48 | + * Capture tracing data for nest. |
| 49 | + */ |
| 50 | +export const nestIntegration = defineIntegration(_nestIntegration); |
| 51 | + |
| 52 | +/** |
| 53 | + * Setup an error handler for Nest. |
| 54 | + */ |
| 55 | +export function setupNestErrorHandler(app: MinimalNestJsApp, baseFilter: NestJsErrorFilter): void { |
| 56 | + // Sadly, NestInstrumentation has no requestHook, so we need to add the attributes here |
| 57 | + // We register this hook in this method, because if we register it in the integration `setup`, |
| 58 | + // it would always run even for users that are not even using Nest.js |
| 59 | + const client = getClient(); |
| 60 | + if (client) { |
| 61 | + client.on('spanStart', span => { |
| 62 | + addNestSpanAttributes(span); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + app.useGlobalInterceptors({ |
| 67 | + intercept(context, next) { |
| 68 | + if (getIsolationScope() === getDefaultIsolationScope()) { |
| 69 | + logger.warn('Isolation scope is still the default isolation scope, skipping setting transactionName.'); |
| 70 | + return next.handle(); |
| 71 | + } |
| 72 | + |
| 73 | + if (context.getType() === 'http') { |
| 74 | + const req = context.switchToHttp().getRequest(); |
| 75 | + if (req.route) { |
| 76 | + getIsolationScope().setTransactionName(`${req.method?.toUpperCase() || 'GET'} ${req.route.path}`); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return next.handle(); |
| 81 | + }, |
| 82 | + }); |
| 83 | + |
| 84 | + const wrappedFilter = new Proxy(baseFilter, { |
| 85 | + get(target, prop, receiver) { |
| 86 | + if (prop === 'catch') { |
| 87 | + const originalCatch = Reflect.get(target, prop, receiver); |
| 88 | + |
| 89 | + return (exception: unknown, host: unknown) => { |
| 90 | + const status_code = (exception as { status?: number }).status; |
| 91 | + |
| 92 | + // don't report expected errors |
| 93 | + if (status_code !== undefined) { |
| 94 | + return originalCatch.apply(target, [exception, host]); |
| 95 | + } |
| 96 | + |
| 97 | + captureException(exception); |
| 98 | + return originalCatch.apply(target, [exception, host]); |
| 99 | + }; |
| 100 | + } |
| 101 | + return Reflect.get(target, prop, receiver); |
| 102 | + }, |
| 103 | + }); |
| 104 | + |
| 105 | + app.useGlobalFilters(wrappedFilter); |
| 106 | +} |
| 107 | + |
| 108 | +function addNestSpanAttributes(span: Span): void { |
| 109 | + const attributes = spanToJSON(span).data || {}; |
| 110 | + |
| 111 | + // this is one of: app_creation, request_context, handler |
| 112 | + const type = attributes['nestjs.type']; |
| 113 | + |
| 114 | + // If this is already set, or we have no nest.js span, no need to process again... |
| 115 | + if (attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] || !type) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + span.setAttributes({ |
| 120 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.otel.nestjs', |
| 121 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: `${type}.nestjs`, |
| 122 | + }); |
| 123 | +} |
0 commit comments