sentry.service.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { Inject, Injectable, OnApplicationBootstrap, OnApplicationShutdown } from '@nestjs/common';
  2. import * as Sentry from '@sentry/node';
  3. import { CaptureContext, TransactionContext } from '@sentry/types';
  4. import { SENTRY_PLUGIN_OPTIONS } from './constants';
  5. import { SentryPluginOptions } from './types';
  6. @Injectable()
  7. export class SentryService implements OnApplicationBootstrap, OnApplicationShutdown {
  8. constructor(@Inject(SENTRY_PLUGIN_OPTIONS) private options: SentryPluginOptions) {}
  9. onApplicationBootstrap(): any {
  10. const integrations = this.options.integrations ?? [
  11. new Sentry.Integrations.Http({ tracing: true }),
  12. ...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(),
  13. ];
  14. Sentry.init({
  15. ...this.options,
  16. tracesSampleRate: this.options.tracesSampleRate ?? 1.0,
  17. integrations,
  18. dsn: this.options.dsn,
  19. });
  20. }
  21. onApplicationShutdown() {
  22. return Sentry.close();
  23. }
  24. captureException(exception: Error) {
  25. Sentry.captureException(exception);
  26. }
  27. captureMessage(message: string, captureContext?: CaptureContext) {
  28. Sentry.captureMessage(message, captureContext);
  29. }
  30. startTransaction(context: TransactionContext) {
  31. return Sentry.startTransaction(context);
  32. }
  33. }