@geekbears/gb-nest-sentry
TypeScript icon, indicating that this package has built-in type declarations

1.0.8 • Public • Published

GbNestSentry

A NestJS module for Sentry with support for error reporting and operation tracing.

Instalation

To install this package, you must have a reference to the @geekbears repository on your npmrc.

Run the following command to install the NestJS module along with the required dependencies

npm install --save @geekbears/gb-nest-sentry @sentry/node @sentry/tracing

Usage

Once installed, configure the module provider on your application's root module:

GbNestModule.forRoot({
  dsn: '<dsn>',
  environment: '<env>',
  ...
}),

Alternatively, an async factory can be used:

GbNestModule.forRootAsync({
  inject: [],
  useFactory: () => ({
    dsn: '<dsn>',
    environment: '<env>',
    ...
  }),
}),

Options

The module uses the default NodeOptions interface exposed by @sentry/node to configure the Sentry instance.

For more details regarding supported options, please refer to the Sentry Documentation.

Tracing

The module itself sets up a span for every operation, and database tracing can be configured by adding the corresponding integration to the configuration options. For example, a Mongoose connection can be traced by defining the following integration on the config object:

import {Integrations} from '@sentry/node';

...

GbNestModule.forRoot({
  integrations: [new Integrations.Mongo({ useMongoose: true })]
}),

Manual Tracing

For more details regarding database tracing, please refer to the Sentry Documentation

Additionally, manual tracing can be integrated by injecting the SentryService and then creating a new child span. The developer must ensure to handle the child span appropriately.

  1. Import the injection decorator
import { InjectSentry } from '@geekbears/gb-nest-sentry';
  1. Inject the service wherever you need it
constructor(@InjectSentry() private readonly sentryService: SentryService) {}
  1. Start a new child span
this.sentryService.startChild({ ... })

Optimal configuration for Geekbears projects

Geekbears projects rely on NestJS's ConfigService and environment variables, by leveraging these features we can optimally integrate Sentry with the following configuration:

GbSentryModule.forRootAsync({
  inject: [ConfigService],
  useFactory: (config: ConfigService) => ({
    dsn: config.get<string>('SENTRY_DSN'),
    environment: config.get<string>('NODE_ENV'),
    enabled: config.get('SENTRY_ENABLED') === 'true' ? true : false,
    tracesSampleRate: 1.0,
    integrations: [new Integrations.Mongo({ useMongoose: true })],
    // debug: true,
  }),
}),

Explanation:

  • The factorye requires injecting the ConfigService so every value can be read from environment variables.
  • The dsn and environment fields are mandatory.
  • The enabled flag is a useful way to reduce noise by ignoring certain environments or configs (such as testing on development).
  • Integrate Mongo tracing with the useMongoose flag

Considerations

  • The current implementation for the SentryService uses a REQUEST scope. An improvement is already underway to support other contexts appart from HTTP

Readme

Keywords

Package Sidebar

Install

npm i @geekbears/gb-nest-sentry

Weekly Downloads

20

Version

1.0.8

License

MIT

Unpacked Size

34.8 kB

Total Files

53

Last publish

Collaborators

  • it-gb