Skip to main content

Overview

@kubiks/otel-drizzle provides comprehensive OpenTelemetry instrumentation for Drizzle ORM. Add distributed tracing to your database queries with a single line of code—supports PostgreSQL, MySQL, and SQLite.
Drizzle ORM Trace Visualization
Visualize your database queries with detailed span information including operation type, SQL statements, and performance metrics.

Installation

Peer Dependencies: @opentelemetry/api >= 1.9.0, drizzle-orm >= 0.28.0

Supported Frameworks

Works with any TypeScript framework and Node.js runtime that Drizzle supports:

Next.js

App Router & Pages Router

Fastify

High-performance server

NestJS

Enterprise framework

Express

Classic Node.js server

Remix

Full-stack framework

SvelteKit

Modern web framework

Supported Platforms

Works with any observability platform that supports OpenTelemetry:

Quick Start

Use instrumentDrizzleClient() to add tracing to your Drizzle database instance:
This is the simplest and most straightforward approach—just wrap your existing Drizzle instance!

Database-Specific Setup

Configuration Options

By default, SQL queries are captured in spans. You can disable this by setting captureQueryText: false for sensitive environments.

What You Get

Each database query automatically creates a span with rich telemetry data:
  • Span name: drizzle.select, drizzle.insert, drizzle.update, etc.
  • Operation type: db.operation attribute (SELECT, INSERT, UPDATE, DELETE, SET)
  • SQL query text: Full query statement captured in db.statement (configurable)
  • Database system: db.system attribute (postgresql, mysql, sqlite, etc.)
All queries within transactions are automatically traced, including:
  • RLS (Row Level Security) queries like SET LOCAL role and set_config()
  • All nested transaction queries
  • Transaction rollbacks and commits
  • Exceptions are recorded with stack traces
  • Proper span status (OK, ERROR)
  • Error messages and types
  • Duration and timing information for every query
  • Query execution time
  • Database connection time

Span Attributes

The instrumentation adds the following attributes to each span following OpenTelemetry semantic conventions:

Usage Examples

Basic Queries

Transactions

Transaction queries are automatically marked with db.transaction attribute.

Complex Queries

Row Level Security (PostgreSQL)

Complete Integration Example

Here’s a complete example of Drizzle ORM with OpenTelemetry in a Next.js application:
lib/db.ts
db/schema.ts
app/api/users/route.ts

Best Practices

Always use connection pooling in production:
Disable SQL capture in production if queries contain sensitive data:
Always use transactions for multi-step operations:
Monitor slow queries in your traces and add indexes:

Performance Considerations

The instrumentation adds minimal overhead (~1-2ms per query) for tracing operations.
Use OpenTelemetry sampling to reduce data volume in high-traffic applications:
Use batch span processors for better performance:

Troubleshooting

Ensure OpenTelemetry is initialized before instrumenting Drizzle:
Check that captureQueryText is enabled:
Make sure you’re using Drizzle’s transaction API correctly:

Resources

Drizzle ORM Documentation

Learn more about Drizzle ORM

GitHub Repository

View source code and examples

npm Package

View package on npm

Report Issues

Found a bug? Let us know!

License

MIT