-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nestjs): Change nest sdk setup (#12920)
- Adds a new nest root module that can be used to setup the Nest SDK as a replacement for the existing setup (with a function). Instead of calling `setupNestErrorHandler` in the main.ts file, users can now add `SentryModule.forRoot()` (feedback about the name is definitely welcome) as an import in their main app module. This approach is much more native to nest than what we used so far. This root module is introduced in the setup.ts file. - This root module is exported with a submodule export `@sentry/nestjs/setup`, because the SDK now depends on nestjs directly and without this the nest instrumentation does not work anymore, since nest gets imported before Sentry.init gets called, which disables the otel nest instrumentation. - Judging from the e2e tests it seems that this new approach also resolves some issues the previous implementation had, specifically [this issue](#12351) seems to be resolved. The e2e test that was in place, just documented the current (wrong) behavior. So I updated the test to reflect the new (correct) behavior. - I updated all the test applications to use the new approach but kept a copy of the nestjs-basic and nestjs-distributed-tracing with the old setup (now named node-nestjs-basic and node-nestjs-distributed-tracing respectively) so we can still verify that the old setup (which a lot of people use) still keeps working going forward. - Updated/New tests in this PR: - Sends unexpected exception to Sentry if thrown in Submodule - Does not send expected exception to Sentry if thrown in Submodule and caught by a global exception filter - Does not send expected exception to Sentry if thrown in Submodule and caught by a local exception filter - Sends expected exception to Sentry if thrown from submodule registered before Sentry - To accomodate the new tests I added several submodules in the nestjs-with-submodules test-application. These are overall similarly but have important distinctions: - example-module-local-filter: Submodule with a local filter registered using `@UseFilters` on the controller. - example-module-global-filter: Submodule with a global filter registered using APP_FILTER in the submodule definition. - example-module-global-filter-wrong-registration-order: Also has a global filter set with APP_FILTER, but is registered in the root module as first submodule, even before the SentryIntegration is initialized. This case does not work properly in the new setup (Sentry should be set first), so this module is used for tests documenting this behavior. - Also set "moduleResolution": "Node16" in the nestjs-basic sample app to ensure our submodule-export workaround works in both, default and sub-path-export-compatible TS configs as was suggested [here](#12948 (comment)).
- Loading branch information
1 parent
f867cc0
commit b577079
Showing
67 changed files
with
1,953 additions
and
59 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
3 changes: 2 additions & 1 deletion
3
dev-packages/e2e-tests/test-applications/nestjs-basic/src/app.module.ts
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
3 changes: 2 additions & 1 deletion
3
...ages/e2e-tests/test-applications/nestjs-distributed-tracing/src/trace-initiator.module.ts
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
12 changes: 10 additions & 2 deletions
12
dev-packages/e2e-tests/test-applications/nestjs-with-submodules/src/app.module.ts
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
17 changes: 17 additions & 0 deletions
17
...ubmodules/src/example-module-global-filter-wrong-registration-order/example.controller.ts
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,17 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { ExampleExceptionWrongRegistrationOrder } from './example.exception'; | ||
|
||
@Controller('example-module-wrong-order') | ||
export class ExampleController { | ||
constructor() {} | ||
|
||
@Get('/expected-exception') | ||
getCaughtException(): string { | ||
throw new ExampleExceptionWrongRegistrationOrder(); | ||
} | ||
|
||
@Get('/unexpected-exception') | ||
getUncaughtException(): string { | ||
throw new Error(`This is an uncaught exception!`); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...submodules/src/example-module-global-filter-wrong-registration-order/example.exception.ts
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,5 @@ | ||
export class ExampleExceptionWrongRegistrationOrder extends Error { | ||
constructor() { | ||
super('Something went wrong in the example module!'); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...th-submodules/src/example-module-global-filter-wrong-registration-order/example.filter.ts
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,13 @@ | ||
import { ArgumentsHost, BadRequestException, Catch } from '@nestjs/common'; | ||
import { BaseExceptionFilter } from '@nestjs/core'; | ||
import { ExampleExceptionWrongRegistrationOrder } from './example.exception'; | ||
|
||
@Catch(ExampleExceptionWrongRegistrationOrder) | ||
export class ExampleExceptionFilterWrongRegistrationOrder extends BaseExceptionFilter { | ||
catch(exception: unknown, host: ArgumentsHost) { | ||
if (exception instanceof ExampleExceptionWrongRegistrationOrder) { | ||
return super.catch(new BadRequestException(exception.message), host); | ||
} | ||
return super.catch(exception, host); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...th-submodules/src/example-module-global-filter-wrong-registration-order/example.module.ts
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,16 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { APP_FILTER } from '@nestjs/core'; | ||
import { ExampleController } from './example.controller'; | ||
import { ExampleExceptionFilterWrongRegistrationOrder } from './example.filter'; | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [ExampleController], | ||
providers: [ | ||
{ | ||
provide: APP_FILTER, | ||
useClass: ExampleExceptionFilterWrongRegistrationOrder, | ||
}, | ||
], | ||
}) | ||
export class ExampleModuleGlobalFilterWrongRegistrationOrder {} |
25 changes: 25 additions & 0 deletions
25
...pplications/nestjs-with-submodules/src/example-module-global-filter/example.controller.ts
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,25 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import * as Sentry from '@sentry/nestjs'; | ||
import { ExampleException } from './example.exception'; | ||
|
||
@Controller('example-module') | ||
export class ExampleController { | ||
constructor() {} | ||
|
||
@Get('/expected-exception') | ||
getCaughtException(): string { | ||
throw new ExampleException(); | ||
} | ||
|
||
@Get('/unexpected-exception') | ||
getUncaughtException(): string { | ||
throw new Error(`This is an uncaught exception!`); | ||
} | ||
|
||
@Get('/transaction') | ||
testTransaction() { | ||
Sentry.startSpan({ name: 'test-span' }, () => { | ||
Sentry.startSpan({ name: 'child-span' }, () => {}); | ||
}); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
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
14 changes: 14 additions & 0 deletions
14
...applications/nestjs-with-submodules/src/example-module-local-filter/example.controller.ts
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,14 @@ | ||
import { Controller, Get, UseFilters } from '@nestjs/common'; | ||
import { LocalExampleException } from './example.exception'; | ||
import { LocalExampleExceptionFilter } from './example.filter'; | ||
|
||
@Controller('example-module-local-filter') | ||
@UseFilters(LocalExampleExceptionFilter) | ||
export class ExampleControllerLocalFilter { | ||
constructor() {} | ||
|
||
@Get('/expected-exception') | ||
getCaughtException() { | ||
throw new LocalExampleException(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...-applications/nestjs-with-submodules/src/example-module-local-filter/example.exception.ts
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,5 @@ | ||
export class LocalExampleException extends Error { | ||
constructor() { | ||
super('Something went wrong in the example module with local filter!'); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...est-applications/nestjs-with-submodules/src/example-module-local-filter/example.filter.ts
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,13 @@ | ||
import { ArgumentsHost, BadRequestException, Catch } from '@nestjs/common'; | ||
import { BaseExceptionFilter } from '@nestjs/core'; | ||
import { LocalExampleException } from './example.exception'; | ||
|
||
@Catch(LocalExampleException) | ||
export class LocalExampleExceptionFilter extends BaseExceptionFilter { | ||
catch(exception: unknown, host: ArgumentsHost) { | ||
if (exception instanceof LocalExampleException) { | ||
return super.catch(new BadRequestException(exception.message), host); | ||
} | ||
return super.catch(exception, host); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...est-applications/nestjs-with-submodules/src/example-module-local-filter/example.module.ts
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,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ExampleControllerLocalFilter } from './example.controller'; | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [ExampleControllerLocalFilter], | ||
providers: [], | ||
}) | ||
export class ExampleModuleLocalFilter {} |
12 changes: 0 additions & 12 deletions
12
...e-tests/test-applications/nestjs-with-submodules/src/example-module/example.controller.ts
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
Oops, something went wrong.