Skip to content

OpenTelemetry Distributed Tracing & Instrumentation

Core Standards

  1. W3C Trace Context Propagation:
  2. Ensure the traceparent and tracestate headers are passed along all outbound HTTP/gRPC calls and message queues.
  3. Semantic Conventions:
  4. Standardize span names: use {http.method} {http.route} (e.g. POST /v1/orders) rather than dynamic URLs (POST /v1/orders/123).
  5. Tag spans with standard attributes: service.name, service.version, deployment.environment.
  6. No Sensitive PII in Span Attributes:
  7. Never log authorization headers, credit cards, user passwords, or raw request bodies into spans.

Trace Initialization Boilerplate (TypeScript / Node.js)

import { NodeSDK } from '@opentelemetry/sdk-node';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';

const sdk = new NodeSDK({
  resource: new Resource({
    [SemanticResourceAttributes.SERVICE_NAME]: process.env.OTEL_SERVICE_NAME || 'cloud-service',
    [SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]: process.env.NODE_ENV || 'production',
  }),
  traceExporter: new OTLPTraceExporter({
    url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT || 'http://otel-collector:4318/v1/traces',
  }),
  instrumentations: [getNodeAutoInstrumentations()],
});

sdk.start();