Building an OpenTelemetry Collector pipeline you can afford to run
Instrumenting services is the easy part of observability. The hard part comes a few months later, when the telemetry bill grows faster than traffic and the one trace you need was sampled away. The collector pipeline between your services and your back ends is where both problems are solved.
Contents
Key facts
- OpenTelemetry defines vendor-neutral APIs, SDKs and the OTLP protocol for traces, metrics and logs; the Collector receives, processes and exports that data.
- A common topology uses agents close to the workload and a separate gateway tier of collectors for central processing.
- Tail-based sampling decides after a trace is complete, so it can keep every slow or failed request; it requires all spans of a trace to reach the same collector instance.
- Metric cost is driven mostly by cardinality — the number of unique label combinations — not by the number of requests.
Why the pipeline matters more than the SDK
Most teams start observability work inside the application: add an SDK, wrap HTTP handlers and database calls, export to a back end. That produces data quickly, and for a while it is enough. Problems appear when several teams instrument independently. One adds request IDs as metric labels, another logs full request bodies, a third exports every span from a chatty background job. The back end receives everything, bills for everything, and becomes slower to query as volume grows.
Changing every service to fix this is slow. Changing a shared pipeline is fast. The OpenTelemetry Collector sits between applications and back ends and can batch, filter, transform, sample and route data with configuration rather than code. It turns telemetry from something each service decides on its own into something the platform governs.
The pipeline is also the natural place for controls that must never be skipped: redaction of personal data, enrichment with deployment metadata and enforcement of budgets. If those controls live only in application code, the first service that forgets them leaks data or cost.
Agents and a gateway tier
A Collector can run in two roles. As an agent, it runs next to the workload — as a sidecar or a node-level daemon — receives telemetry over the local network, adds host and Kubernetes attributes and forwards it quickly. As a gateway, it runs as a separately scaled deployment that receives data from many agents and applies central processing.
Splitting the roles has practical benefits. Agents keep applications decoupled from back-end availability: if the gateway is restarting, the agent buffers. The gateway concentrates expensive processing such as tail sampling, so it can be sized and scaled independently. Configuration changes to sampling or redaction are rolled out to one tier instead of to every node.
| Concern | Agent tier | Gateway tier |
|---|---|---|
| Location | Same node or pod as the workload | Central deployment per cluster or region |
| Typical processors | Resource detection, batching, memory limiting | Tail sampling, redaction, routing, aggregation |
| Scaling | One per node | Horizontally, by throughput |
| Failure impact | Local; buffered | Shared; must be highly available |
Every collector should run the memory limiter processor first in its pipelines and the batch processor near the end. The memory limiter protects the collector from out-of-memory crashes under bursts; batching reduces the number of outbound requests and improves compression.
Where and how to sample
Traces are the largest signal, and keeping all of them is rarely justified. There are two families of sampling. Head-based sampling decides when a trace starts — for example, keep 10% — and propagates the decision to downstream services. It is cheap and predictable, but it decides before anyone knows whether the request will fail or be slow.
Tail-based sampling decides after the trace is complete. A gateway collector buffers spans for a short period, then applies policies such as: keep every trace with an error, keep every trace slower than the latency objective, keep a small percentage of the rest. The result is a much smaller volume that still contains almost every trace an engineer will want to look at during an incident.
Tail sampling has one structural requirement: all spans of a trace must reach the same collector instance, otherwise each instance sees only fragments. The standard approach is a two-layer gateway: a first layer uses a load-balancing exporter keyed by trace ID, and a second layer runs the tail-sampling processor. Size the decision wait to cover your longest normal trace and monitor how many traces arrive late.
Controlling metric cardinality
A metric with labels service, route and status_code might have a few hundred series. Add customer_id and it can have millions. Storage, memory and query cost in most metric back ends scale with the number of active series, so a single unbounded label can dominate the bill.
- Set a budget of active series per service and publish it alongside the service's SLOs.
- Inventory the current series by metric and label; usually a handful of attributes cause most of the volume.
- Drop or aggregate offending attributes in the gateway with the attributes, filter or transform processors.
- Move detail to traces, where a customer or order identifier is useful and sampling bounds the cost.
- Normalise routes so that
/orders/123and/orders/456are both recorded as/orders/204617887.
Logs need the same discipline: structured fields, defined severity levels, and sampling or rate limits for repetitive debug output. Retention can differ by signal — weeks for detailed traces, months for aggregated metrics — and should be decided on purpose rather than left at a vendor default.
Keeping personal data out
Telemetry often contains personal data by accident: email addresses in URLs, names in log messages, tokens in headers. Once it reaches a back end, it is copied into indexes and backups with retention that nobody reviewed for privacy. Under the GDPR that data still needs a legal basis, a retention limit and protection.
The pipeline should redact before export. The OpenTelemetry Collector's attributes and transform processors can delete or hash known attributes, and pattern-based rules can mask values such as email addresses or card-like numbers in strings. Treat the redaction configuration as code: review changes, and test it with sample payloads that contain the patterns you expect to remove.
Worked example: cutting trace volume without losing incidents
A platform with 40 services sends every span directly from the SDK to a tracing back end. Volume is around 2 billion spans a month, and engineers still complain that they cannot find traces for failed checkouts because a head-based 5% sample drops most of them.
- Agents are deployed as a DaemonSet with memory limiter, resource detection and batch processors.
- A first gateway layer load-balances by trace ID to a second layer of three collectors running tail sampling.
- Policies keep all traces with an error status, all traces above the checkout latency objective, and 2% of the remaining traces.
- Request-rate and latency metrics are generated from spans before sampling, so dashboards stay accurate.
- A redaction rule removes the
user.emailattribute and masks email-like strings in span names.
Stored span volume falls to roughly a tenth of the original in this example, while every failed checkout trace is kept. The numbers depend on the error rate and traffic shape; the point is that the cost now follows a policy the team chose, not the accident of who instrumented what.
Common pitfalls
- Running tail sampling on several gateway replicas without routing spans by trace ID.
- Deriving latency metrics from sampled traces, which skews dashboards.
- Letting each service choose its own labels with no cardinality budget.
- Redacting in the back end, after data has already been stored and replicated.
- Omitting the memory limiter, so a burst crashes the collector and drops data.
- Treating collector configuration as a manual setting instead of versioned code.
FAQ
Do we need agents if services can export directly to the gateway?
Not always. Direct export is simpler for small setups. Agents become worthwhile when you want local buffering, host metadata enrichment and fewer connections from every pod to the gateway.
Should sampling rates be the same for every service?
Usually not. High-volume, low-value paths can be sampled aggressively, while critical flows such as checkout or authentication keep more. Tail-sampling policies can be scoped by service or attribute.
Is the Collector a single point of failure?
The gateway tier is shared, so run it with several replicas, autoscaling and health checks. Agents should buffer and retry so a short gateway outage does not lose data.
Checklist
- Agent and gateway roles defined, with configuration in version control.
- Memory limiter first and batching last in every pipeline.
- Tail sampling behind trace-ID-aware load balancing.
- RED metrics generated before sampling.
- Cardinality budget per service with a regular inventory.
- Redaction rules tested with sample payloads; retention set per signal.