Sub topic 5
Exporting with OTLP through a Collector
This chapter covers how spans leave your application and reach a backend. It assumes you can read basic Python, you have set up a tracer provider as in the previous chapter, and you can run a local service from a terminal. You do not need a tracing backend yet.
What OTLP is
OTLP (the OpenTelemetry Protocol) is the wire format OpenTelemetry uses to send telemetry. It carries traces, metrics and logs. It runs over two transports:
| Transport | Default port | Python exporter package | Typical endpoint |
|---|---|---|---|
| HTTP with protobuf bodies | 4318 | `opentelemetry-exporter-otlp-proto-http` | `http://localhost:4318/v1/traces` |
| gRPC | 4317 | `opentelemetry-exporter-otlp-proto-grpc` | `http://localhost:4317` |
The HTTP exporter needs the full path /v1/traces when you pass endpoint= in code. The gRPC exporter takes only the host and port. Mixing these up is the most common reason spans never arrive.
Most backends and vendors accept OTLP directly, so you can often skip the Collector. The sections below explain why you may not want to.
Why put a Collector in the middle
The Collector is a separate process that receives telemetry, transforms it and forwards it. Your app sends to one local address, and the Collector decides where the data goes.
A Collector gives you these things:
- Decoupling. Changing backends means editing Collector configuration, not redeploying every service.
- Batching and retries outside your app. If the backend is slow, the Collector absorbs the delay instead of your request path.
- Central redaction. You can remove or hash attributes in one place. This matters for LLM apps, where span data may include user text.
- Central sampling. Some sampling strategies need to see a whole trace, which only a shared component can do.
- Fan-out. One stream of spans can go to several destinations.
The cost is one more component to run, monitor and size.
The Collector pipeline model
A Collector configuration has four top-level parts:
- Receivers accept data. The
otlpreceiver listens for OTLP over HTTP, gRPC or both. - Processors change data in flight. Examples are batching, attribute edits and sampling.
- Exporters send data out.
- Service pipelines wire the others together. Defining a component does nothing until a pipeline references it.
That last point catches many people. A receiver listed under receivers: but missing from service.pipelines never starts.
A minimal Collector for development
This configuration receives OTLP/HTTP traces and prints them to the console. Use it to confirm that instrumentation works before you pick a backend.
receivers:
otlp:
protocols:
http:
endpoint: 0.0.0.0:4318
processors:
batch: {}
exporters:
debug:
verbosity: detailed
service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [debug]Save it as collector.yaml. One way to run it is with Docker, using the Collector contrib image:
docker run --rm -p 4318:4318 \
-v "$(pwd)/collector.yaml:/etc/otelcol-contrib/config.yaml" \
otel/opentelemetry-collector-contrib:latestThe 0.0.0.0 bind address makes the receiver reachable from outside the container. Binding to localhost inside a container would block the port mapping. On a shared host, think about who can reach port 4318, because the receiver has no authentication by default.
Pointing the SDK at the Collector
The SDK side is the same configuration you saw in the SDK setup chapter. The exporter sends to the Collector's HTTP port.
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
resource = Resource.create({"service.name": "support-agent"})
provider = TracerProvider(resource=resource)
provider.add_span_processor(
BatchSpanProcessor(OTLPSpanExporter(endpoint="http://localhost:4318/v1/traces"))
)
trace.set_tracer_provider(provider)
tracer = trace.get_tracer("support_agent")Install the packages first:
pip install opentelemetry-sdk opentelemetry-exporter-otlp-proto-httpYou can also configure the exporter with environment variables instead of code. This keeps endpoints out of source and lets operations change them per environment:
export OTEL_SERVICE_NAME=support-agent
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobufWith OTEL_EXPORTER_OTLP_ENDPOINT, the HTTP exporter appends /v1/traces itself. If you set the signal-specific variable OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, you must include the full path, because the SDK uses that value as given.
Verifying the pipeline end to end
With the Collector running, execute a small script that creates a span and flushes it:
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
provider = TracerProvider(resource=Resource.create({"service.name": "support-agent"}))
provider.add_span_processor(
BatchSpanProcessor(OTLPSpanExporter(endpoint="http://localhost:4318/v1/traces"))
)
trace.set_tracer_provider(provider)
tracer = trace.get_tracer("support_agent")
with tracer.start_as_current_span("smoke test") as span:
span.set_attribute("check", "collector reachable")
provider.shutdown()The Collector's terminal should print a block describing a span named smoke test, with service.name: Str(support-agent) in the resource attributes. The call to provider.shutdown() matters. It flushes the batch queue before the process exits.
Batching and what it means for your app
BatchSpanProcessor queues finished spans and sends them from a background thread. This keeps export latency off your request path. It has three consequences:
- Spans are delayed. By default the processor exports roughly every five seconds, or sooner when a batch fills up.
- Short-lived processes can lose spans. A script or serverless function that exits before the next flush drops whatever is queued. Call
provider.force_flush()orprovider.shutdown()before exit. - The queue is bounded. If the Collector is down or slow and the queue fills, the SDK drops new spans rather than blocking your app. Export failures are logged by the SDK, so check your application logs when traces go missing.
SimpleSpanProcessor exports each span synchronously when it ends. It is handy in a unit test but adds network latency to every span, so avoid it in production.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| No output in the Collector | Wrong port, or path missing from the HTTP endpoint | Use `http://localhost:4318/v1/traces` in code |
| Connection refused from a container | Collector bound to `localhost` | Bind the receiver to `0.0.0.0:4318` |
| Collector starts, but receives nothing | Receiver not listed in a pipeline | Add it under `service.pipelines.traces.receivers` |
| Spans appear with `unknown_service` | `service.name` not set | Set it on the `Resource` or with `OTEL_SERVICE_NAME` |
| Script runs, but no spans arrive | Process exited before the flush | Call `provider.shutdown()` |
| Protocol errors or timeouts | HTTP exporter pointed at the gRPC port, or the reverse | Match exporter package, protocol and port |
Choosing between direct export and a Collector
Export directly to a backend when you run a single service, you are prototyping, or your vendor's endpoint meets your needs. Add a Collector when several services share a destination, when you need redaction or tail-based sampling, or when you want to switch backends without touching application code.
For LLM apps there is a further reason. Prompt and response content can appear in span attributes or events, and the Collector gives you one place to enforce what leaves your network. Later chapters on content capture and sampling build on this setup.
Exercise
Your script sends one span to a Collector running in Docker with the minimal configuration above. The script finishes without errors, but the Collector prints nothing. List three things you would check, in the order you would check them.
Show answer
First, confirm the exporter endpoint is http://localhost:4318/v1/traces, including the /v1/traces path, and that the port is mapped with -p 4318:4318. Second, confirm the receiver is bound to 0.0.0.0:4318 and is listed under service.pipelines.traces.receivers. Third, confirm the script calls provider.shutdown() or provider.force_flush() before exiting, because the batch processor may not have exported the span yet. Also check the application logs for exporter errors.
Exercise
Why might you choose to remove a prompt-content attribute in the Collector instead of in the application code?
Show answer
The Collector applies the rule in one place for every service that sends to it, so each team does not have to implement and maintain its own redaction. It also lets operations change the policy without redeploying applications. The trade-off is that the raw content has already left the application process and crossed the network to the Collector, so the hop must be trusted. If content should never leave the app, avoid recording it there.