Observability for Node.js: Logs, Metrics and Traces With OpenTelemetry

If you can't see it, you can't fix it

console.log doesn't scale to production. Modern Node.js services need three signals — logs, metrics, and traces — connected by shared IDs. OpenTelemetry is the standard way to produce them.

Structured Logging With Pino

import pino from 'pino';
const logger = pino({ level: process.env.LOG_LEVEL || 'info' });

logger.info({ orderId, userId, durationMs }, 'order placed');

JSON logs are searchable by field. Never log passwords, tokens, or full card numbers.

Auto-Instrumentation

npm install @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node \
  @opentelemetry/exporter-trace-otlp-proto
// instrumentation.js — load before your app
import { NodeSDK } from '@opentelemetry/sdk-node';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';

new NodeSDK({
  serviceName: 'orders-api',
  traceExporter: new OTLPTraceExporter(), // configure endpoint via OTEL_EXPORTER_OTLP_* env vars
  instrumentations: [getNodeAutoInstrumentations()],
}).start();
node --import ./instrumentation.js server.js

HTTP, Express, Fastify, database clients, and Redis are traced automatically.

Custom Spans

import { trace } from '@opentelemetry/api';
const tracer = trace.getTracer('orders');

await tracer.startActiveSpan('calculate-discount', async (span) => {
  span.setAttribute('customer.tier', tier);
  try {
    return await calculateDiscount(cart);
  } finally {
    span.end();
  }
});

Custom Metrics

import { metrics } from '@opentelemetry/api';
const meter = metrics.getMeter('orders');
const ordersCounter = meter.createCounter('orders_placed');
ordersCounter.add(1, { region: 'ap-south' });

Connecting Logs to Traces

Include the active trace ID in every log line (Pino and OTel integrations can do this automatically). In New Relic or any OTel backend, you then jump from a slow trace straight to its logs.

What to Alert On

  • Error rate and p95 latency per endpoint
  • Event loop lag
  • Memory growth (possible leaks)
  • Dependency failures (DB, external APIs)

Instrument with OpenTelemetry once, and you can send data to New Relic today and any other backend tomorrow.

Arivanandhan Chitheshwaran