# Node.js Instrument Node.js applications with OpenTelemetry # Integration Instrument your Node.js applications to send traces and logs to Kopai. ## Installation **npm:** ```bash npm install @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node ``` **pnpm:** ```bash pnpm add @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node ``` **yarn:** ```bash yarn add @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node ``` ## Basic Setup Create an `instrumentation.mjs` file: ```javascript 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](https://opentelemetry.io/docs/languages/sdk-configuration/). ## Running Your Application Set the required environment variables and run: ```bash export OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp-http.kopai.app" export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer YOUR_TOKEN" export OTEL_SERVICE_NAME="my-service" node --import ./instrumentation.mjs app.js ``` | Variable | Description | |----------|-------------| | `OTEL_EXPORTER_OTLP_ENDPOINT` | Kopai OTLP endpoint | | `OTEL_EXPORTER_OTLP_HEADERS` | Auth header with your backend token | | `OTEL_SERVICE_NAME` | Name shown in Kopai UI | ## Auto-Instrumentation The `@opentelemetry/auto-instrumentations-node` package provides automatic instrumentation for: - HTTP/HTTPS - Express, Fastify, Koa - MongoDB, PostgreSQL, MySQL - Redis, Memcached - gRPC - And more ## Working Example For a complete working example: **[Node.js Example](https://github.com/kopai-app/kopai-integration-examples/tree/main/node-js)**