Skip to content

mahendradambe/sentry-nestjs

Repository files navigation

Sentry NestJS

Features

  • Handles creating Transactions, Span and configuring the Scope
    • Avoid Boilerplate for setting up Sentry Express Integration
  • TraceInterceptor For Controllers to help with creating a Span or Transaction
  • Trace and Span Decorators for Provider Methods
  • InjectSentry Decorator which provides SentryService with helper methods and getters for Sentry

Installation

  • Using Yarn
yarn add sentry-nestjs @sentry/node @sentry/tracing
  • Using NPM
npm install sentry-nestjs @sentry/node @sentry/tracing

Usage

  1. Register Module
import { LogLevel } from '@sentry/types'
import { SentryModule } from 'sentry-nestjs'

@Module( {
    imports: [
        SentryModule.forRoot( {
            dsn: '<sentry-dsn>',
            environment: 'dev',
            debug: true,
            logLevel: LogLevel.Debug,
            sampleRate: 1,
            tracesSampleRate: 1,
            expressTracing: true, // use sentry tracing extensions for express
            // rest of the Sentry.Options
        } ),
    ],
    controllers: [ AppController ],
    service: [ AppService ]
} )
  1. On HTTP Controller
import { Span, Transaction } from '@sentry/types'
import { TraceInterceptor, CurrentSpan, CurrentTransaction } from 'sentry-nestjs'

@Controller()
@UseInterceptor( TraceInterceptor ) // use on controller
class AppController {

    constructor(private readonly appService: AppService)

    @Get()
    @UseInterceptor( TraceInterceptor ) // or just on method
    getHello(
        @CurrentSpan() span: CurrentSpan,
        @CurrentTransaction(): transaction: Transaction
    ) {

        return this.appService.getHello()

    }

}
  1. On any Provider
import { InjectSentry, SentryService } from 'sentry-nestjs'

@Injectable()
@Instrument() // use Instrument to trace all methods
class AppService {

    constructor(
        @InjectSentry() sentry: SentryService
    ) {}

    @Trace() // or, use Trace to trace for individual methods
    getHello() {

        this.sentry.currentSpan.setData('data', 'some-data)

        return this.appService.getHello()

    }

}
  1. Inspect the generated Trace on your Sentry Dashboard Trace on Sentry

TODO

  • Add Tests

Author

Mahendra Dambe dambemahendra@gmail.com

License

Licensed under the MIT License - see the LICENSE file for details.