Course contents

13. Observability Lesson 4 of 6

OpenTelemetry

The OpenTelemetry API, SDK and Collector pipeline: OTLP, one context for traces, metrics and logs, head vs tail sampling, and scaling collectors.

13 minIntermediate9 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • The API is what code calls and is a no-op without an SDK; the SDK samples, batches and exports.
  • Services send OTLP to a Collector, which batches, redacts, retries and routes to any backend.
  • Traces, metrics and logs share context: trace ids in log records, exemplars in metrics.
  • Head sampling decides at the root, cheaply and blind to outcome; tail sampling decides after the trace completes and keeps errors and slow traces.
  • Tail sampling at scale needs routing by trace id (agent → gateway); pipelines drop telemetry under pressure, never requests.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

OpenTelemetry (OTel) is a CNCF project that standardises how software produces and transports telemetry: an API and SDKs for many languages, instrumentation libraries for common frameworks, semantic conventions for attribute names, the OTLP protocol, and the Collector. It does not store or visualise data; backends do.

Why it exists

Before OTel, each vendor had its own agents, libraries and formats, so changing backends meant re-instrumenting every service, and libraries could not ship instrumentation without picking a vendor. OTel separates producing telemetry (once, in a standard way) from storing and analysing it (any backend).

How it works

API and SDK. Libraries and application code call the API (tracer.startSpan, meter.createCounter). Without an SDK, calls are no-ops. The application installs and configures the SDK once: a resource (service.name, version, environment), a sampler, processors (e.g. batch) and exporters. Configuration can also come from standard environment variables such as OTEL_SERVICE_NAME and OTEL_EXPORTER_OTLP_ENDPOINT.

Instrumentation. Instrumentation libraries (or zero-code agents in Java, .NET, Python, Node.js and others) create spans and metrics for HTTP servers and clients, gRPC, database drivers and messaging clients, and propagate W3C Trace Context. You add spans and attributes for business logic.

OTLP. The OpenTelemetry Protocol carries traces, metrics and logs over gRPC (port 4317) or HTTP (4318, protobuf or JSON).

Collector. A standalone binary configured as pipelines per signal: receivers (OTLP, Prometheus scraping, host metrics, log files…) → processors (memory_limiter, batch, attribute and resource rules, filtering, tail_sampling) → exporters (OTLP to a backend, Prometheus remote write, vendor exporters). Distributions (core, contrib, vendor builds) differ in which components they include.

Signals and context. Logs emitted within a span carry its trace and span ids; metric points can carry exemplars that reference a trace. This is what lets you jump between signals.

Sampling. Head sampling happens in the SDK at the root span, typically ParentBased(TraceIdRatioBased(p)): the root decides by trace id and children follow the sampled flag, so traces are complete and the cost is low, but the decision is blind to outcome. Tail sampling happens in the Collector: it buffers spans per trace for a decision window, then applies policies (errors, latency above a threshold, attributes, a probabilistic baseline). It needs all spans of a trace on the same collector instance.

Scaling. A common layout is an agent collector per node or as a sidecar, close to the apps, plus a pool of gateway collectors. Agents use the load-balancing exporter with trace id as the routing key, so each trace's spans reach one gateway, which tail-samples and exports.

Backpressure. SDK and Collector exporters use bounded queues with retries. When a backend is slow, the memory_limiter refuses data, upstream exporters retry and eventually drop. The Collector exposes its own metrics (accepted, refused, dropped, queue size) for monitoring.

When to use it

For new instrumentation in almost any service today: it is supported by all major observability vendors and open-source backends. Use the Collector whenever more than a handful of services export telemetry, or when you need redaction, sampling or multiple destinations.

When not to

  • A tiny system where a single vendor agent already does everything and switching is unlikely may not need the extra moving part of a Collector.
  • Do not expect OTel to replace a backend: you still need storage, querying, dashboards and alerting.
  • Some language SDKs and signals mature at different speeds; check the status of the signal and language you depend on.

Trade-offs

Choice You gain You pay
Collector instead of direct export Central config, redaction, batching, vendor independence Another service to run, scale and monitor
Head sampling Cheap, simple, complete traces Rare errors and slow traces are mostly lost
Tail sampling Keeps errors and slow traces Buffering memory, all spans sent first, trace-id routing
Agent + gateway tiers Scalability, tail sampling across instances More components, more configuration
Zero-code agents Fast coverage Less control; generic span names; runtime overhead

Alternatives

  • Vendor-specific agents and SDKs: often feature-rich, but lock instrumentation to one vendor.
  • Prometheus client libraries for metrics only, scraped directly; OTel can interoperate with them.
  • eBPF-based instrumentation: no code changes, sees network-level behaviour, limited view inside processes and of business context.

Failure modes

  • Silent drops: queues overflow during backend outages and nobody watches the collector's dropped-data metrics.
  • Fragmented tail sampling: spans of one trace reach different gateways, so policies decide on fragments.
  • Collector OOM: no memory_limiter, or a tail-sampling window too long for the traffic.
  • Missing resource attributes: telemetry without service.name or environment cannot be filtered.
  • Inconsistent sampling: services with different head samplers that ignore the parent's decision produce partial traces.
  • Sensitive data: attributes with personal data exported before redaction.

In production

Instrument with OTel SDKs and auto-instrumentation, set resource attributes everywhere, export OTLP to a local agent collector, route by trace id to a gateway pool for tail sampling, batch and limit memory at every collector, redact in the pipeline, and monitor the pipeline's own metrics. Combine a modest head-sampling rate with tail rules if volume is very high.

Common misconceptions

  • "OpenTelemetry is a backend." It produces and transports telemetry; storage and UI come from elsewhere.
  • "The Collector is required." It is optional, but recommended in production.
  • "Head sampling keeps errors." It decides before the outcome is known.
  • "If the telemetry backend is down, my app slows down." Correctly configured exporters drop data instead of blocking.

Interview angle

You may be asked how to add observability to a fleet of services. Describe OTel SDKs with auto-instrumentation, OTLP to collectors, per-signal pipelines, resource attributes and semantic conventions, and the head versus tail sampling trade-off including trace-id routing for tail sampling. Mention backpressure behaviour and monitoring the pipeline.

Model assumptions

This lesson is fully scripted: no node runs in the simulation engine. Requests, span exports and refusals are scripted packets, and configuration snippets are annotations; timings are illustrative. Component names and configuration keys follow the OpenTelemetry specification and Collector contrib components as of 2026, but exact options vary by version and distribution.

The lesson as textEvery step of the simulation, written out.
  1. 1. API in your code, SDK in your process

    OpenTelemetry (OTel) is a vendor-neutral standard for producing traces, metrics and logs. In each service you meet two layers. The API is what code calls: start a span, add an attribute, record a counter. Libraries can depend on it safely, because without an SDK installed every call is a cheap no-op.

    The SDK is what the application installs at startup: it decides which spans to keep (sampler), batches them (span processor) and sends them somewhere (exporter), tagged with resource attributes such as service.name. Instrumentation libraries for HTTP servers, clients and database drivers create most spans for you; you add spans for business steps.

    Right now Checkout calls Payments and spans are recorded in memory. Nothing leaves either process yet.

    Scene: A user request reaches Checkout, which calls Payments. Both services have the OpenTelemetry SDK; spans are recorded in memory and nothing is exported yet.

    Key idea: API = what code calls (no-op by default); SDK = sampler + processor + exporter, configured once per process.

  2. 2. Every service talks to every backend

    The simplest setup: each SDK exports straight to the backends. Watch the purple packets: after the request, both services send to both backends.

    It works, and it is fine for a laptop. In production it spreads concerns into every service: backend addresses and API keys in every deployment, retries and buffering inside the application process, and switching vendors means redeploying everything. Add a third backend and every service changes again.

    Scene: After the request, Checkout and Payments each export telemetry directly to both the trace backend and the metrics backend.

    Key idea: Direct export couples every service to every backend: credentials, retries and vendor choice live in application code.

  3. 3. The Collector: one pipeline in the middle

    The OpenTelemetry Collector is a separate process that receives, processes and exports telemetry. Services now send OTLP (the OpenTelemetry protocol, on port 4317 for gRPC or 4318 for HTTP) to one place and forget about backends.

    Inside, each pipeline is receivers → processors → exporters. Typical processors: memory_limiter (refuse data before running out of memory), batch (fewer, bigger exports), attribute rules that drop or redact fields such as emails. Watch the packets: the services hand their spans to the collector, which batches them into one export.

    Scene: Checkout and Payments send their spans over OTLP to the OpenTelemetry Collector, which batches them and sends one export to the trace backend.

    Key idea: Apps speak OTLP to a collector; the collector owns batching, redaction, retries and where data goes.

  4. 4. Three signals, one context

    The same collector handles all three signals, each in its own pipeline: traces to the trace backend, metrics to the metrics backend, logs to the log backend. Watch the fan-out after the batch.

    What makes them one system is shared context. Log records emitted inside a span carry its trace_id and span_id; histogram points can carry exemplars, a sample trace id for a slow bucket. From a latency spike you jump to an example trace, and from a span to its log lines.

    Scene: The collector receives the services’ telemetry and exports traces to the trace backend, metrics to the metrics backend and logs to the log backend.

    Key idea: Separate pipelines per signal, joined by the same trace context (trace ids in logs, exemplars in metrics).

  5. 5. Head sampling: decide at the start

    Keeping every trace is expensive, so traces are sampled. Head sampling decides when the root span starts, before anything is known about the request. A common sampler keeps a fixed share by trace id (TraceIdRatioBased, here 10%) and puts the decision into the sampled flag of traceparent, so every downstream service follows it and traces stay complete.

    Ten requests arrive; only the third one is sampled. The sixth one fails (red), and its trace is gone: the decision was made before anyone knew it would fail. Head sampling is cheap and simple, and blind to outcome.

    Scene: Ten requests reach Checkout. Only the third is sampled and exported through the collector. The sixth request fails, but its trace was not sampled and is lost.

    Key idea: Head sampling: cheap, consistent across services, decided before the outcome, so rare errors are usually lost.

  6. 6. Tail sampling: decide at the end

    Tail sampling moves the decision to the collector, after the trace is complete. Services send every span; the collector holds each trace for a decision window (often around 10 seconds) and then applies policies: keep every trace with an error, keep every trace slower than 1 s, keep 10% of the rest.

    All ten traces reach the collector; three leave it: the error, a slow one and one baseline sample. You keep the interesting traces, at the price of sending and buffering everything first, which costs network and collector memory.

    Scene: All ten traces are sent to the collector. After its decision window it exports three to the trace backend: the failed request, a slow one and one baseline sample.

    Key idea: Tail sampling keeps errors and slow traces by deciding after the fact; it costs buffering every trace in the collector.

  7. 7. Scaling out: agents and gateways

    One collector is a single point of failure, so you run several. That breaks tail sampling: a trace’s spans come from different services, and if they land on different collectors none of them sees the whole trace.

    The usual layout has two tiers. An agent collector next to the services (a sidecar or one per node) uses the load-balancing exporter to route by trace id, so all spans of a trace reach the same gateway collector, which does the tail sampling. Watch it: both spans of the first trace go to Gateway 1, both of the second to Gateway 2.

    Scene: Services send spans to an agent collector, which routes them by trace id: both spans of trace A go to Gateway Collector 1, both spans of trace B to Gateway Collector 2. Each gateway exports to the trace backend.

    Key idea: Tail sampling at scale needs routing by trace id: agents in front, sampling gateways behind.

  8. 8. When the pipeline is full

    The trace backend slows down. Gateway 1’s queue grows until its memory_limiter starts refusing data (red). The agent retries with backoff and buffers; when its buffer is full it drops data. The SDKs behave the same way.

    Meanwhile the next user request is served normally. That is deliberate: telemetry export is asynchronous and bounded, so an observability outage loses telemetry, never requests. Watch for the dropped-data counters the collector itself exports (otelcol_* metrics), or you will not notice the gap until you need the data.

    Scene: The trace backend is slow. Gateway Collector 1 refuses data from the agent, which retries and then drops it. A new user request is still served normally.

    Key idea: Telemetry pipelines shed load by dropping data, never by blocking the application. Monitor the pipeline itself.

  9. 9. What OpenTelemetry standardises

    OpenTelemetry standardises the API, the SDKs, the semantic conventions (attribute names such as http.request.method or db.system), the OTLP protocol and the Collector. It does not store or display anything: backends do, and you can switch them by changing collector config, not application code.

    Sampling is a cost decision: head sampling is cheap and simple; tail sampling keeps errors and slow traces but needs routing by trace id and collector capacity. Many teams combine them: a modest head rate, plus tail rules in the gateway.

    Scene: The full pipeline: services with the OpenTelemetry SDK send to an agent collector, which routes by trace id to two gateway collectors that export to the trace, metrics and log backends.

    Key idea: Instrument once with OTel; choose and change backends in the collector.