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.
  • Figure out how to handle streams

@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
@logaretm logaretm force-pushed the awad/add-tracing-channels branch from 0ca8e01 to 4c49d41 Compare November 24, 2025 22:41
@coderabbitai
Copy link

coderabbitai bot commented Nov 24, 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.

Copy link
Member

Choose a reason for hiding this comment

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

We should probably split addons as an opt-in plugin that modifies registred middleware/routes

Copy link
Author

@logaretm logaretm Dec 1, 2025

Choose a reason for hiding this comment

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

Oh sorry, I just saw this, so something like this?

import { serve } from "srvx";
import { tracingPlugin } from 'srvx/tracing'

const server = serve({
  fetch: () => {
    // ...
  },
  plugins: [tracingPlugin],
});

Copy link
Member

Choose a reason for hiding this comment

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

Yes!

Copy link
Author

Choose a reason for hiding this comment

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

All done!

@logaretm logaretm force-pushed the awad/add-tracing-channels branch from a89a68d to c8e96a0 Compare December 1, 2025 14:42
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