sentry-context.middleware.ts 885 B

12345678910111213141516171819202122232425
  1. import { Inject, Injectable, NestMiddleware } from '@nestjs/common';
  2. import { Request, Response, NextFunction } from 'express';
  3. import { SENTRY_PLUGIN_OPTIONS, SENTRY_TRANSACTION_KEY } from './constants';
  4. import { SentryService } from './sentry.service';
  5. import { SentryPluginOptions } from './types';
  6. @Injectable()
  7. export class SentryContextMiddleware implements NestMiddleware {
  8. constructor(
  9. @Inject(SENTRY_PLUGIN_OPTIONS) private options: SentryPluginOptions,
  10. private sentryService: SentryService,
  11. ) {}
  12. use(req: Request, res: Response, next: NextFunction) {
  13. if (this.options.enableTracing) {
  14. const transaction = this.sentryService.startTransaction({
  15. op: 'resolver',
  16. name: `GraphQLTransaction`,
  17. });
  18. req[SENTRY_TRANSACTION_KEY] = transaction;
  19. }
  20. next();
  21. }
  22. }