Skip to content

Conversation

@dohrisalim
Copy link

I have successfully implemented support for Node.js Diagnostic Channels in Express.js. This allows for fine-grained observability of the request lifecycle, middleware execution, and route matching without the need for monkey-patching.

Changes Made

Core Infrastructure

Created diagnostics.js to centralize diagnostic channel definitions using node:diagnostics_channel.
Defined the following channels:

  • express.request.start
  • express.request.end
  • express.request.error
  • express.middleware.start
  • express.middleware.end
  • express.middleware.error
  • express.route.match

Request Lifecycle Events

Modified application.js to publish events at the start and end of each request in app.handle.
Used on-finished to reliably detect request completion and errors for diagnostic publishing.

Middleware and Route Match Events

Implemented a wrapMiddleware helper in application.js to intercept middleware execution.
Added support for wrapping middleware in app.use, app.param, and all HTTP verb methods (e.g., app.get, app.post).
Ensured transparency by preserving function length and maintaining req.next consistency to avoid breaking existing Express logic and tests.
Implemented express.route.match publishing when a route is first encountered during middleware execution.

Verification Results

Automated Tests

Created test/diagnostics.js to specifically verify all new diagnostic events.
Ran the full Express test suite (npm test) to ensure no regressions were introduced.
Result: All 1243 existing tests and the 5 new diagnostic tests PASSED.

> express@5.2.1 test
> mocha --require test/support/env --reporter spec --check-leaks test/ test/acceptance/
  ...
  1243 passing (4s)
  diagnostics
    ✔ should publish express.request.start
    ✔ should publish express.request.end
    ✔ should publish express.request.error
    ✔ should publish express.middleware events
    ✔ should publish express.route.match
  5 passing (58ms)

Performance Consideration

All diagnostic publishing is guarded by channel.hasSubscribers checks, ensuring zero to minimal overhead when no subscribers are active.

Implements Node.js diagnostics_channel support for Express:
- express.request.start/end/error for request lifecycle
- express.middleware.start/end/error for middleware timing
- express.route.match for route matching
Closes expressjs#6353
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