sentry-javascript: Prisma Tracer not working on production builds

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using? If you use the CDN bundles, please specify the exact bundle (e.g. bundle.tracing.min.js) in your SDK setup.

@sentry/nextjs

SDK Version

7.37.2

Framework Version

13.1.1

Link to Sentry event

https://pulposapp.sentry.io/performance/pulpos-app-dev:d8283cfa1e9f4cd893e0e8cc26e59988/?project=4504339811074048&query=http.method%3AGET&referrer=performance-transaction-summary&statsPeriod=7d&transaction=GET+%2Fapi%2Ftrpc%2F[trpc]&unselectedSeries=p100()

SDK Setup

  Sentry.init({
    environment: env.NEXT_PUBLIC_APP_ENV,
    dsn: env.NEXT_PUBLIC_SENTRY_DSN,
    tracesSampleRate: 1.0,
    profilesSampleRate: 1.0,
    integrations: [
      new CaptureConsole({
        levels: ['log', 'warn', 'error', 'debug', 'assert'],
      }),
      new Tracing.Integrations.Prisma({ client: prisma }),
      new ProfilingIntegration(),
    ],
  });

Steps to Reproduce

For context, this is being run within an NX monorepo.

Steps:

  1. Build production build: nx build app (application is called app)
  2. Execute production build: nx run app:serve:production
  3. Open the application in a page that has any tRPC API call with database use.

Expected Result

Profiling contains prisma database spans.

Actual Result

No database spans are present. However the interesting thing is that if I were to execute the application in development mode, the spans are reported correctly. This issue started happening when we upgraded the following packages:

"@sentry/nextjs": "^7.28.1" -> "^7.37.2",
"@sentry/profiling-node": "^0.0.12" -> "^7.37.2",
"@sentry/replay": "^7.28.1" -> "^7.37.2",
"@sentry/tracing": "^7.28.1" -> "^7.37.2",

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 2
  • Comments: 16 (7 by maintainers)

Most upvoted comments

@ThallesP Yes I looked into it and it is currently not possible to have a proper tracer that uses the client extension as it breaks out of async context. Luckily we will soon have full OTEL support so this is gonna be solved by that.

We’ve thought about adding manual instrumentation to prisma, but given that it was working without any issues before, we’d rather help surface this than going the “manual route”.

I recently noticed something similar going on in a test app of mine. I investigated a bit but couldn’t get to the root cause yet. What I found though is that manually instrumenting prisma in sentry.server.config.js seems to work:

import * as Sentry from '@sentry/nextjs';
import { getCurrentHub } from '@sentry/nextjs';
import { prismaClient } from './prisma/client';

prismaClient.$use(async (params, next) => {
  const scope = getCurrentHub().getScope();
  const parentSpan = scope?.getSpan();

  const action = params.action;
  const model = params.model;

  const span = parentSpan?.startChild({
    description: model ? `${model} ${action}` : action,
    op: 'db.sql.prisma',
  });

  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
  const rv: unknown = await next(params);

  span?.finish();

  // eslint-disable-next-line @typescript-eslint/no-unsafe-return
  return rv;
});

Sentry.init({
  dsn: 'dsn',
  tracesSampleRate: 1.0,
  environment: process.env.VERCEL_ENV || 'development',
  includeLocalVariables: true,
});