@unhandled/instrumentation-kysely
v0.1.14
Published
Kysely query tracing and pg pool metrics for Unhandled
Readme
@unhandled/instrumentation-kysely
Traces Kysely queries, streams and transactions, and exports pg pool gauges. Supports Kysely ^0.28.16 and ^0.29.6 with PostgresDialect (pg) and PostgresJSDialect (kysely-postgres-js).
This package is manual only. It has no /integration entry, so the AdonisJS agent never enables it for you.
Install
npm i @unhandled/instrumentation-kyselyUse
Create the dialect after OpenTelemetry is initialised and pass the wrapped dialect to Kysely:
import { Kysely, PostgresDialect } from 'kysely'
import pg from 'pg'
import { observeDialect } from '@unhandled/instrumentation-kysely'
const pool = new pg.Pool({ host, port, database, user, password, max: 10 })
export const db = new Kysely<Database>({
dialect: observeDialect({
dialect: new PostgresDialect({ pool }),
dbSystem: 'postgresql',
connection: { host, port, database },
}),
})Options
| Option | Default | Effect |
| ------------------- | -------- | --------------------------------------------------------------------------------------------- |
| dialect | required | The dialect to wrap. |
| dbSystem | required | Value of db.system.name, for example postgresql. |
| recordQueryText | true | false leaves db.query.text off every span. |
| recordErrors | true | false keeps ERROR status but drops the status message and the exception event. |
| requireParentSpan | false | Only create spans when a span is already active. |
| connection | none | { host, port, database }, reported as server.address, server.port and db.namespace. |
SQL text and error messages can contain sensitive data. Query parameters are never recorded, but literals inlined in SQL are. Turn off recordQueryText and recordErrors if that matters.
Spans
Every span is CLIENT kind. The name is <OPERATION> <table> (SELECT users), or just the operation when no table can be parsed, with the operation and table read from the SQL text. A transaction produces BEGIN, one span per query, then COMMIT or ROLLBACK. A stream is one span that lasts until the stream is drained, closed or fails.
| Attribute | Value |
| -------------------- | ----------------------------------------------------------------------- |
| db.system.name | dbSystem |
| db.query.text | SQL with placeholders, when recordQueryText is on |
| db.operation.name | Upper-cased first keyword, for example SELECT |
| db.collection.name | Target table when it can be parsed |
| db.namespace | connection.database |
| server.address | connection.host |
| server.port | connection.port |
| db.transaction | true on BEGIN, COMMIT, ROLLBACK and queries inside a transaction |
| error.type | Error class name on failures |
Pool metrics
import { registerPgPoolMetrics } from '@unhandled/instrumentation-kysely'
const stop = registerPgPoolMetrics({ pool, max: 10, poolName: 'primary' })
// later, before pool.end()
stop()Call it after the metrics provider is set up. Only pg.Pool is supported; anything else throws a TypeError. postgres.js exposes no pool counters.
| Gauge | Meaning |
| ---------------------------------------- | ---------------------------------------- |
| db.client.connection.used | Connections checked out |
| db.client.connection.idle | Open connections sitting idle |
| db.client.connection.pending_requests | Callers waiting for a connection |
| db.client.connection.max | The max you passed |
Each point carries db.client.connection.pool.name set to poolName.
Duplicate spans
If you also enable OpenTelemetry's pg instrumentation, every query is traced twice, once here and once at the driver. Keep one of them. postgres.js has no OpenTelemetry coverage, so wrapping its dialect here causes no duplicates.
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.
