For years, monitoring an Express app meant monkey patching: APM tools wrapped app.use, app.get and friends, or hooked into require so they loaded before Express. That breaks easily. Load order matters, ESM needs special loader hooks, and bundled apps may never “import” Express at all.
On 17 November 2026 the Express team announced a better way. The router now publishes a Node.js TracingChannel called express.router.request for every middleware, route handler and router.param() callback. Any code can subscribe to it, without patching and without even having a reference to your app. This tutorial shows how to use it for route-level logging, latency and error tracking.
Source: the official Express blog post Express is now observable by design.
What you need
- Express 5 (the channel ships in
routerv2.3.0, the package that powers Express 5 routing) - Node.js 18.19 or later. Older versions simply publish nothing.
No Express upgrade is needed. Just make sure your lockfile resolves router 2.3.0 or newer:
npm update router
npm ls router
Tracing channels in 60 seconds
TracingChannel lives in node:diagnostics_channel. It is a bundle of five channels that describe one operation’s lifecycle:
start– the operation is about to runend– it returned synchronouslyasyncStart– the callback ran or the promise settledasyncEnd– the callback or promise continuation finishederror– it threw, rejected or passed an error to its callback
Every event for one operation receives the same context object, so you can store a start time in start and read it in asyncEnd. Publishing with no subscribers is close to free, so libraries can leave the channels on.
Step 1: Log every handler
Put this in a file that runs before your server starts, for example observability.js:
// observability.js
const dc = require('node:diagnostics_channel');
dc.tracingChannel('express.router.request').subscribe({
start({ req, route, handler }) {
console.log(`${req.method} ${req.baseUrl}${route} -> ${handler.name || '<anonymous>'}`);
},
error({ handler, error }) {
console.error(`${handler.name || '<anonymous>'} failed:`, error);
},
});
// app.js
require('./observability');
const { randomUUID } = require('node:crypto');
const express = require('express');
const app = express();
app.use(function requestId(req, res, next) {
req.id = randomUUID();
next();
});
app.get('/users/:id', async function loadUser(req, res) {
res.json({ id: req.params.id });
});
app.listen(3000);
Because the subscription is global, it does not matter whether observability.js loads before or after Express, or whether Express ends up bundled.
Tip: give your middleware and handlers names (function loadUser) so logs are readable.
The context object
Each event receives a context with these fields, according to the Express post:
| Field | Meaning |
|---|---|
req, res |
Request and response objects |
route |
The matched pattern, like /users/:id, not /users/42 |
handler |
The middleware, route handler or param callback being called |
param |
Parameter name, only for router.param() callbacks |
error |
The error the handler reported via next(err), throw or rejection |
incomingError |
The error an error-handling middleware received |
errorHandler |
true when the handler is error-handling middleware |
result |
What the handler returned, or what its promise resolved to |
The route field is the big win. You get the route pattern, which is exactly what you want for metrics, because /users/1 and /users/2 should land in the same bucket.
Step 2: Measure latency per route
This example, adapted from the Express post, records the last route that handled each request and logs its total time when the response finishes:
const dc = require('node:diagnostics_channel');
const routes = new WeakMap();
dc.tracingChannel('express.router.request').start.subscribe(
({ req, res, route, errorHandler }) => {
if (errorHandler) return;
if (!routes.has(req)) {
const startedAt = performance.now();
res.once('finish', () => {
const ms = (performance.now() - startedAt).toFixed(1);
console.log(`${req.method} ${routes.get(req)} ${res.statusCode} ${ms}ms`);
});
}
routes.set(req, req.baseUrl + route); // the last one to start handled the request
},
);
Output looks like:
GET /users/:id 200 3.4ms
Swap console.log for your metrics client (Prometheus, StatsD, OpenTelemetry metrics) to get latency histograms per route with no framework-specific plugin.
Step 3: Know exactly which middleware failed
Errors are reported where they start, so you can blame the right function:
dc.tracingChannel('express.router.request').subscribe({
error({ req, route, handler, error }) {
console.error(
`${req.method} ${req.baseUrl}${route} failed in ${handler.name || '<anonymous>'}:`,
error.message,
);
},
});
// GET /orders/:id failed in loadOrder: connection refused
This works for errors passed to next(err), thrown synchronously, or from rejected promises in async handlers.
Step 4: Spans with OpenTelemetry
If you already use OpenTelemetry’s HTTP instrumentation, it creates a span for the request. You can add one child span per handler. The Express post shows the full pattern; the core idea is:
const dc = require('node:diagnostics_channel');
const { context, trace, SpanStatusCode } = require('@opentelemetry/api');
const tracer = trace.getTracer('express');
const parents = new WeakMap();
dc.tracingChannel('express.router.request').subscribe({
start(ctx) {
if (!parents.has(ctx.req)) parents.set(ctx.req, context.active());
const parent = parents.get(ctx.req);
ctx.span = tracer.startSpan(ctx.handler.name || '<anonymous>', {}, parent);
ctx.token = context.attach(trace.setSpan(parent, ctx.span));
},
end(ctx) {
ctx.token.dispose();
if ('result' in ctx || 'error' in ctx) ctx.span.end();
},
asyncEnd(ctx) {
ctx.span.end();
},
error(ctx) {
ctx.span.recordException(ctx.error);
ctx.span.setStatus({ code: SpanStatusCode.ERROR });
},
});
Storing the parent context per request keeps each handler span attached to the request instead of nesting middleware spans inside each other. Check the context.attach call against the OpenTelemetry API version you use, since the exact context-management API can differ between versions.
Publishing your own channel
The same pattern works for your own libraries. Wrapping an operation takes one line:
const dc = require('node:diagnostics_channel');
const queryChannel = dc.tracingChannel('my-db:query');
function query(sql) {
return queryChannel.tracePromise(() => runQuery(sql), { sql });
}
Subscribers anywhere in the process can now time every query. The Express team notes that mysql2, node-redis, ioredis, graphql, mongoose and Nitro’s ecosystem already publish their own tracing channels.
Things to keep in mind
- Subscribers run in the request path. Keep them fast and never throw from them.
- Use
WeakMaps keyed byreqrather than global maps, so memory is released with the request. - On Node older than 18.19 nothing is published, so do not rely on these channels for security logic.
Takeaway
Express 5 apps can now be observed without monkey patching. Update router to 2.3.0, subscribe to express.router.request, and you get route patterns, handler names, timings and precise error locations in a few lines of plain Node.js. It works the same whether your app is CommonJS, ESM or bundled.
