Observability
Metrics
Prometheus-compatible counters, gauges and histograms, plus observabilityMiddleware for automatic HTTP request metrics and tracing.
On this page2
Usage
TypeScript
TS
1import { MetricsRegistry, metricsHandler, observabilityMiddleware } from '@axiljs/observability'
2
3const metrics = new MetricsRegistry()
4
5const orders = metrics.counter('orders_created_total', 'Orders created', ['channel'])
6const gauge = metrics.gauge('active_connections', 'Active connections')
7const histogram = metrics.histogram('checkout_duration_ms', 'Checkout duration', [50, 100, 250, 500])
8
9orders.inc({ channel: 'web' })
10gauge.set(12)
11histogram.observe(87)
12
13app.use(observabilityMiddleware({ metrics }))
14app.get('/metrics', metricsHandler(metrics))Registration is idempotent per name: registering a name again with the same type returns the existing metric; a different type throws.
observabilityMiddleware
With a metrics registry, the middleware records http_requests_total (labels method, path, status), http_request_duration_ms (labels method, path) and http_active_requests.
| Option | Default | Description |
|---|---|---|
metrics | — | MetricsRegistry to record HTTP metrics into |
tracer | — | Tracer for a per-request server span (see Tracing) |
pathLabel | req.path | (req) => string for the path label; return a route template such as /users/:id to avoid high-cardinality labels |
propagate | true | Continue an incoming W3C traceparent / B3 trace |
TypeScript
TS
1app.use(observabilityMiddleware({
2 metrics,
3 tracer,
4 pathLabel: (req) => req.path.replace(/\/\d+(?=\/|$)/g, '/:id')
5}))Was this page helpful?