-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
nest.ts
69 lines (62 loc) · 2.01 KB
/
nest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { NestInstrumentation } from '@opentelemetry/instrumentation-nestjs-core';
import { captureException, defineIntegration, getDefaultIsolationScope, getIsolationScope } from '@sentry/core';
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
import type { IntegrationFn } from '@sentry/types';
import { logger } from '@sentry/utils';
interface MinimalNestJsExecutionContext {
switchToHttp: () => {
// minimal request object
// according to official types, all properties are required but
// let's play it safe and assume they're optional
getRequest: () => {
route?: {
path?: string;
};
method?: string;
};
};
}
interface MinimalNestJsApp {
useGlobalFilters: (arg0: { catch(exception: unknown): void }) => void;
useGlobalInterceptors: (interceptor: {
intercept: (context: MinimalNestJsExecutionContext, next: { handle: () => void }) => void;
}) => void;
}
const _nestIntegration = (() => {
return {
name: 'Nest',
setupOnce() {
addOpenTelemetryInstrumentation(new NestInstrumentation({}));
},
};
}) satisfies IntegrationFn;
/**
* Nest framework integration
*
* Capture tracing data for nest.
*/
export const nestIntegration = defineIntegration(_nestIntegration);
const SentryNestExceptionFilter = {
catch(exception: unknown) {
captureException(exception);
},
};
/**
* Setup an error handler for Nest.
*/
export function setupNestErrorHandler(app: MinimalNestJsApp): void {
app.useGlobalInterceptors({
intercept(context, next) {
if (getIsolationScope() === getDefaultIsolationScope()) {
logger.warn('Isolation scope is still the default isolation scope, skipping setting transactionName.');
return next.handle();
}
const req = context.switchToHttp().getRequest();
if (req.route) {
getIsolationScope().setTransactionName(`${req.method?.toUpperCase() || 'GET'} ${req.route.path}`);
}
return next.handle();
},
});
app.useGlobalFilters(SentryNestExceptionFilter);
}