OpenTelemetry for Node.js

OpenTelemetry provides robust support for Node.js applications. With automatic instrumentation for common frameworks and a wide ecosystem of plugins, you can quickly capture spans, logs, and metrics from your backend services.

Installation

To get started, install the OpenTelemetry packages for Node.js:

npm install @opentelemetry/api @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node

For full installation instructions:

👉 OpenTelemetry Node.js Getting Started

Auto-Instrumentation

Auto-instrumentation lets you automatically trace common libraries such as HTTP clients, database drivers, and more without needing to modify your code. OpenTelemetry provides auto-instrumentation libraries for many popular languages, making it fast to get started with tracing.

For Node.js, you can use the @opentelemetry/auto-instrumentations-node package to automatically capture telemetry from supported libraries like Express, MySQL, and gRPC.

Basic setup looks like:

const { NodeSDK } = require('@opentelemetry/sdk-node'); 
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const sdk = new NodeSDK({ instrumentations: [getNodeAutoInstrumentations()] });

sdk.start();

Learn more:

👉 Node.js Auto-Instrumentation Setup

Manual Instrumentation

Manual instrumentation gives you full control over your traces, allowing you to create spans wherever needed. You can customize span names, attributes, and relationships to capture the most important parts of your application’s flow.

You can manually create and manage spans in your Node.js code using the OpenTelemetry API:

const { trace } = require('@opentelemetry/api');
const tracer = trace.getTracer('my-service');
const span = tracer.startSpan('my-operation'); // your applicationlogic
span.end();

Learn more:

👉 Node.js Manual Instrumentation Guide

Exporters

Once your application is instrumented, you need to export telemetry data. PlayerZero supports the OpenTelemetry Protocol (OTLP) over HTTP. You can configure your OpenTelemetry SDK to export traces, logs, and metrics to PlayerZero’s endpoint by setting the endpoint URL and API token.

In Node.js, you can configure an OTLP/HTTP exporter using environment variables or directly in your code.

Key environment variables:

OTEL_EXPORTER_OTLP_ENDPOINT=https://sdk.playerzero.app/otlp
OTEL_EXPORTER_OTLP_HEADERS=Authorization=Bearer <your-playerzero-token> OTEL_TRACES_EXPORTER=otlp
OTEL_LOGS_EXPORTER=otlp OTEL_METRICS_EXPORTER=otlp

Official reference:

👉 Node.js SDK Configuration

Optional: Using a Collector

Using an OpenTelemetry Collector is optional for most setups. A collector can help route telemetry to multiple destinations, perform transformations, or batch data efficiently. You might use a collector if you want to forward telemetry to both PlayerZero and another observability platform simultaneously.

You can optionally forward your telemetry through an OpenTelemetry Collector before reaching PlayerZero, if you need additional control or processing.

For detailed language-specific instrumentation examples and full OpenTelemetry documentation, refer to the links below.