Sub topic 15
Instrumentation libraries and auto-instrumentation
This chapter assumes you can set up the SDK (provider, resource, processor) and have hand-written a model call span. Here you decide when to let a library create spans for you and when to keep writing them yourself.
What auto-instrumentation does
An instrumentation library patches a client library at import or startup time. It wraps calls such as chat.completions.create and emits a span for each one. The span carries attributes like the model name and token counts, and it needs no edits to your call sites.
There are two common ways to turn it on:
- Run your program through a launcher, such as
opentelemetry-instrument python app.py, which loads every installed instrumentation. - Call the library's
instrument()method in your own startup code.
The second way gives you explicit control, so this chapter uses it.
Using an instrumentation library
Instrumentation packages are separate installs, one per client library. Names and maturity vary by provider, so check the package's README. Many GenAI packages are still marked experimental.
The order matters. Configure the SDK first, then call instrument(), then create your client.
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
from opentelemetry.instrumentation.requests import RequestsInstrumentor
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)
# Patches the requests library. Every outgoing HTTP call now gets a client span.
RequestsInstrumentor().instrument()RequestsInstrumentor is a real, stable package (opentelemetry-instrumentation-requests). A GenAI library for your provider follows the same pattern. If you call instrument() before the provider is set, spans go to a no-op tracer and are lost.
How auto spans fit with manual spans
Auto-instrumentation uses the same context mechanism as your own code. A span it creates becomes a child of whatever span is current. So your manual agent span can wrap automatic spans:
This mix is common. Libraries cover the repetitive client calls. You add spans for the parts only you understand, such as the agent loop and business tools.
Trade-offs and common errors
| Concern | Auto-instrumentation | Manual spans |
|---|---|---|
| Effort | Low | Higher |
| Attribute names | Follow the library's version of the conventions | You choose and keep consistent |
| Agent structure | Usually absent | You define it |
| Content capture | May be on or off by default | You decide |
Watch for these problems:
- Double spans. Instrumenting both an HTTP library and a provider SDK built on it produces two spans for one call. Disable one.
- Content leaking. Some libraries can record prompts and responses. Check the default and set it explicitly, as covered in the chapter on capturing content safely.
- Version drift. The GenAI conventions are still evolving, so attribute names may differ between library versions. Pin versions and check the output.
- Late patching. If a client was created before
instrument(), it may not be patched.
Exercise
Your service creates an OpenAI-style client at import time, then configures the SDK and calls instrument() in main(). No model spans appear. What are two likely causes?
Show answer
First, the client was created before instrument() ran, so it was not patched. Second, instrument() may have run before the tracer provider was set, so spans went to a no-op tracer. Fix both by setting the provider, calling instrument(), and only then creating the client.