-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(nestjs): Change nest sdk setup #12920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
5963dbe
d4ca10a
d9588ef
edcc9dc
76eee81
3778ea5
dd5f558
b9d22c2
18cba0a
d27f688
380657e
ae5dc1f
fd1e084
5a134ba
2795344
72e4f8e
dbf33fc
98363a3
75e8da3
eab3357
683dce7
ff07588
c89c13c
149f1ae
27fad22
49fe07d
c56b363
53e40b3
72aead3
8804071
14e5439
ca4ba13
b095279
da0fffd
6eb6fc5
ae1d6d6
b50c3cb
f6f2ff5
215deee
aae09ba
ac28e4b
56d3e8f
11adb8f
116f0db
ada0ecc
6bfbdf0
0170f14
e19f434
17e4237
b11ecd2
5b23ae6
72d0031
07533a7
a0dcd4f
ce3322a
220a943
0edd4d1
da2b952
b3b6d14
2eb4bdc
357a274
46edc34
345b558
7908cb2
8273956
9466308
9524ac9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,11 +25,15 @@ import { logger } from '@sentry/utils'; | |
import type { Observable } from 'rxjs'; | ||
|
||
/** | ||
* | ||
* Note: We cannot use @ syntax to add the decorators, so we add them directly below the classes as function wrappers. | ||
*/ | ||
|
||
/** | ||
* Interceptor to add Sentry tracing capabilities to Nest.js applications. | ||
*/ | ||
export class SentryTracingInterceptor implements NestInterceptor { | ||
/** | ||
* | ||
* Intercepts HTTP requests to set the transaction name for Sentry tracing. | ||
*/ | ||
public intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> { | ||
if (getIsolationScope() === getDefaultIsolationScope()) { | ||
|
@@ -49,13 +53,14 @@ export class SentryTracingInterceptor implements NestInterceptor { | |
return next.handle(); | ||
} | ||
} | ||
Injectable()(SentryTracingInterceptor); | ||
|
||
/** | ||
* | ||
* Global filter to handle exceptions and report them to Sentry. | ||
*/ | ||
export class SentryGlobalFilter extends BaseExceptionFilter { | ||
/** | ||
* | ||
* Catches exceptions and reports them to Sentry unless they are expected errors. | ||
*/ | ||
public catch(exception: unknown, host: ArgumentsHost): void { | ||
const status_code = (exception as { status?: number }).status; | ||
|
@@ -69,13 +74,14 @@ export class SentryGlobalFilter extends BaseExceptionFilter { | |
return super.catch(exception, host); | ||
} | ||
} | ||
Catch()(SentryGlobalFilter); | ||
|
||
/** | ||
* Set up a nest service that provides error handling and performance tracing. | ||
* Service to set up Sentry performance tracing for Nest.js applications. | ||
*/ | ||
export class SentryIntegrationService implements OnModuleInit { | ||
/** | ||
* Called when the SentryModuleIntegration gets initialized. | ||
* Initializes the Sentry integration service and registers span attributes. | ||
*/ | ||
public onModuleInit(): void { | ||
// Sadly, NestInstrumentation has no requestHook, so we need to add the attributes here | ||
|
@@ -89,13 +95,14 @@ export class SentryIntegrationService implements OnModuleInit { | |
} | ||
} | ||
} | ||
Injectable()(SentryIntegrationService); | ||
|
||
/** | ||
* Set up a root module that can be injected in nest applications. | ||
*/ | ||
export class SentryIntegrationModule { | ||
/** | ||
* Called by the user to set the module as root module in a nest application. | ||
* Configures the module as the root module in a Nest.js application. | ||
*/ | ||
public static forRoot(): DynamicModule { | ||
return { | ||
|
@@ -115,6 +122,21 @@ export class SentryIntegrationModule { | |
}; | ||
} | ||
} | ||
Global()(SentryIntegrationModule); | ||
Module({ | ||
providers: [ | ||
SentryIntegrationService, | ||
{ | ||
provide: APP_FILTER, | ||
useClass: SentryGlobalFilter, | ||
}, | ||
{ | ||
provide: APP_INTERCEPTOR, | ||
useClass: SentryTracingInterceptor, | ||
}, | ||
], | ||
exports: [SentryIntegrationService], | ||
})(SentryIntegrationModule); | ||
|
||
function addNestSpanAttributes(span: Span): void { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. l: Since this function is just used once I would just inline it, which would make it one less indirection. It's stylistic preference though so I will leave it up to you. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll leave this as is, because I think it is a bit easier to understand what is going on with the function name as context |
||
const attributes = spanToJSON(span).data || {}; | ||
|
@@ -132,22 +154,3 @@ function addNestSpanAttributes(span: Span): void { | |
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: `${type}.nestjs`, | ||
}); | ||
} | ||
|
||
Catch()(SentryGlobalFilter); | ||
Injectable()(SentryTracingInterceptor); | ||
Injectable()(SentryIntegrationService); | ||
Global()(SentryIntegrationModule); | ||
Module({ | ||
providers: [ | ||
SentryIntegrationService, | ||
{ | ||
provide: APP_FILTER, | ||
useClass: SentryGlobalFilter, | ||
}, | ||
{ | ||
provide: APP_INTERCEPTOR, | ||
useClass: SentryTracingInterceptor, | ||
}, | ||
], | ||
exports: [SentryIntegrationService], | ||
})(SentryIntegrationModule); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
h: I have a stupid hunch about how we order use of the decorators and exporting. Can we do it like the following so we ensure that we are acutally exporting the decorated versions of the classes:
I am just slightly worried that at one point the class is turned immutable (or is frozen) when exported, and decorating won't do anything. I think our tests prove that this is not necessary right now but just logically and defensively it is more sound.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, updated