Skip to content

Conversation

@logaretm
Copy link

@logaretm logaretm commented Dec 1, 2025

As per the discussion I created, this PR implements support for tracing channel events, allow subscribers to create instrumentations without monkey patching.

Implementation

  • Emits h3.request.handler tracing events with full lifecycle hooks (start, end, asyncStart, asyncEnd, error)
  • Includes request context.

Example usage

import { tracingChannel } from 'node:diagnostics_channel';

const middlewareChannel = tracingChannel('srvx.middleware');

middlewareChannel.subscribe({
  start: (data) => console.log(`${data.event.req.method} ${data.event.url.pathname}`),
  asyncEnd: (data) => console.log(`${data.event.req.method} completed`),
  // ... asyncStart, end, error
});

Span Relationships

Since we have an onion effect here with each middleware being able to wait for the response/next, then it means if the SDK provider isn't careful, they might end up creating middleware spans as children of one another rather than siblings.

HTTP Request
└── Middleware: auth
			└── Middleware: cors
						└── Middleware: logging
									└── Fetch Handler

This is fine at h3 level because it is technically correct from an execution standpoint, but each provider can handle this manually when subscribing to those diagnostic events, and they can manually unscope each span from the previous one to get the desired effect.

HTTP Request
├── Middleware: auth
├── Middleware: cors
├── Middleware: logging
└── Fetch Handler

TODO:

  • Figure out a way to fix the nested middleware trace calls
  • Figure out how to handle streams?

@coderabbitai
Copy link

coderabbitai bot commented Dec 1, 2025

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Comment @coderabbitai help to get the list of available commands and usage tips.

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