drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
23.06k stars 556 forks source link

Observability support #371

Open kibertoad opened 1 year ago

kibertoad commented 1 year ago

APM solutions, such as DataDog and New Relic can benefit from execution traces, such as OpenTelemetry, in order to provide useful runtime information about how queries are performing.

See how Prisma did it: https://github.com/prisma/prisma/tree/main/packages/instrumentation

ghost commented 1 year ago

It is worth mentioning that currently it is possible to instrument the driver library itself using instrumentation libraries provided by otel:

pg - https://www.npmjs.com/package/@opentelemetry/instrumentation-pg mysql - https://www.npmjs.com/package/@opentelemetry/instrumentation-mysql

gustavolzangelo commented 1 year ago

It is worth mentioning that currently it is possible to instrument the driver library itself using instrumentation libraries provided by otel:

pg - https://www.npmjs.com/package/@opentelemetry/instrumentation-pg mysql - https://www.npmjs.com/package/@opentelemetry/instrumentation-mysql

@bohdan-sviripa Did you manage to make it work with Drizzle?

issackr commented 1 year ago

@gustavolzangelo I opened #1054 and tried it on my project.

I was able to work on drizzle with pg, and get the instrumentation working properly with instrumentation-pg

jstlaurent commented 9 months ago

There's already OpenTelemetry traces set up throughout the pg-core module. I've already setup Otel tracing in the rest of my app and it would be great to be able to turn it on for Drizzle as well.

i-am-mani commented 8 months ago

Does any similar library exists for OTEL Tracing for Planetscale? any suggestions on how one could include automatic tracing for queries?

pudgereyem commented 7 months ago

Also curious if there's any documentation on how you best go about setting up OpenTelemetry tracing when using Drizzle. I've done this with Prisma in the past and benefited from documentation such as the links below:

  1. Docs - OpenTelemetry tracing
  2. Monitor Your Server with Tracing Using OpenTelemetry & Prisma

Can you point us in the right direction here @AndriiSherman, @Angelelz? Thanks for a great ORM!

Angelelz commented 7 months ago

Drizzle doesn't have official support for this yet. At some point the team stated implementing something like that but it is not done yet.

bearkfear commented 4 months ago

for postgres you can use this packages to track traces

instrumentation libraries:

probably will be like this:

image

instrumentations: [
                new PgInstrumentation({
                    enhancedDatabaseReporting: true,
                    enabled: true,
                    addSqlCommenterCommentToQueries: true,
                }),
            ],
i-am-mani commented 4 months ago

We use MySQL (Planetscale) and for us patching the db instance (or js Proxy) at the entry-point of package with manual tracing worked out. We only use it for dev/staging, no performance or other issues found so far.

Eg.

    Connection.prototype.execute = async function (query, values, options) {
      return trace.getTracer('database').startActiveSpan(query.toString(), async (span) => {
        span.setAttribute('db.statement', query);
        const _res = await originalMethod.call(this, query, values as any, options as any);
        span.end();
        return _res;
      });
    };
AlexXanderGrib commented 1 month ago

Suggestion:

OR even simpler:

Add this to src/drizzle-orm/tracing.ts

let otel: typeof import('@opentelemetry/api') | undefined;

export function setGlobalOtelApi(api: typeof import('@opentelemetry/api') | undefined): void {
  otel = api;
}

And this to index.ts

export { setGlobalOtelApi } from './tracing.ts';

OpenTelemetry itself uses global configuration, for example when starting Node SDK, so i believe usage of globals if appropriate here

Jonatthu commented 3 weeks ago

@AlexXanderGrib Could you do a pull request?

AlexXanderGrib commented 3 weeks ago

I tried vite rewrites and found out that drizzle tries to serialize query params to JSON for span attribues. But query params can be BigInt so it blows up