OpenTelemetry in practice: instrument a Node.js service, run a production Collector, sample traces and link metrics, logs and traces in Grafana.
At 02:00 a latency alert fires. The metrics dashboard shows p99 climbing, the logs show errors with no request ID, and the traces live in a different tool with a different service name. You spend 40 minutes lining up timestamps by hand.
OpenTelemetry (OTel) exists to remove that gap. It gives every signal the same vocabulary and the same trace context, so one request can be followed across services, metrics and logs.
OpenTelemetry is a vendor-neutral, open standard (a CNCF project) for generating and exporting telemetry: traces, metrics and logs. It defines APIs, SDKs, a wire protocol called OTLP and a Collector, so you instrument your code once and send the data to any compatible backend.
It does not include a database or UI. Think of it as the plumbing between your applications and whichever backend you choose.
Each signal answers a different question, and a shared trace context links them. Metrics show that something is wrong in aggregate, traces show where in the request path, and logs show what happened at that point.
Context travels between services in the W3C traceparent header, which looks like 00-<trace-id>-<span-id>-01. Any service that forwards the header keeps the trace connected.
The application talks only to the Collector. Backends can change without touching application code.
Use zero-code auto-instrumentation first, configured with environment variables. It patches common libraries such as HTTP servers, Express and database clients, with no change to business logic.
npm install @opentelemetry/api \ @opentelemetry/auto-instrumentations-node export OTEL_SERVICE_NAME=payment-serviceexport OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4317export OTEL_EXPORTER_OTLP_PROTOCOL=grpcexport OTEL_RESOURCE_ATTRIBUTES=\deployment.environment.name=production,team=payments node --require \ @opentelemetry/auto-instrumentations-node/register \ server.jsThis produces traces for incoming requests and outgoing calls. The resource attributes tag every span, and they become the labels you filter by months later, so agree on names early. Package names and semantic convention keys change over time (for example deployment.environment became deployment.environment.name), so check the current docs when you copy this.
If you prefer an SDK file, keep it small and read configuration from the same environment variables:
// tracing.js - load before the app with --requireconst { NodeSDK } = require('@opentelemetry/sdk-node');const { getNodeAutoInstrumentations,} = require('@opentelemetry/auto-instrumentations-node');const { OTLPTraceExporter,} = require('@opentelemetry/exporter-trace-otlp-grpc'); const sdk = new NodeSDK({ traceExporter: new OTLPTraceExporter(), instrumentations: [getNodeAutoInstrumentations()],});sdk.start();Add manual spans for business steps that auto-instrumentation cannot see, such as a fraud decision or a retry loop. Logs support in the Node SDK is less mature than traces, so check its current status and use a log library instrumentation that injects trace_id into each log line.
Run the Collector as its own deployment, with at least two replicas, and let it handle batching, retries, sampling and routing. Pin a specific image version and keep the configuration in a ConfigMap.
apiVersion: apps/v1kind: Deploymentmetadata: name: otel-collector namespace: monitoringspec: replicas: 2 selector: matchLabels: {app: otel-collector} template: metadata: labels: {app: otel-collector} spec: containers: - name: otel-collector image: otel/opentelemetry-collector-contrib:0.115.0 args: ["--config=/conf/config.yaml"] volumeMounts: - name: config mountPath: /conf volumes: - name: config configMap: name: otel-collector-configReplace the version with a current release. The configuration below routes each signal to the Grafana stack. Loki now accepts OTLP natively (version 3.0 and later), so the old loki exporter is no longer needed.
receivers: otlp: protocols: grpc: {endpoint: "0.0.0.0:4317"} http: {endpoint: "0.0.0.0:4318"} processors: memory_limiter: check_interval: 1s limit_mib: 512 spike_limit_mib: 128 batch: timeout: 1s send_batch_size: 1024 exporters: otlp/tempo: endpoint: tempo:4317 tls: {insecure: true} prometheusremotewrite: endpoint: http://prometheus:9090/api/v1/write send_exemplars: true otlphttp/loki: endpoint: http://loki:3100/otlp service: pipelines: traces: receivers: [otlp] processors: [memory_limiter, batch] exporters: [otlp/tempo] metrics: receivers: [otlp] processors: [memory_limiter, batch] exporters: [prometheusremotewrite] logs: receivers: [otlp] processors: [memory_limiter, batch] exporters: [otlphttp/loki]Put memory_limiter first in each pipeline so overload slows senders instead of killing the Collector. For Prometheus remote write, start Prometheus with --web.enable-remote-write-receiver and --enable-feature=exemplar-storage.
Enable exemplars, then tell Grafana which trace datasource to link to. The metric point carries a trace ID, and Grafana turns it into a link to Tempo.
apiVersion: 1datasources: - name: Prometheus type: prometheus url: http://prometheus:9090 jsonData: exemplarTraceIdDestinations: - name: trace_id datasourceUid: tempo-uidOn the Tempo side, configure "trace to logs" so a trace opens the matching log lines filtered by trace ID. That gives you the full path: dashboard spike, exemplar, trace, then logs, in a few clicks.
Use tail sampling: keep every error and slow trace, and a small share of the rest. Head sampling in the SDK (for example OTEL_TRACES_SAMPLER=parentbased_traceidratio with OTEL_TRACES_SAMPLER_ARG=0.1) is cheap but random, so it discards failures at the same rate as normal requests.
processors: tail_sampling: decision_wait: 10s policies: - name: errors type: status_code status_code: {status_codes: [ERROR]} - name: slow type: latency latency: {threshold_ms: 1000} - name: baseline type: probabilistic probabilistic: {sampling_percentage: 10}Tail sampling needs every span of a trace to reach the same Collector instance. Use a first tier with the load-balancing exporter (keyed by trace ID) in front of a second sampling tier. Add tail_sampling to the traces pipeline after memory_limiter.
Yes. The OpenTelemetry Operator can inject auto-instrumentation into pods through an Instrumentation resource and a pod annotation, so teams do not edit Dockerfiles. Annotate a workload with instrumentation.opentelemetry.io/inject-nodejs: "true" and the operator adds the agent and environment variables. This suits platform teams that want a consistent baseline across many services.
OpenTelemetry does not give you storage, query, dashboards or alerting, and it does not fix poor naming. Auto-instrumentation also costs CPU and memory, and high-cardinality attributes such as user IDs in metric labels can overload a metrics backend. Review attribute choices the same way you review database schemas.
| Option | Lock-in | Effort |
|---|---|---|
| OpenTelemetry | Low | Medium |
| Vendor agent | High | Low |
| Prometheus only | Low | Low |
A vendor agent can work in an afternoon and the bill may be acceptable. OTel costs more setup but keeps your instrumentation portable. The two combine well: instrument with OTel and export to the vendor.
service.name, environment, team, cluster) in a shared template.trace_id in every structured log line.References and Further Reading
- OpenTelemetry Documentation - SDKs, Collector and semantic conventions
- Collector Configuration - Receivers, processors and exporters
- Grafana Tempo - Trace storage and trace-to-logs linking
- OpenTelemetry Operator - Kubernetes auto-instrumentation
No. OpenTelemetry standardises how telemetry is generated, propagated and exported (the API, SDKs, OTLP and the Collector). It does not store, query, alert on or visualise data, so you still need one or more backends.
Yes. Instrument once with OpenTelemetry and export OTLP to a vendor through the Collector or the vendor's OTLP endpoint. You keep the commercial backend while avoiding lock-in at the instrumentation layer, and switching later means changing exporter config, not code.
An exemplar is a metric data point that carries a trace ID. In Grafana it becomes a clickable link from a latency spike on a graph to a specific slow trace in Tempo. Prometheus must have exemplar storage enabled for this to work.
Probabilistic head sampling decides before a request finishes, so it drops errors and slow requests at the same rate as healthy ones. Use tail sampling in a Collector tier to keep all error and high-latency traces, and sample the rest.
Put the `memory_limiter` processor first in every pipeline, set `limit_mib` and `spike_limit_mib` below the container limit, and run at least two replicas behind a load balancer. Back-pressure then reaches senders instead of the Collector being killed.
Discussion0