Skip to content

feat: initial support for diagnostics_channel TracingChannel API#16137

Open
Neha-Sharmaaa wants to merge 5 commits intoAutomattic:masterfrom
Neha-Sharmaaa:feat/diagnostics-channel
Open

feat: initial support for diagnostics_channel TracingChannel API#16137
Neha-Sharmaaa wants to merge 5 commits intoAutomattic:masterfrom
Neha-Sharmaaa:feat/diagnostics-channel

Conversation

@Neha-Sharmaaa
Copy link
Copy Markdown

Problem in current implementation

Currently, monitoring Mongoose operations from APM tools requires monkey-patching which is brittle and can lead to performance regressions.

Objective

Enable APM tools (like OpenTelemetry) to instrument Mongoose without monkey-patching, by emitting tracing lifecycle events using diagnostics_channel.

Scope

Implement support for:

  • Query.prototype.exec
  • Aggregate.prototype.exec
  • Model.prototype.save

Features

  • Follows the tracing lifecycle flow: start → operation → asyncEnd (with error on failure).
  • Zero overhead when no subscribers are active.
  • Contextual objects for each operation including filters, pipelines, and collection names.
  • Compatible with Node.js 18+ and handles Node 20.x stable branch APIs correctly.

Note: This PR is now correctly separated from the populate-limit optimization PR.

@Neha-Sharmaaa
Copy link
Copy Markdown
Author

Usage Example and Verification

You can subscribe to these channels to monitor Mongoose operations without monkey-patching:

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

// Monitor all Mongoose queries
dc.tracingChannel('mongoose:query').subscribe({
  start(ctx) {
    console.log('Query Start:', ctx.operation, 'on', ctx.collection);
    console.log('Filter:', ctx.query);
  },
  asyncEnd(ctx) {
    console.log('Query End');
  },
  error(ctx) {
    console.error('Query Error:', ctx.error);
  }
});

// Works similarly for 'mongoose:aggregate' and 'mongoose:save'

Verified locally with a new test suite (test/diagnostics_channel.test.js) in Node v20.19.4.

@Neha-Sharmaaa Neha-Sharmaaa force-pushed the feat/diagnostics-channel branch from d205e06 to a056e17 Compare March 23, 2026 05:26
@Neha-Sharmaaa
Copy link
Copy Markdown
Author

If CI checks aren't showing, a maintainer may need to approve workflow runs from this fork: go to the Actions tab, open the pending workflow run for this PR, and click Approve and run.

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