Skip to content

feat: Add tracing channel support#16275

Open
logaretm wants to merge 2 commits into
Automattic:masterfrom
logaretm:feat/tracing-channel-support
Open

feat: Add tracing channel support#16275
logaretm wants to merge 2 commits into
Automattic:masterfrom
logaretm:feat/tracing-channel-support

Conversation

@logaretm
Copy link
Copy Markdown

@logaretm logaretm commented May 7, 2026

This PR explores adding tracing channels to enable agnostic observability for users and APMs alike without having to resort to monkey patching or module patchers.

Unlike the original proposal in #16105, I realized that one channel is cleaner and allows for creating all the needed span descriptions that currently exist in the OTEL instrumentation. This keeps the changes small and managable.

Implementation

So a singlemongoose:query channel that emits the following payload:

Field Type Description
operation string The operation name (e.g. find, updateOne, aggregate)
collection string MongoDB collection name
database string Database name
serverAddress string DB host
serverPort number DB port
args Record<string, any> Operation-specific arguments

I also exported the types for use by consumers.

Example Consumer

const dc = require('node:diagnostics_channel');

const channel = dc.tracingChannel('mongoose:query');

channel.subscribe({
  start({ operation, collection, database, serverAddress, serverPort, args }) {
    console.log('[query:start]', operation, collection, args);
  },
  asyncEnd({ operation, collection, result }) {
    console.log('[query:end]', operation, collection);
  },
  error({ operation, collection, error }) {
    console.error('[query:error]', operation, collection, error);
  }
});

this is what it looks like after consuming the events in an APM, which is inline with current instrumentations today.

CleanShot 2026-05-07 at 13 08 23@2x

closes #16105

logaretm added 2 commits May 5, 2026 14:19
Add first-class Node.js TracingChannel (diagnostics_channel) support
so APM tools can instrument Mongoose without monkey-patching.

Four channels covering all operation types:
- mongoose:query (Query.exec - find, update, delete, count, distinct)
- mongoose:aggregate (Aggregate.exec)
- mongoose:save (Model.prototype.save)
- mongoose:model (insertMany, bulkWrite)

Zero-cost when no subscribers are registered.

Ref: Automattic#16105
Collapse 4 channels into one. All operations (find, save, aggregate,
insertMany, bulkWrite) emit on mongoose:query with a common payload:
operation, collection, database, serverAddress, serverPort, args.

The args field carries operation-specific data (filter, pipeline, docs,
ops) without imposing a typed shape — APM tools that need it can dig in.
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.

TracingChannel Proposal for observability

1 participant