Sub topic 5
Exporting with OTLP through a Collector
This chapter assumes you can run a Python script and a local service, and that you have set up a tracer provider as in the previous chapter. Here you will see how spans leave your process and why a Collector sits in the middle.
What OTLP is
OTLP (the OpenTelemetry Protocol) is the standard wire format for sending traces, metrics and logs. It runs over two transports:
| Transport | Default port | Typical endpoint path for traces |
|---|---|---|
| gRPC | 4317 | none (gRPC service) |
| HTTP (protobuf) | 4318 | `/v1/traces` |
The setup code from the SDK chapter uses OTLP/HTTP. With the OTLPSpanExporter from the HTTP package, an endpoint passed in code is used exactly as written, so you must include /v1/traces. The OTEL_EXPORTER_OTLP_ENDPOINT environment variable behaves differently: the SDK appends the signal path for you.
Why use a Collector
Your app could send spans straight to a backend. A Collector is a separate process that receives, processes and forwards telemetry. It helps for these reasons:
- Your app only knows one local address. You can change backends without redeploying the app.
- The Collector can batch, retry, drop or redact data, so the app does less work.
- API keys for the backend live in the Collector, not in application code.
A Collector pipeline has three parts: receivers (how data comes in), processors (what happens to it) and exporters (where it goes).
A minimal Collector configuration
This configuration receives OTLP/HTTP traces and prints them to the console. Use it to check 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]Defining a component under receivers, processors or exporters does not activate it. Only components listed in a pipeline under service run. Forgetting that listing is a common reason for "my config is valid but nothing arrives".
To run it with Docker, save the file as config.yaml and start the contrib image:
docker run --rm -p 4318:4318 \
-v "$(pwd)/config.yaml:/etc/otelcol-contrib/config.yaml" \
otel/opentelemetry-collector-contrib:latestThen run your instrumented app. Each span should print in the Collector's output, including the service.name resource attribute and any gen_ai.* attributes.
Common errors
- Nothing appears. Check that the app's endpoint matches the Collector port and that
/v1/tracesis present when you set the endpoint in code. - Connection refused in a container.
localhostinside a container is the container itself. Use the Collector's service name or host address. - Spans missing at exit.
BatchSpanProcessorbuffers spans. A short script can exit before the buffer flushes, so callprovider.shutdown()orprovider.force_flush()at the end. - Wrong protocol. Sending HTTP to the gRPC port, or the reverse, fails. Match the exporter package to the receiver protocol.
Exercise
Your Python app sets endpoint="http://localhost:4318" in OTLPSpanExporter (the HTTP exporter), and the Collector shows no spans. What is the likely cause, and what is the fix?
Show answer
When the endpoint is passed in code, the HTTP exporter uses it as written and does not append the signal path. The request goes to the wrong URL. Change it to http://localhost:4318/v1/traces, or set the OTEL_EXPORTER_OTLP_ENDPOINT environment variable to http://localhost:4318 so the SDK adds the path.
Exercise
A Collector config defines an otlp receiver, a batch processor and a debug exporter, but the service section is empty. Will it process traces?
Show answer
No. Components run only when they are listed in a pipeline under service.pipelines. Add a traces pipeline that names the receiver, processor and exporter.