-
-
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): Automatic instrumentation of nestjs middleware (#13065)
Adds middleware instrumentation to the `@sentry/nestjs`. The implementation lives in `@sentry/node` so that both users using `@sentry/nestjs` directly as well as users still on `@sentry/node` benefit. The instrumentation is automatic without requiring any additional setup. The idea is to hook into the Injectable decorator (every class middleware is annotated with `@Injectable` and patch the `use` method if it is implemented. Caveat: This implementation only works for class middleware, which implements the `use` method, which seems to be the standard for implementing middleware in nest. However, nest also provides functional middleware, for which this implementation does not work.
- Loading branch information
1 parent
b7e62c4
commit e3af1ce
Showing
12 changed files
with
394 additions
and
6 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
9 changes: 7 additions & 2 deletions
9
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MiddlewareConsumer, Module } from '@nestjs/common'; | ||
import { ScheduleModule } from '@nestjs/schedule'; | ||
import { SentryModule } from '@sentry/nestjs/setup'; | ||
import { AppController } from './app.controller'; | ||
import { AppService } from './app.service'; | ||
import { ExampleMiddleware } from './example.middleware'; | ||
|
||
@Module({ | ||
imports: [SentryModule.forRoot(), ScheduleModule.forRoot()], | ||
controllers: [AppController], | ||
providers: [AppService], | ||
}) | ||
export class AppModule {} | ||
export class AppModule { | ||
configure(consumer: MiddlewareConsumer): void { | ||
consumer.apply(ExampleMiddleware).forRoutes('test-middleware-instrumentation'); | ||
} | ||
} |
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: 12 additions & 0 deletions
12
dev-packages/e2e-tests/test-applications/nestjs-basic/src/example.middleware.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,12 @@ | ||
import { Injectable, NestMiddleware } from '@nestjs/common'; | ||
import * as Sentry from '@sentry/nestjs'; | ||
import { NextFunction, Request, Response } from 'express'; | ||
|
||
@Injectable() | ||
export class ExampleMiddleware implements NestMiddleware { | ||
use(req: Request, res: Response, next: NextFunction) { | ||
// span that should be a child span of the middleware span | ||
Sentry.startSpan({ name: 'test-middleware-span' }, () => {}); | ||
next(); | ||
} | ||
} |
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
9 changes: 7 additions & 2 deletions
9
dev-packages/e2e-tests/test-applications/node-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MiddlewareConsumer, Module } from '@nestjs/common'; | ||
import { ScheduleModule } from '@nestjs/schedule'; | ||
import { AppController } from './app.controller'; | ||
import { AppService } from './app.service'; | ||
import { ExampleMiddleware } from './example.middleware'; | ||
|
||
@Module({ | ||
imports: [ScheduleModule.forRoot()], | ||
controllers: [AppController], | ||
providers: [AppService], | ||
}) | ||
export class AppModule {} | ||
export class AppModule { | ||
configure(consumer: MiddlewareConsumer): void { | ||
consumer.apply(ExampleMiddleware).forRoutes('test-middleware-instrumentation'); | ||
} | ||
} |
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: 12 additions & 0 deletions
12
dev-packages/e2e-tests/test-applications/node-nestjs-basic/src/example.middleware.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,12 @@ | ||
import { Injectable, NestMiddleware } from '@nestjs/common'; | ||
import * as Sentry from '@sentry/nestjs'; | ||
import { NextFunction, Request, Response } from 'express'; | ||
|
||
@Injectable() | ||
export class ExampleMiddleware implements NestMiddleware { | ||
use(req: Request, res: Response, next: NextFunction) { | ||
// span that should be a child span of the middleware span | ||
Sentry.startSpan({ name: 'test-middleware-span' }, () => {}); | ||
next(); | ||
} | ||
} |
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.