Skip to content

Node.js

Instrument your Node.js applications to send traces and logs to Kopai.

Terminal window
npm install @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node

Create an instrumentation.mjs file:

import { NodeSDK } from "@opentelemetry/sdk-node";
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
const sdk = new NodeSDK({
instrumentations: [getNodeAutoInstrumentations()],
});
sdk.start();

The SDK automatically reads configuration from standard OpenTelemetry environment variables.

Set the required environment variables and run:

Terminal window
export OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp.kopai.app"
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer YOUR_TOKEN"
export OTEL_SERVICE_NAME="my-service"
node --import ./instrumentation.mjs app.js
VariableDescription
OTEL_EXPORTER_OTLP_ENDPOINTKopai OTLP endpoint
OTEL_EXPORTER_OTLP_HEADERSAuth header with your backend token
OTEL_SERVICE_NAMEName shown in Kopai UI

The @opentelemetry/auto-instrumentations-node package provides automatic instrumentation for:

  • HTTP/HTTPS
  • Express, Fastify, Koa
  • MongoDB, PostgreSQL, MySQL
  • Redis, Memcached
  • gRPC
  • And more

For a complete working example:

Node.js Example