Skip to main content
nestrs builds its observability story around three composable layers: a global tracing subscriber for structured logging, a request-tracing middleware that emits per-request spans and log lines, and an optional Prometheus scrape endpoint. A fourth layer — OpenTelemetry span export — is behind the otel feature flag and can be added without changing the rest of the pipeline.

Installing the tracing subscriber

Call configure_tracing once, before listen, so all log output and request spans share the same pipeline. The log level is read from NESTRS_LOG first, then RUST_LOG, then the default_directive on TracingConfig (default "info"):
TracingFormat::Pretty is the default and produces human-readable multi-line output. Switch to TracingFormat::Json in production for log aggregation platforms.

Request tracing middleware

use_request_tracing adds a middleware that:
  • Records a completion log line with method, path, status, duration_ms, and request_id (when use_request_id() is also enabled).
  • Creates a tracing span named http.server.request for each request, with fields http.request.method and http.route.
Skip high-volume infrastructure paths (metrics, health) to avoid flooding request logs:
http.route is set to the concrete request path at this middleware layer. Axum’s route template (e.g. /users/:id) is not available here. For OTLP dashboards, treat the literal path as the closest stable route identifier unless you add a custom layer that sets a template field.

Prometheus metrics

enable_metrics registers a Prometheus scrape handler at the path you provide (default /metrics). It tracks:
  • http_request_duration_seconds — histogram with standard buckets
  • http_requests_total{method, status} — counter
  • http_requests_in_flight — in-flight gauge
The /metrics path is mounted at the server root — it is not affected by set_global_prefix or enable_uri_versioning.

Health and readiness checks

Use enable_health_check for a simple liveness probe that always returns 200:
Use enable_readiness_check when you want to gate traffic on the health of dependencies. Implement HealthIndicator for each dependency and pass the indicators at startup:
When any indicator returns HealthStatus::Down, the readiness endpoint returns 503 with a Terminus-style JSON summary containing status, info, error, and details keys.

OpenTelemetry (OTLP)

Enable the otel feature to export spans to any OTLP-compatible collector (Jaeger, Tempo, Honeycomb, etc.):
Replace configure_tracing with configure_tracing_opentelemetry and supply an OpenTelemetryConfig:
When endpoint is not set, nestrs falls back to the OTEL_EXPORTER_OTLP_ENDPOINT environment variable, then to http://localhost:4317.
Use try_init_tracing or try_init_tracing_opentelemetry directly if you need to install the tracing subscriber outside of the NestApplication builder chain (for example in test harnesses or CLI tools).

Environment variables reference

Troubleshooting

Local dev vs production