@unhandled/instrumentation-drizzle
v0.1.14
Published
Drizzle ORM query tracing for Unhandled
Readme
@unhandled/instrumentation-drizzle
Traces Drizzle ORM queries and prepared queries as CLIENT spans. Needs drizzle-orm >= 0.28.0 and your own database driver. This package is opt-in: the AdonisJS agent never enables it, and it has no /integration entry.
Install
npm i @unhandled/instrumentation-drizzleUse
Initialise OpenTelemetry first, then pick exactly one entry point per database.
Wrap the database returned by drizzle() (works with any Drizzle driver):
import { drizzle } from 'drizzle-orm/node-postgres'
import { instrumentDrizzleClient } from '@unhandled/instrumentation-drizzle'
export const db = instrumentDrizzleClient(drizzle(pool), { dbSystem: 'postgresql', dbName: 'app' })Or wrap the node-postgres Pool or Client before handing it to drizzle():
import { instrumentDrizzle } from '@unhandled/instrumentation-drizzle'
export const db = drizzle(instrumentDrizzle(pool, { dbSystem: 'postgresql', dbName: 'app' }))Do not use both on the same database. Every query would be traced twice, once at the Drizzle layer and once at the driver, with the driver span nested inside the other.
Config
| Option | Default | Effect |
| -------------------- | --------- | --------------------------------------------------------------------------- |
| dbSystem | unknown | Value of db.system.name: postgresql, mysql, sqlite. Set it. |
| dbName | none | db.namespace |
| peerName | none | server.address, the database host |
| peerPort | none | server.port |
| captureQueryText | true | false leaves db.query.text off every span |
| maxQueryTextLength | 1000 | Query text is cut to this many characters. Operation and table still come from the full SQL. |
SQL text can contain sensitive literals. Bound parameters are never recorded, and a failed query's span carries the driver's error message rather than Drizzle's wrapper, which embeds the parameters.
Spans
Every span is CLIENT kind, named <OPERATION> <table> (SELECT users), or just the operation when no table can be parsed. A prepared query produces one span per execution.
| Attribute | Value |
| -------------------- | -------------------------------------------------------- |
| db.system.name | dbSystem |
| db.query.text | SQL with placeholders, truncated, when captured |
| db.operation.name | Upper-cased first keyword, for example SELECT |
| db.collection.name | Target table when it can be parsed |
| db.namespace | dbName |
| server.address | peerName |
| server.port | peerPort |
| db.transaction | true for queries inside a transaction |
| error.type | Error class name on failures, plus the recorded exception and ERROR status |
Transactions
instrumentDrizzlesees what the connection sees, soBEGIN,COMMIT,ROLLBACK, savepoints and every query between them are spans flaggeddb.transaction.instrumentDrizzleClienttraces queries run throughdb.transactionand flags them. The control statements Drizzle issues itself (BEGIN,COMMIT,ROLLBACK, savepoints) are not traced.
Drizzle's own spans
When @opentelemetry/api is installed, Drizzle itself emits drizzle.* spans under the drizzle-orm scope. They are independent of this package and cannot be turned off from here.
Drivers
instrumentDrizzleClient patches the session behind db, so it is driver-agnostic. instrumentDrizzle wraps query() on a node-postgres Pool (including the clients it hands out) or Client; any other object throws a TypeError.
For AI agents
A coding agent can install this package, check that its data arrives and fix the errors it reports through the Unhandled MCP server at https://unhandled.net/mcp. In Claude Code, the Unhandled plugin adds that server plus skills for instrumenting a service, fixing a production error with a failing test first and verifying a deploy:
claude plugin marketplace add treboryx/unhandled
claude plugin install unhandled@unhandledCodex, Cursor, Gemini CLI and other agents connect to the same URL. Coding agents has the setup for each one and an AGENTS.md section to add to your repository.
