OpenTelemetry Express Instrumentation (Enhanced)

0.41.0 · active · verified Sun Apr 19

opentelemetry-instrumentation-express is an enhanced instrumentation library for the `express` web framework, designed to integrate seamlessly with OpenTelemetry for Node.js applications. Currently at version 0.41.0, it is actively maintained and part of the `aspecto-io/opentelemetry-ext-js` project, which sees frequent updates. This package differentiates itself from the standard `@opentelemetry/instrumentation-express` by providing more granular and accurate route information through custom span attributes like `express.route.full`, `express.route.configured`, and `express.route.params`, which capture the full path and parsed parameters. It also tracks `express.unhandled` requests, offering better insights into unhandled routes and 404s. The instrumentation supports Express versions `^4.9.0` and is suitable for detailed tracing of Express applications where precise route context is crucial for observability.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to set up and register the ExpressInstrumentation with a NodeTracerProvider to automatically trace Express routes, including custom options.

const express = require('express');
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { ExpressInstrumentation } = require('opentelemetry-instrumentation-express');

const tracerProvider = new NodeTracerProvider();
tracerProvider.register();

registerInstrumentations({
  tracerProvider,
  instrumentations: [
    new ExpressInstrumentation({ 
      // Optional: include HTTP attributes on spans
      includeHttpAttributes: true,
      // Optional: custom hook for adding attributes before request handling
      requestHook: (span, { moduleVersion, req, res }) => {
        span.setAttribute('my.custom.attribute', 'value');
      }
    })
  ]
});

const app = express();

app.get('/', (req, res) => {
  res.send('Hello OpenTelemetry Express!');
});

app.get('/users/:id', (req, res) => {
  res.send(`User ID: ${req.params.id}`);
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

view raw JSON →