Skip to content

Conversation

@logaretm
Copy link

@logaretm logaretm commented Nov 9, 2025

This PR adds tracing instrumentation for middleware and fetch handlers using Node.js diagnostics/tracing channels, enabling integration with observability tools like OpenTelemetry and Sentry.

Implementation

  • Emits srvx.middleware and srvx.fetch tracing events with full lifecycle hooks (start, end, asyncStart, asyncEnd, error)
  • Includes request context, server info, middleware names, and execution order in trace data

I think this has to be available out of the box, so it "just works" with SDK providers rather than OPT in.

Example usage

import { tracingChannel } from 'node:diagnostics_channel';

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

middlewareChannel.subscribe({
  start: (data) => console.log(`${data.name} started`),
  asyncEnd: (data) => console.log(`${data.name} completed`),
  // ... asyncStart, end, error
});

Span Relationships

Something I noticed is 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

Now this can be fine since 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
  • test it out with express, hono, and other frameworks.

@logaretm logaretm changed the title feat: add tracing channels for fetch and middleware operations feat: add tracing channels for fetch and middleware handlers Nov 9, 2025
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.

2 participants