Skip to content

Add Support for Prisma ORM #3143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JonathanCallewaert opened this issue Dec 24, 2020 · 12 comments
Closed

Add Support for Prisma ORM #3143

JonathanCallewaert opened this issue Dec 24, 2020 · 12 comments
Assignees

Comments

@JonathanCallewaert
Copy link

Package + Version

  • [ x ] @sentry/integrations - ^5.27.3
  • [ x ] @sentry/node - ^5.27.3
  • [ x ] @sentry/tracing - ^5.29.2

Description

I want to use the Postgres Tracing Integrations mentioned in these docs: https://docs.sentry.io/platforms/node/performance/database/
But for some reason, it does not work.
I'm using Prisma to do my Postgres queries: https://github.com/prisma/prisma
Underlying, Prisma is using the pg client package: https://github.com/prisma/prisma/blob/master/src/packages/client/src/utils/setupPostgres.ts

My code looks like this:

 Sentry.init({
      dsn: '***',
      environment: env,
      integrations: [
        new ExtraErrorData({ depth: 20 }),
        app && new Sentry.Integrations.Http({ tracing: true }),
        app && new Tracing.Integrations.Express({ app }),
        // Only works with pg client. Wait on support for Prisma
        app && new Tracing.Integrations.Postgres(),
      ].filter(Boolean),
      tracesSampleRate: 1.0,
      normalizeDepth: 21,
    });
@jonahsnider
Copy link

That function is only used in test scripts internally. Regular Prisma users have the Prisma engines connect to the database. The Node.js app never connects directly to the database.

@Jolg42
Copy link

Jolg42 commented Apr 13, 2021

@pizzafox is correct here.

@kamilogorek kamilogorek changed the title @sentry/tracing postgres not working when using Prisma Add Support for Prisma ORM Apr 14, 2021
@kamilogorek
Copy link
Contributor

Thanks for the info. I allowed myself to change the title of this issue then.

@viperfx
Copy link

viperfx commented May 25, 2021

Hope this gets more attention. How are people working around this? Has anyone managed to configure it for better stack traces?

@stnwk
Copy link

stnwk commented Jul 24, 2021

I took a look at how this is implemented for the pg integration: https://github.com/getsentry/sentry-javascript/blob/master/packages/tracing/src/integrations/postgres.ts and applied prisma's middleware concept to it: https://www.prisma.io/docs/concepts/components/prisma-client/middleware

Here's what worked for me:

  import { PrismaClient } from "@prisma/client";
  import { getCurrentHub, Severity } from "@sentry/nextjs"; // whatever package you're using

  prisma.$use(async (params, next) => {
    const { model, action, runInTransaction, args } = params;
    const description = [model, action].filter(Boolean).join(".");
    const data = {
      model,
      action,
      runInTransaction,
      args,
    };

    const scope = getCurrentHub().getScope();
    const parentSpan = scope?.getSpan();
    const span = parentSpan?.startChild({
      op: "db",
      description,
      data,
    });

    // optional but nice
    scope?.addBreadcrumb({
      category: "db",
      message: description,
      data,
    });

    const result = await next(params);
    span?.finish();

    return result;
  });

@github-actions
Copy link
Contributor

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@patryk-smc
Copy link

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!

"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

Leave it

@smeubank
Copy link
Member

this is shipped in betas for V7! 🚀

we will close this request and would love to get any feedback on the usage!

@Noitidart
Copy link

Noitidart commented Jul 1, 2022

So I read that Prisma is not auto-instrumented, but I can't figure out how to run setupOnce or whatever. Is this documented somewhere? I can't figure out how to import Sentry Prisma stuff :(

Edit:

I'm getting closer, I found the import:

import { Integrations } from '@sentry/tracing';

const PrismaSentry = Integrations.Prisma;

However I'm not sure what to pass to the setupOnce:

const prisma = new PrismaClient();
new PrismaSentry({ client: prisma }).setupOnce();

Apparently setupOnce needs some args which I don't understand.

@MaxBittker
Copy link
Contributor

MaxBittker commented Jul 3, 2022

@Noitidart it is confusing, but I believe that you don't need to call setup yourself, but instead pass into your sentry init like this:

https://github.com/MaxBittker/sandspiel-studio/blob/main/sentry.server.config.js#L17

@Noitidart
Copy link

Oh thanks @MaxBittker I'll try that out.

I noticed we don't have params.args or params.runInTransaction in the data of the span. Is it possible to add this?

Also I noticed that in some errors, if it happens after the span starts, and prevents the span from finishing, then we don't see that data fire up for that request. One such error is permission to db user is denied. So maybe we should also add a type "query" category "started" breadcrumb with this data?

@supalarry
Copy link

I took a look at how this is implemented for the pg integration: https://github.com/getsentry/sentry-javascript/blob/master/packages/tracing/src/integrations/postgres.ts and applied prisma's middleware concept to it: https://www.prisma.io/docs/concepts/components/prisma-client/middleware

Here's what worked for me:

  import { PrismaClient } from "@prisma/client";
  import { getCurrentHub, Severity } from "@sentry/nextjs"; // whatever package you're using

  prisma.$use(async (params, next) => {
    const { model, action, runInTransaction, args } = params;
    const description = [model, action].filter(Boolean).join(".");
    const data = {
      model,
      action,
      runInTransaction,
      args,
    };

    const scope = getCurrentHub().getScope();
    const parentSpan = scope?.getSpan();
    const span = parentSpan?.startChild({
      op: "db",
      description,
      data,
    });

    // optional but nice
    scope?.addBreadcrumb({
      category: "db",
      message: description,
      data,
    });

    const result = await next(params);
    span?.finish();

    return result;
  });

If this runs in node js, but the queries are actually ran by prisma's query engine in rust under the hood, are these spans accurate? I measured time that const result = await next(params); takes and then what is reported by prisma if using (what the query engine under the hood actually ran)

client.$on("query", (e) => {
    console.log(e.query);
    console.log(e.duration);
  });

Then time measured on the nodejs side is longer than the query actually took. Just wanted to note this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests