Sub topic 4
Setting up the OpenTelemetry SDK
This chapter assumes you know basic Python, including context managers, and that you have called an LLM API before. You should also know what a local HTTP service is. By the end you will have a working tracer that sends spans to a local Collector.
The three pieces of setup
The OpenTelemetry API only defines how to create spans. Without an SDK behind it, every call is a no-op. Setup connects three SDK objects:
- A Resource describes the thing producing telemetry. Its most important attribute is
service.name, which groups all spans from your app in the backend. - A TracerProvider creates tracers and owns the processors. You register it once, globally, at startup.
- A span processor takes finished spans and hands them to an exporter.
BatchSpanProcessorqueues spans and sends them in the background, so your model calls are not slowed by network I/O.
Install the packages
pip install opentelemetry-sdk opentelemetry-exporter-otlp-proto-httpConfigure the provider
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")The exporter uses OTLP over HTTP. Port 4318 is the Collector's default for that protocol, and /v1/traces is the path for trace data. The string passed to get_tracer names the instrumentation scope, which is usually your module or package.
Run a Collector to receive spans
This minimal configuration accepts OTLP/HTTP and prints each span to the console. Use it to confirm your 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. Run it with a Collector distribution such as otelcol-contrib --config collector.yaml, or with the Collector's container image.
Verify it works
Add a span after the setup code:
with tracer.start_as_current_span("hello"):
print("inside a span")
provider.shutdown()The shutdown() call flushes the batch queue. Without it, a short script can exit before the processor sends anything, and you would see no output.
Common errors
- No spans appear. Check that
set_tracer_providerruns before any code callsget_tracer. Also check that the script flushes or shuts down before exit. - Connection refused warnings. The Collector is not running, or the port is wrong. The exporter logs the failure but does not crash your app.
- Every service shows as
unknown_service. The resource was not set, soservice.nameis missing. - Setting the provider twice. OpenTelemetry ignores later attempts and logs a warning. Configure it once at startup.
Exercise
Why does the example call provider.shutdown() at the end of a short script, and what would you likely see without it?
Show answer
BatchSpanProcessor sends spans from a background queue on a timer or when the batch fills. A short script can exit before that happens, so the queued spans are never exported. shutdown() flushes the queue. Without it, the Collector would show no spans even though the code ran.