2.2.5 • Published 7 months ago

@trxn/nestjs-sentry v2.2.5

Weekly downloads
-
License
-
Repository
github
Last release
7 months ago

NestJs Sentry

Provides a service to send log to Sentry and an interceptor to catch errors.

Usage

Add Sentry Module to your App

@Module({
  imports: [
    SentryModule.register({
      dsn: 'https://public@sentry.example.com/1',
      debug: true,
      environment: 'production',
      release: API_VERSION ?? undefined,
      logLevel: 2,
    }),
  ],
})
export class AppModule {}

Enqueue steps (breadcrumbs) during the request life cycle

import { Controller, Get } from '@nestjs/common';
import { Public } from '@trxn/nestjs-core';
import { SentryPerRequestLogger } from '@trxn/nestjs-sentry';

@Controller(['sentry-tester'])
export class SentryTester {
  constructor(protected sentryPerRequestLogger: SentryPerRequestLogger) {}

  @Get('/sample')
  @Public()
  public async sample() {
    this.sentryPerRequestLogger.push('Calling sample method', 'debug');
    // ... More things
    this.sentryPerRequestLogger.push('Found 2 users in database', 'query');
    // ... More things
  }
}

Add the interceptor to the App

import { SentryAppInterceptorProvider } from '@trxn/nestjs-sentry';
import { RequestTimestampModule } from '@trxn/request-timestamp';

@Module({
  imports: [
    RequestTimestampModule,
    // ...
  ],
  providers: [SentryAppInterceptorProvider({})],
  exports: [],
})
export class AppModule {}

With this configuration, if an error occurred during the request, it will be sent to Sentry with the attached breadcrumbs.

RequestTimestampModule is required to enhanced error reporting. More information below.

Advanced usage

Filter errors that will be sent to Sentry

import { SentryAppInterceptorProvider } from '@polo/nestjs-sentry';

@Module({
  imports: [
    // ...
  ],
  providers: [
    SentryAppInterceptorProvider({
      filters: [
        (error) => error instanceof HttpException && error.getStatus() >= 500,
      ],
    }),
  ],
  exports: [],
})
export class AppModule {}

Use interceptor on specific controller

import { SentryInterceptor } from '@trxn/nestjs-sentry';

@Controller(['sentry-tester'])
@UseInterceptors(
  SentryInterceptor({
    filters: [
      (error) => error instanceof HttpException && error.getStatus() >= 500,
    ],
  }),
)
export class SentryTester {
  // ...
}

Send logs to Sentry outside a request context

You can use the global logger if you are not in a request context.

import { Injectable } from '@nestjs/common';
import { SentryLogger } from '@trxn/nestjs-sentry';

@Injectable()
export class OtherService {
  constructor(protected sentryLogger: SentryLogger) {}

  public method() {
    this.sentryLogger.warn('This is a warning');
  }
}

Send error manually

import { Injectable } from '@nestjs/common';
import { SentryLogger } from '@trxn/nestjs-sentry';

@Injectable()
export class OtherService {
  constructor(protected sentryPerRequestLogger: SentryPerRequestLogger) {}

  public method() {
    try {
      // ....
    } catch (error) {
      this.sentryPerRequestLogger.handleError(e);
    }
  }
}

Reporting

Global and per request

This module uses 2 scopes for reporting: global and per-request.

For better logging and breadcrumb merging, use the per-request scope.

To send logs to the global scope, use service SentryLogger.

To send logs to the per-request scope, use service SentryPerRequestLogger. Beware that all services using SentryPerRequestLogger will use the request scope:

@Injectable({ scope: Scope.REQUEST })

The interceptor SentryInterceptor uses the SentryPerRequestLogger.

Default integrations

By default, Sentry add console logs and HTTP requests as breadcrumbs (in the global scope). If an error occurs, these breadcrumbs are sent with the error (along manually added breadcrumbs).

For more information about these default integrations: https://docs.sentry.io/platforms/javascript/configuration/integrations/default/

Global breadcrumbs

These default integration are not request scoped. Therefore, we do filter breadcrumbs older than the request to separate signal from noise.

Warning

If two requests are concurrent, global breadcrumbs emitted by those requests will be mixed.

To disable global breadcrumbs from per request reporting, use this option: pushGlobalBreadcrumbsWithPerRequestBreadcrumbs.

Request age

We need to determine the age of the request in order to filter global breadcrumbs and associate them to the current request.

To determine the age of the request, we use the module RequestTimestampModule from @trxn/request-timestamp. This module adds the request timestamp in req.res.locals.timestamp.

If this module is not loaded, we assume the request is 2 seconds old. This age can be configured with this option: requestDefaultAge (in milliseconds).

Prisma middleware

This module adds a listener on Prisma queries: https://www.prisma.io/docs/concepts/components/prisma-client/middleware

As long as we cannot access the request context in this middleware, queries are pushed as breadcrumbs in the global scope. These breadcrumbs will then be filtered using the request's age mechanism.

Example

This controller:

@Controller(['sentry-tester'])
@UseInterceptors(
  SentryInterceptor({
    filters: [
      (error) => error instanceof HttpException && error.getStatus() >= 500,
    ],
  }),
)
export class SentryTester {
  constructor(
    @Inject(USER_SERVICE)
    protected userService: UserService,
    protected httpService: HttpService,
    protected sentryPerRequestLogger: SentryPerRequestLogger,
  ) {}

  @Get('/throw')
  @Public()
  public async throw() {
    this.sentryPerRequestLogger.push('Calling throw method', 'debug');

    const user = await this.userService.findFirst({
      where: {},
    });

    console.warn('This is a console.warn');

    await new Promise((r) => setTimeout(r, 1000));

    if (user) {
      this.sentryPerRequestLogger.push(
        `Found user "${user.name}" with email "${user.email}"`,
        'user',
      );
    } else {
      this.sentryPerRequestLogger.push('No user found', 'user');
    }

    await this.httpService
      .request({
        method: 'GET',
        url: 'https://dog.ceo/api/breeds/image/random',
        headers: {
          'Content-type': 'application/json;charset=utf-8',
        },
      })
      .toPromise();

    await new Promise((resolve, reject) =>
      setTimeout(() => {
        reject(new ConflictException('This is a conflict'));
      }, 1000),
    );

    return 'ended';
  }
}

Will show this in Sentry:

Sentry log sample

2.2.3

10 months ago

2.2.5

7 months ago

2.2.4

9 months ago

2.2.1

12 months ago

2.2.2

12 months ago

2.2.0

1 year ago

2.1.14

1 year ago

2.2.0-next.2

1 year ago

2.2.0-next.3

1 year ago

2.1.14-next.0

1 year ago

2.1.9

1 year ago

2.1.12

1 year ago

2.1.13

1 year ago

2.1.10

1 year ago

2.1.11

1 year ago

2.2.0-next.0

1 year ago

2.2.0-next.1

1 year ago

2.1.4

1 year ago

2.1.6

1 year ago

2.1.5

1 year ago

2.1.8

1 year ago

2.1.7

1 year ago

2.1.0-next.1

1 year ago

2.1.0-next.0

1 year ago

2.1.2

1 year ago

2.1.1

1 year ago

2.1.3

1 year ago

2.1.0

1 year ago

2.0.11-next.1

1 year ago

2.0.11-next.0

1 year ago

2.0.11-next.3

1 year ago

2.0.11-next.2

1 year ago

2.0.13

1 year ago

2.0.11

1 year ago

2.0.12

1 year ago

2.0.9

1 year ago

2.0.10

1 year ago

2.0.8

1 year ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.5

1 year ago

2.0.4

1 year ago

2.0.7

1 year ago

2.0.6

1 year ago

2.0.0-next.2

1 year ago

2.0.0-next.0

1 year ago

2.0.0-next.1

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.65.13-next.4

1 year ago

1.65.13-next.5

1 year ago

1.65.11-beta.3

1 year ago

1.65.3

1 year ago

1.65.13-next.2

1 year ago

1.65.4

1 year ago

1.65.5

1 year ago

1.65.13-next.8

1 year ago

1.65.6

1 year ago

1.65.11-next.0

1 year ago

1.65.7

1 year ago

1.65.13-next.6

1 year ago

1.65.8

1 year ago

1.65.13-next.7

1 year ago

1.65.9

1 year ago

1.65.10

1 year ago

1.65.12

1 year ago

1.65.11

1 year ago

1.65.13-next.0

1 year ago

1.65.11-beta.0

1 year ago

1.65.13-next.1

1 year ago

1.65.11-beta.2

1 year ago

1.65.11-beta.1

1 year ago

1.65.2

1 year ago