Skip to content

Conversation

@logaretm
Copy link

@logaretm logaretm commented Nov 7, 2025

Adds Node.js tracing channels for db query operations, enabling observability and performance monitoring.

Channel Naming Convention

All channels use the db0.query convention which when coupled with tracing channels produce the following diagnostic channels:

  • tracing:db0.query:start
  • tracing:db0.query:end
  • tracing:db0.query:asyncStart
  • tracing:db0.query:asyncEnd
  • tracing:db0.query:error

Which matches nicely with fastify but there isn't a lot of convention for them other than the Node.js recommendation.

Unlike unstorage it didn't feel like we need multiple channels since more or less it boils down to executing a query.

Tracing context includes

  • query - query to be executed
  • method - source of the query like sql or exec, basically the method name.
  • dialect - the db dialect

Example

I have made it so it is opt-in like unstorage.

Users can create traced instances like so:

// opt in
import { withTracing, createDatabase } from "db0";
import sqlite from 'db0/connectors/node-sqlite';

// Create traced DB
const db = withTracing(
  createDatabase(
    sqlite({
      name: 'db',
    })
  )
);

SDKs and consumers can then do:

import { tracingChannel } from 'node:diagnostics_channel';

const channel = tracingChannel('db0.query');

channel.subscribe({
  start: (data) => {
    // start timing, or spans or whatever...
  },
  asyncEnd: (data) => {
    console.log(`${data.driver?.name} fetched ${data.keys[0]} in ${duration}ms`);
  },
  error: (data) => {
    console.error(`Error in ${data.driver?.name}:`, data.error);
  }
});

All operations emit standard tracing events: start, end, asyncStart, asyncEnd, and error.

Copilot AI review requested due to automatic review settings December 11, 2025 11:19
@logaretm logaretm force-pushed the awad/add-tracing-channels branch from 375b187 to 0ae9a84 Compare December 11, 2025 11:19
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds Node.js tracing channels support to db0, enabling observability and performance monitoring for database query operations. The implementation follows an opt-in approach where users must explicitly wrap their database instance with withTracing() to enable tracing.

Key Changes:

  • Introduces withTracing() wrapper function that instruments database operations with Node.js diagnostic channels
  • Adds comprehensive test coverage for all traced database operations (exec, sql, prepare.all, prepare.run, prepare.get)
  • Exports tracing functionality and types through the main index

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
src/tracing.ts Implements the core tracing functionality with withTracing() wrapper and channel management for db query operations
test/tracing.test.ts Provides comprehensive test coverage for tracing behavior including opt-in verification, success/error scenarios, and query reconstruction
src/index.ts Exports the new tracing API (withTracing, TraceContext, TracedOperation) for public consumption

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant