nestjs-sentry icon indicating copy to clipboard operation
nestjs-sentry copied to clipboard

Track sessions

Open nick-lvov-dev opened this issue 2 years ago • 1 comments

Sentry has Release health tracking that's performed by providing the release name in the Sentry.init config and monitoring sessions. According to their docs, session tracking in backend apps is done per request. But I cannot make Sentry track requests as sessions, or transactions for that matter, per request. I've implemented a custom filter that creates transactions manually for reach request, but sessions are still aren't generated, which means I can't benefit from Release health tracking. Has anyone else encountered this problem? Is there a solution for this? I tried applying solutions their docs have for Express, but wasn't successful.

nick-lvov-dev avatar Apr 05 '22 09:04 nick-lvov-dev

@nick-lvov-dev I've been on a ride with trying to enable sentry performance with this library, thinking that the library didn't support it, but after a while, I could make it work. My setup is with graphql but I hope this helps you.

There are two things that worked for me, first, it was including the tracesSampleRate in the sentry module, and second and most important is adding the import @sentry/tracking package in the first line of the module.

Note the sentry message in their code example here: https://docs.sentry.io/platforms/node/performance/ "// Note: You MUST import the package for tracing to work"

    SentryModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        debug: false,
        logLevel: configService.get('sentry.logLevel'),
        dsn: configService.get('sentry.dns'),
        environment: configService.get('environment.name'),
        sampleRate: Number(configService.get('sentry.sampleRate')),
        tracesSampleRate: Number(configService.get('sentry.tracesSampleRate')),  <-- initially confused this with the sample rate
        integrations: [new Sentry.Integrations.Http({ tracing: true })],
      }),
      inject: [ConfigService],
    }),

After this I was able to inject the sentry instance and start a transaction like so

    const transaction = this.sentryService.instance().startTransaction({
      op: 'gql',
      name: 'GraphQL',
    });

And when the request finishes you can do transaction.finish().

Hope this helps, it took me a while, I was even considering forking the library but those two hidden things worked for me.

DiogoBatista avatar Sep 08 '22 21:09 DiogoBatista