OpenTelemetry and the collector

Intermediate · 10 min

OpenTelemetry gives traces, metrics and logs one standard format, OTLP, and the Collector receives, processes and routes them to any backend you choose.

Before thisMetrics with PrometheusLog aggregation pipelines

Adam Gardner · 10 min

OpenTelemetry and the collector

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Adam GardnerReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • OpenTelemetry is a vendor-neutral standard: APIs and SDKs to produce telemetry, a wire protocol called OTLP, and shared names for common attributes. It is not a storage backend.
  • Instrument once with OpenTelemetry and you can change where the data goes without touching application code, by changing the Collector's configuration.
  • The Collector is built from receivers, processors and exporters, wired together into one pipeline per signal type: traces, metrics and logs.
  • Put memory_limiter first in every pipeline so the Collector sheds load instead of crashing, and batch near the end so exporters send fewer, larger requests.
  • Run the Collector as an agent on each node, as a central gateway, or both. The agent adds host context and the gateway centralizes credentials, sampling and routing.

The problem: three agents and a lock-in

By now a service might have a Prometheus endpoint for metrics, Fluent Bit for logs, and a vendor’s library for traces. Each has its own agent, configuration and data format. Then the company changes monitoring vendor, and every service has to be re-instrumented, because the tracing library was the old vendor’s.

OpenTelemetry, or OTel, is the CNCF project that standardizes the producing side of telemetry so that the storing side can be swapped freely. It defines:

  • APIs and SDKs in each major language, plus auto-instrumentation for common frameworks, to create traces, metrics and logs.
  • OTLP, the OpenTelemetry Protocol, one wire format for all three signals over gRPC or HTTP.
  • Semantic conventions: agreed attribute names, such as service.name or http.response.status_code, so data from different languages lines up.
  • The Collector, a standalone program that receives, processes and exports telemetry.

OTel does not store anything. You still need a backend: Prometheus or Mimir for metrics, Loki or Elasticsearch for logs, Jaeger or Tempo for traces, or a commercial service. What traces are and why they matter is covered in system design’s distributed tracing lesson.

How the Collector is built

The Collector is a router with a plug-in architecture. Think of a sorting depot with loading docks.

  • Receivers take data in. The otlp receiver accepts OTLP on port 4317 (gRPC) and 4318 (HTTP). Others scrape Prometheus endpoints, tail files or accept other vendors’ formats.
  • Processors change data in flight: limit memory, drop or sample, add or remove attributes, batch.
  • Exporters send data out: OTLP to another Collector or backend, Prometheus remote-write, a vendor’s API.
  • Pipelines, under service, wire them together, one per signal type.

The order of processors matters. The Collector’s own documentation lists memory_limiter first, so it can shed load before anything else buffers data. Filtering and sampling come next, then enrichment, then batch last. The same page now says to prefer an exporter’s built-in batching where it has one.

The Collector ships in distributions. The core build has a minimal set, and contrib has hundreds of components, including the prometheus receiver used below. The current release when I checked was v0.166.0. Version 0.x means the configuration still changes between releases, so pin your version and read the changelog.

A worked example: what OTLP looks like on the wire

I don’t have a Collector here, so I wrote a 17-line Python stand-in for its OTLP/HTTP receiver. It listens on 4318, accepts JSON at /v1/traces and prints each span it gets. Then I posted a hand-written OTLP payload with curl. The payload holds one trace with two spans: a POST /checkout server span and a child charge card client span, both marked as errors:

{"resourceSpans":[{"resource":{"attributes":[
   {"key":"service.name","value":{"stringValue":"shop-web"}}, ...]},
 "scopeSpans":[{"scope":{"name":"manual"},"spans":[
   {"traceId":"5b8efff798038103d269b633813fc60c","spanId":"eee19b7ec3c1b174",
    "name":"POST /checkout","kind":2, ...},
   {"traceId":"5b8efff798038103d269b633813fc60c","spanId":"eee19b7ec3c1b175",
    "parentSpanId":"eee19b7ec3c1b174","name":"charge card","kind":3, ...}]}]}]}
$ curl -s -X POST localhost:4318/v1/traces -H 'Content-Type: application/json' --data @span.json -w ' HTTP %{http_code}\n'
{} HTTP 200
$ cat received.txt
/v1/traces service=shop-web trace=5b8efff7.. span="POST /checkout" 3012ms parent=-
/v1/traces service=shop-web trace=5b8efff7.. span="charge card" 3000ms parent=eee19b7ec3c1b174

Three things in the format are worth seeing once. Resource attributes, like service.name, describe who sent the data and are stated once per batch, not per span. Spans in one request share a traceId and link by parentSpanId, which is how a backend rebuilds the tree: 3,000 of checkout’s 3,012 ms were the card charge. And it is plain JSON over HTTP, so any language, or curl, can send it. This is my stand-in, not the real Collector, which also validates the payload and supports protobuf, the more common encoding.

A Collector configuration

Here is a configuration that takes OTLP from apps and also scrapes the Prometheus endpoint from the metrics lesson, adds an environment attribute and routes each signal to its own backend. I wrote it from the Collector docs. I did not run it, because the Collector is not installed here. I did load the YAML and check, with a short script, that every component named in a pipeline is defined:

receivers:
  otlp:
    protocols:
      grpc: { endpoint: 0.0.0.0:4317 }
      http: { endpoint: 0.0.0.0:4318 }
  prometheus:
    config:
      scrape_configs:
        - job_name: shop-web
          static_configs: [{ targets: ["localhost:9464"] }]
processors:
  memory_limiter: { check_interval: 1s, limit_percentage: 80, spike_limit_percentage: 20 }
  attributes/env:
    actions: [{ key: deployment.environment.name, value: prod, action: upsert }]
  batch: {}
exporters:
  otlphttp/traces: { endpoint: https://tempo.internal:4318 }
  otlphttp/logs: { endpoint: https://loki.internal:3100/otlp }
  prometheusremotewrite: { endpoint: https://prometheus.internal/api/v1/write }
service:
  pipelines:
    traces:  { receivers: [otlp], processors: [memory_limiter, attributes/env, batch], exporters: [otlphttp/traces] }
    metrics: { receivers: [otlp, prometheus], processors: [memory_limiter, attributes/env, batch], exporters: [prometheusremotewrite] }
    logs:    { receivers: [otlp], processors: [memory_limiter, attributes/env, batch], exporters: [otlphttp/logs] }
$ python3 check_pipelines.py collector.yaml
traces   otlp               -> memory_limiter -> attributes/env -> batch -> otlphttp/traces
metrics  otlp + prometheus  -> memory_limiter -> attributes/env -> batch -> prometheusremotewrite
logs     otlp               -> memory_limiter -> attributes/env -> batch -> otlphttp/logs

The prometheus receiver and prometheusremotewrite exporter come from the contrib distribution. The real check is otelcol-contrib validate --config collector.yaml, which I could not run here.

Agent, gateway or both

A Collector can run on every node as an agent, adding host and pod attributes and buffering locally. It can run as a central gateway that holds backend credentials and applies sampling in one place. Many setups use both: apps send to the local agent, and the agent forwards to the gateway.

About the video

“OpenTelemetry Collector: EVERYTHING you need to know [to get started]” is from Adam Gardner’s channel, published August 2024, 10 minutes 6 seconds. Its chapters cover what the Collector is, the ways to run it (binary, container, Kubernetes), receivers, processors, exporters, pipelines, monitoring the Collector itself, connectors and extensions. That matches the Collector half of this lesson closely. It is a small individual channel, so the CNCF talk by Steve Flanders is linked for depth. The official OpenTelemetry channel’s beginner video covers the “why” half. English is judged from the title, description and channel.

Where this fits

The data is flowing and stored. The next question is when it should wake a human up: alerting on symptoms, not causes.

Check yourself

  1. 01What problem does OpenTelemetry solve that a vendor's own agent does not?

    Lock-in. With a vendor agent, switching backends means re-instrumenting every service. With OpenTelemetry the instrumentation stays and only the exporter configuration changes.

  2. 02What are the three kinds of Collector component that make up a pipeline?

    Receivers, which take data in, processors, which modify, filter or batch it, and exporters, which send it on.

  3. 03Why should memory_limiter come first in the processor list?

    So it can refuse data before later processors have buffered it. Placed later, the data is already in memory and the Collector can still run out.

  4. 04On which ports does an OTLP receiver listen by default for gRPC and HTTP?

    4317 for gRPC and 4318 for HTTP.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark