Skip to content

NestJS TypeScript sentry issues not showing local variables for errors #13768

Closed
@ganjianwei

Description

@ganjianwei

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nestjs

SDK Version

8.30.0

Framework Version

NestJS 10.3.0

Link to Sentry event

https://bloom-studios.sentry.io/issues/5891977466/?project=4506396368568320

Reproduction Example/SDK Setup

import { GrpcInstrumentation } from '@opentelemetry/instrumentation-grpc';
import * as Sentry from '@sentry/nestjs';
import { nodeProfilingIntegration } from '@sentry/profiling-node';

const IGNORE_TRANSACTIONS = [
  /__ApolloGetServiceDefinition__/g,
  /graphql.parse/g,
  /graphql.parseSchema/g,
  /redis-connect/g,
];

const MIN_TRANSACTION_DURATION = 0.3;

export function sentryInit() {
  // Ensure to call this before requiring any other modules!
  Sentry.init({
    dsn: process.env.SENTRY_DSN,
    attachStacktrace: true,
    environment: process.env.ENV,
    enabled: Boolean(process.env.IS_CLOUD),
    includeLocalVariables: true,
    integrations: [
      // Add our Profiling integration
      nodeProfilingIntegration(),
      Sentry.extraErrorDataIntegration({ captureErrorCause: true, depth: 10 }),
      Sentry.graphqlIntegration(),
      Sentry.dedupeIntegration(),
      Sentry.localVariablesIntegration({ captureAllExceptions: true }),
    ],
    ignoreTransactions: IGNORE_TRANSACTIONS,
    tracesSampleRate: 1.0,
    profilesSampleRate: 1.0,
    beforeSendTransaction(event) {
      if (event.timestamp && event.start_timestamp) {
        const duration = event.timestamp - event.start_timestamp;

        if (duration >= MIN_TRANSACTION_DURATION) {
          if (event.transaction?.includes('graphql')) {
            const match = /POST \/graphql \((.+)\)/.exec(event.transaction);
            const parsedOperation = match?.[1];

            if (parsedOperation) {
              event.transaction = parsedOperation.replace(/__.*/g, '');
            }
          }

          return event;
        }
      }

      return null;
    },
  });

  Sentry.addOpenTelemetryInstrumentation(new GrpcInstrumentation());
}

export const SentryGraphqlHooks = {
  preExecution: (_schema, documentNode, _mercuriusContext, variables) => {
    const operationDefinition = documentNode.definitions.find(
      (def) => def.kind === 'OperationDefinition'
    );
    const operation = operationDefinition?.operation;
    const operationName = operationDefinition?.name?.value?.replace(
      /__.*/g,
      ''
    );
    const currentScope = Sentry.getCurrentScope();

    if (operation) {
      currentScope.setTag(operation, operationName);
      currentScope.setTransactionName(`${operation} ${operationName}`);
      currentScope.setContext('variables', variables);
    }
  },
};

Steps to Reproduce

Setup NestJS (TypeScript) server app with Sentry like our above setup with:

    includeLocalVariables: true,
    integrations: [
      // ...
      Sentry.localVariablesIntegration({ captureAllExceptions: true }),
    ],

We also use GraphQL with Mercurius & Fastify.

Have any NestJS module > service > any other code throw an error, and it does not show local variables.

Note we used to use "@sentry/node": "^7.110.1" and just tried changing to NestJS to fix this issue, but to no avail.

Expected Result

I'd expect to see local variables in my Sentry page for easier debugging, as we've seen in other integrations (e.g. Django/Python, and also we see the local variables when there's errors in our NestJS controller code specifically, but not everywhere else).

E.g. from your blog post: https://blog.sentry.io/local-variables-for-nodejs-in-sentry/

Image

Actual Result

No local variables in my Sentry issue page for debugging

E.g.
Image
Image
Image

Thank you!

Metadata

Metadata

Assignees

Labels

Package: nestjsIssues related to the Sentry Nestjs SDK

Type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions