Learn how to configure Datadog tracing for a Prisma ORM project. Capture spans for every query using the @prisma/instrumentation package, dd-trace, and view them in Datadog
In this guide, you'll learn how to set up Datadog tracing for a new Prisma project. By combining the @prisma/instrumentation package with Prisma Client extensions, you can capture detailed spans for every database query. These spans are enriched with query metadata and sent to Datadog using dd-trace, Datadog's official APM library for Node.js, so you can see how each query performs inside your application's requests.
Spans are the individual operations or units of work within a distributed system or complex application. Each database query, service call, or external request is represented by a span.
Tracing ties these spans together to form a complete, end-to-end picture of a request's lifecycle. With tracing, you can visualize bottlenecks, identify problematic queries, and pinpoint where errors occur from your queries.
Datadog provides application performance monitoring (APM), metrics, logs, and dashboards to help you observe and debug production systems.
Prisma ORM generates the SQL for you, which can hide how each query performs unless you instrument it. By integrating Datadog with Prisma using @prisma/instrumentation and dd-trace, you can automatically capture spans for every database query.
This enables you to:
Measure latency per query.
Inspect query arguments and raw SQL.
Trace Prisma operations in the context of application-level requests.
Identify bottlenecks related to database access.
With this in place, slow queries and errors show up in Datadog as they happen.
A Datadog account. If you do not have one, sign up here.
The Datadog Agent installed and running on your machine or server where this application will run. You can follow the Datadog Agent installation docs to set it up.
We will start by creating a new Node.js project to demonstrate tracing with Datadog and Prisma ORM. This will be a minimal, standalone setup focused on running and tracing Prisma queries, to understand the instrumentation flow in isolation.
If you're integrating tracing into an existing Prisma project, you can skip this step and directly follow from the setup tracing section. Apply the changes in your project's equivalent folder structure.
bun
mkdir prisma-datadog-tracing
cd prisma-datadog-tracing
bun init
pnpm
mkdir prisma-datadog-tracing
cd prisma-datadog-tracing
pnpm init
yarn
mkdir prisma-datadog-tracing
cd prisma-datadog-tracing
yarn init
npm
mkdir prisma-datadog-tracing
cd prisma-datadog-tracing
npm init
In this setup, you'll:
Define a Prisma schema with basic models.
Connect to a Postgres database (Prisma Postgres or your own).
Configure Datadog tracing for all queries using @prisma/instrumentation and dd-trace.
Run a sample script that executes Prisma operations and sends spans to Datadog.
In this section, you will install Prisma, create your schema, and generate the Prisma Client. This prepares your application to run the database queries that you will trace with Datadog.
Creates a prisma directory with a schema.prisma file.
Generates the Prisma Client in the /src/generated/prisma directory (as specified in the --output flag).
Creates a .env file at the project root with your database connection string (DATABASE_URL).
The .env file should contain a standard connection string:
.env
# Placeholder url you have to replace
DATABASE_URL="postgresql://janedoe:mypassword@localhost:5432/mydb?schema=sample"
Install the driver adapter for PostgreSQL:
bun
bun add @prisma/adapter-pg pg
pnpm
pnpm add @prisma/adapter-pg pg
yarn
yarn add @prisma/adapter-pg pg
npm
npm i @prisma/adapter-pg pg
bun
bun add --dev @types/pg
pnpm
pnpm add -D @types/pg
yarn
yarn add --dev @types/pg
npm
npm i -D @types/pg
Create a Prisma Postgres database and replace the generated DATABASE_URL in your .env file with the postgres://... connection string from the CLI output:
Now, open prisma/schema.prisma and update your generator block and models. Replace the generator block with the following, and add a User and a Post model:
prisma/schema.prisma
generator client { provider = "prisma-client" output = "../src/generated/prisma"}datasource db { provider = "postgresql"}model User { id Int @id @default(autoincrement()) email String @unique name String? posts Post[]}model Post { id Int @id @default(autoincrement()) title String content String? published Boolean @default(false) authorId Int author User @relation(fields: [authorId], references: [id])}
Create a prisma.config.ts file to configure Prisma:
The setup above gives you more control over how queries are traced:
Tracing is initialized as early as possible by importing the tracer before creating the Prisma Client.
The $on("query") hook captures raw SQL queries and sends them as standalone spans.
The $allOperations extension wraps all Prisma operations in custom spans, allowing you to tag them with metadata like the model, operation type, and arguments.
Unlike the @prisma/instrumentation package, which offers automatic tracing out of the box, this manual setup gives you full control over how each span is structured and tagged. It's helpful when you need custom span names, additional metadata, a simpler setup, or when working around limitations or compatibility issues in the OpenTelemetry ecosystem. It also allows you to adapt tracing behavior based on query context, which can be especially useful in complex applications.