npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@niaro/nestjs-sentry

v4.2.3

Published

Provides an injectable sentry.io client to provide enterprise logging of nestjs modules

Downloads

1,153

Readme

npm version ISC license

Table Of Contents

About

@travelerdev/nestjs-sentry is built upon the foundation developed by @ntegral/nestjs-sentry. Both packages implement a module, SentryModule which when imported into your nestjs project provides a Sentry.io client to any class that injects it. This lets Sentry.io be worked into your dependency injection workflow without having to do any extra work outside of the initial setup.

It can optionally also intercept error messages logged by your system and automatically propogate those to Sentry.

GraphQL Support

If you are writing a server that uses @nestjs/graphql, you probably want to use the package @travelerdev/nestjs-sentry-graphql instead. It contains all the same code as this package but adds an interceptor specifically for GraphQL resolvers. It has been separated out into its own package so that depending on this package does not introduce any dependencies on @nestjs/graphql.

NestJS 9 Support

This package begins at version 4.x.x and supports NestJS 9+. If you need support for NestJS 8 or 7, please visit @ntegral/nestjs-sentry for support.

Installation

npm install --save @travelerdev/nestjs-sentry @sentry/node

Getting Started

To get started with @travelerdev/nestjs-sentry you should add an import of SentryModule.forRoot to your app's root module.

import { Module } from '@nestjs-common';
import { SentryModule } from '@travelerdev/nestjs-sentry';

@Module({
  imports: [
    SentryModule.forRoot({
      dsn: '<< your sentry_io_dsn >>',
      debug: true | false,
      environment: 'dev' | 'production' | 'some_environment',
      release: 'some_release', | null, // must first create a release in sentry.io dashboard
      logLevels: ["debug"] //based on sentry.io loglevel //
    }),
  ],
})
export class AppModule {}

You can alternatively use an async config factory if you need injected dependencies:

import { Module } from '@nestjs-common';
import { SentryModule } from '@travelerdev/nestjs-sentry';
import { ConfigModule } from '@nestjs/config';
import { ConfigService } from '@nestjs/config';

@Module({
  imports: [
    SentryModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (cfg:ConfigService) => ({
        dsn: cfg.get('SENTRY_DSN'),
        debug: true | false,
        environment: 'dev' | 'production' | 'some_environment',
        release: 'some_release', | null, // must create a release in sentry.io dashboard
        logLevels: ["debug"] //based on sentry.io loglevel //
      }),
      inject: [ConfigService],
    })
  ]
})

export class AppModule {}

After importing, you can then inject the Sentry client into any of your injectables with the provided decorator:

import { Injectable } from '@nestjs/common';
import { InjectSentry, SentryService } from '@travelerdev/nestjs-sentry';

@Injectable()
export class AppService {
  public constructor(@InjectSentry() private readonly client: SentryService) {
      client.instance().captureMessage(message, Sentry.Severity.Log);
      client.instance().captureException(exception);
      ... and more
  }
}

To automatically absorb messages from your service into Sentry, you can instruct Nest to use the SentryService as the default logger:

async function bootstrap() {
  const app = await NestFactory.create(AppModule, { logger: false });

  app.useLogger(SentryService.SentryServiceInstance());
  await app.listen(3000);
}
bootstrap();

You can use the various logging and breadcrumbing methods to create helpful debug information in Sentry:

import { Injectable } from '@nestjs/common';
import { InjectSentry, SentryService } from '@travelerdev/nestjs-sentry';
import { Severity } from '@sentry/types';

@Injectable()
export class AppService {
  constructor(@InjectSentry() private readonly client: SentryService) {
    client.log('AppSevice Loaded', 'test', true); // creates log asBreadcrumb //
    client.instance().addBreadcrumb({
      level: Severity.Debug,
      message: 'How to use native breadcrumb',
      data: { context: 'WhatEver' }
    });
    client.debug('AppService Debug', 'context');
  }
}

Flushing sentry

Sentry does not flush all the errors by itself, it does it in background so that it doesn't block the main thread. If you kill the nestjs app forcefully some exceptions don't have to be flushed and logged successfully.

If you want to force that behaviour use the close flag in your options. That is handy if using nestjs as a console runner. Keep in mind that you need to have app.enableShutdownHooks(); enabled in order for closing (flushing) to work.

import { Module } from '@nestjs-common';
import { SentryModule } from '@travelerdev/nestjs-sentry';
import { LogLevel } from '@sentry/types';

@Module({
  imports: [
    SentryModule.forRoot({
      dsn: 'sentry_io_dsn',
      debug: true | false,
      environment: 'dev' | 'production' | 'some_environment',
      release: 'some_release', | null, // must create a release in sentry.io dashboard
      logLevel: LogLevel.Debug //based on sentry.io loglevel //
      close: {
        enabled: true,
        // Time in milliseconds to forcefully quit the application
        timeout?: number,
      }
    }),
  ],
})
export class AppModule {}

Contributing

This project is itself a fork of a long-lived open source projects, and so contributions are always welcome. They are the only way to keep this project alive and thriving. If you want to contribute, please follow these steps:

  1. Fork the repository
  2. Create your branch (git checkout -b my-feature-name)
  3. Commit any changes to your branch
  4. Push your changes to your remote branch
  5. Open a pull request

License

Distributed under the ISC License. See LICENSE for more information.

Acknowledgements

Copyright © 2019 Ntegral Inc. and 2022 Traveler Dev Ltd. (England 13120175)