Skip to main content
This guide shows how to instrument Drizzle for query monitoring. You will learn how to:
  • Wrap your Drizzle database or underlying client
  • Configure which query details are recorded

Overview

@monocle.sh/instrumentation-drizzle creates OpenTelemetry spans for Drizzle queries. Initialize your OpenTelemetry SDK or agent before creating the database, then wrap the Drizzle instance with instrumentDrizzleClient(). The instrumentation is opt-in and is not enabled automatically by the Monocle agent.

Installation

The package requires Drizzle ORM 0.28.0 or later. Install your database driver separately. This example uses pg.
Terminal

Instrument your Drizzle database

Wrap the instance returned by drizzle() before using it. Set dbSystem so Monocle can identify the database in query details.
app/services/database.ts
Queries and prepared queries create database spans. Queries run inside a transaction are marked with db.transaction, and query errors are recorded on their spans. If you want to instrument the underlying client directly, use instrumentDrizzle() before passing the client to Drizzle:
app/services/database.ts
Choose one entry point for a database. Wrapping both the Drizzle instance and its underlying client can create duplicate spans for a query.

Configure query details

Pass these options to either instrumentation function:
  • dbSystem identifies the database, for example postgresql, mysql, or sqlite. It defaults to unknown.
  • dbName, peerName, and peerPort add database name and connection details to spans.
  • captureQueryText includes SQL statements in spans by default. Set it to false to omit SQL text.
  • maxQueryTextLength sets the maximum SQL statement length. It defaults to 1,000 characters.
The instrumentation creates CLIENT spans with database attributes such as the operation and table name when they can be read from the SQL statement. Transaction queries are marked with db.transaction.