question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prisma opt-in with Nest.js doesn't work

See original GitHub issue

Environment

SaaS (https://sentry.io/)

Version

Everything latest

Steps to Reproduce

I’m trying to configure performance monitoring with Nest.js and Prisma ORM. Followed the steps here but I only get the http span in the event, I don’t see any DB related spans.

I also tried to use the @prisma/instrumentation package instead of the @prisma/client which is used in the docs, but nothing seems to work.

schema.prisma:

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["tracing"]
}

This is my basic main.ts file:

import { PrismaClient } from '@prisma/client';
import * as Sentry from '@sentry/node';
import * as Tracing from '@sentry/tracing';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import 'cross-fetch/polyfill'; // required for @microsoft/microsoft-graph-client
import dotenv from 'dotenv';
import AppModule from './app.module';

async function bootstrap() {
  dotenv.config();
  const app = await NestFactory.create(AppModule);
  const httpAdapter = app.getHttpAdapter();

  Sentry.init({
    dsn: 'https://xxxxxx',
    integrations: [
      new Sentry.Integrations.Http({ tracing: true }),
      new Tracing.Integrations.Express({
        app: httpAdapter.getInstance(),
      }),
      new Tracing.Integrations.Prisma({ client: new PrismaClient() }),
    ],
    tracesSampleRate: 1.0,
  });

  app
    .use(Sentry.Handlers.requestHandler())
    .use(Sentry.Handlers.tracingHandler())
    .use(Sentry.Handlers.errorHandler());
  await app.listen(app.get(ConfigService).get('PORT') || 3000);
}
// eslint-disable-next-line @typescript-eslint/no-floating-promises
bootstrap();

Expected Result

I should see DB spans in the transactions

Actual Result

Example event from sentry, without any DB span, only HTTP:

image

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
AbhiPrasadcommented, Aug 30, 2022

We haven’t invested in a first class NestJS SDK - if you want to see that happen please let us know here: https://github.com/getsentry/sentry-javascript/discussions/5578. Backlogging this for now - but PRs welcome!

0reactions
tomgrossmancommented, Aug 30, 2022

Hey thanks for writing in!

new Tracing.Integrations.Prisma({ client: new PrismaClient() }),

Looking at this line, the issue might be that you’re constructing a brand new prisma client, instead of passing down a reference to the existing prisma client you use for your database queries. This means we are not monitoring the prisma client actually beign used to make the query. It seems like from the docs that the prisma client is dependency injected into your app, not sure if there’s an easy way to access it outside of that. I would ask the NestJS folks on advice from that.

@AbhiPrasad yea I also tried to access the PrismaService and not the client, but same results. I guess someone in Sentry tested it with NestJs and Prisma and knows the best way to make it work, I would add it to the docs since it just doesn’t work the current way. What I did for now is adding to the $use Prisma middleware a span with op: 'db.prisma' after getting the transaction. It seems to work, but sounds like it should be better instrumented.

Read more comments on GitHub >

github_iconTop Results From Across the Web

I get "Can't resolve '_http_common'" when I use NestJS with ...
When I try to composite Prisma with NestJS monorepo project I get the error. I can`t use default prisma client output path because...
Read more >
Typescript not detecting Prisma optional fields - Stack Overflow
I'm creating seed files for my NestJS ...
Read more >
Prisma | NestJS - A progressive Node.js framework
Prisma is an open-source ORM for Node.js and TypeScript. It is used as an alternative to writing plain SQL, or using another database...
Read more >
Applying Full Stack Type Safety with Angular, Nest, Nx & Prisma
In this article, we'll look at how to apply type safety to every part of a full stack Angular and NestJS app, including...
Read more >
Building a REST API with NestJS and Prisma - YouTube
In this hands-on workshop, you'll learn how to build a REST API with NestJS and the Prisma ORM in TypeScript.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found