Troubleshooting guide
OpenTelemetry for Express.js: setup guide
Express works well with OpenTelemetry's Node.js auto-instrumentation. The main catch is that it must load before your application code.
3 min read10 sectionsUpdated
On this page
npm install @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node \
@opentelemetry/exporter-trace-otlp-http
// instrumentation.js
const { NodeSDK } = require('@opentelemetry/sdk-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
const sdk = new NodeSDK({ traceExporter: new OTLPTraceExporter(), instrumentations: [getNodeAutoInstrumentations()] });
sdk.start();
process.on('SIGTERM', () => sdk.shutdown().finally(() => process.exit(0)));
# Run it first
OTEL_SERVICE_NAME=api OTEL_EXPORTER_OTLP_ENDPOINT=http://collector:4318 node --require ./instrumentation.js server.jsAdd business context
Enrich the active span with values that help you to debug: use trace.getActiveSpan()?.setAttribute('order.tier', tier). Keep values low in cardinality where you group by them. In ESM, start with node --import ./instrumentation.mjs and the module hook. Turn off noisy instrumentations such as fs through the options of getNodeAutoInstrumentations. Place the --require flag in NODE_OPTIONS if a process manager starts your server, so you do not need to edit its start command. Keep the instrumentation file free of application imports.
Why spans get named GET /
If every span is called only by its method, Express's router instrumentation isn't active, or your route handlers are attached in a way that it cannot see. A proper span name looks like GET /users/:id, and uses the route template, not the actual URL. This is important for two reasons: charts group correctly, and cardinality stays bounded. Never put a user ID or a raw URL into a span name, since each distinct name creates a separate series and can multiply the cost of your telemetry.
Verify it works
Send a request and view the trace in APM. You should see the HTTP span, the Express route and any database calls nested below. Add trace IDs to your logs to jump between the two.
Related reading
- OpenTelemetry for Python: setup guide: Add OpenTelemetry to a Python service: install the distro, auto-instrument with opentelemetry-instrument, export to VitalRix and add custom spans.
- What is a span in tracing?: What a span is in distributed tracing: its fields, parent and child spans, span kinds, attributes and status, with an annotated example you can read.
- OpenTelemetry for FastAPI: Trace a FastAPI service with OpenTelemetry: instrument the app, async database drivers and httpx, run uvicorn workers correctly and export.
- OpenTelemetry for Go: Instrument a Go service with OpenTelemetry: set up the tracer provider, wrap net/http with otelhttp, pass context everywhere and export the spans.
- OpenTelemetry for PHP and Laravel: Trace a Laravel app with OpenTelemetry: install the PHP extension and auto-instrumentation packages, configure PHP-FPM and trace queries.
- OpenTelemetry for Flask: Instrument a Flask app with OpenTelemetry, in code or with zero code: trace routes, SQLAlchemy and outgoing calls, and export to VitalRix.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.