Quickstart: OpenTelemetry OpenAI instrumentation
Esta página aún no está disponible en tu idioma.
opentelemetry-instrumentation-openai-v2 is OpenTelemetry’s own Python instrumentation for the OpenAI client. It records each chat call as a GenAI span. This page sends those spans to Mibo and tests the tools your app calls.
Before you start
Section titled “Before you start”- A Mibo account with a verified email. Open the verification link in the browser you signed up with.
- Python 3.14 and uv.
- An OpenAI API key.
The instrumentation is a pre-release. Mibo’s ingest checks use opentelemetry-instrumentation-openai-v2 2.4b0 with opentelemetry-util-genai 0.4b0. Version 2.4b0 fails to import with opentelemetry-util-genai 1.0 or later, so pin 0.4b0. The install line pins httpx 0.28.1 because the instrumentation imports httpx, which current openai releases no longer install. Tested with Python 3.14.9 and these versions.
1. Create a Mibo API key
Section titled “1. Create a Mibo API key”If you own the workspace, create an API key in Settings and copy it. Mibo shows the key only once. If you’re a workspace member, ask the owner for a key.
2. Install the packages
Section titled “2. Install the packages”uv venv --python 3.14uv pip install opentelemetry-instrumentation-openai-v2==2.4b0 opentelemetry-util-genai==0.4b0 \ openai httpx==0.28.1 opentelemetry-sdk==1.45.2 opentelemetry-exporter-otlp-proto-http==1.45.23. Configure content capture and the exporter
Section titled “3. Configure content capture and the exporter”The instrumentation does not record prompts or replies by default. Mibo needs the user input and the final reply, so turn capture on.
export OTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimentalexport OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=span_onlyexport OTEL_SERVICE_NAME=openai-supportexport MIBO_OTLP_TRACES_ENDPOINT=https://api.mibo-ai.com/public/tracesexport MIBO_API_KEY='<YOUR_MIBO_API_KEY>'export OPENAI_API_KEY='<YOUR_OPENAI_API_KEY>'span_onlywrites message content to span attributes. The other capture modes put content in events, which is a different export shape. Usespan_onlyunless you have checked the others against your traces.- With a workspace key, Mibo routes by
OTEL_SERVICE_NAMEand creates an Agent on the first trace if your plan has a free slot. See trace routing.
4. Send your first trace
Section titled “4. Send your first trace”Save the following as support.py.
import jsonimport os
from openai import OpenAIfrom opentelemetry import tracefrom opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporterfrom opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentorfrom opentelemetry.sdk.resources import Resourcefrom opentelemetry.sdk.trace import TracerProviderfrom opentelemetry.sdk.trace.export import BatchSpanProcessor
provider = TracerProvider( resource=Resource.create({"service.name": os.environ["OTEL_SERVICE_NAME"]}),)provider.add_span_processor( BatchSpanProcessor( OTLPSpanExporter( endpoint=os.environ["MIBO_OTLP_TRACES_ENDPOINT"], headers={"x-api-key": os.environ["MIBO_API_KEY"]}, timeout=10, ) ))trace.set_tracer_provider(provider)OpenAIInstrumentor().instrument(tracer_provider=provider)
client = OpenAI(timeout=30, max_retries=1)TOOLS = [ { "type": "function", "function": { "name": "lookup_order", "description": "Look up shipping status by order ID.", "parameters": { "type": "object", "properties": {"order_id": {"type": "string"}}, "required": ["order_id"], }, }, }]
def lookup_order(order_id: str) -> str: return {"A-1042": "shipped"}.get(order_id, "unknown")
def answer(question: str) -> str: messages: list[object] = [ {"role": "system", "content": "You are an order support assistant."}, {"role": "user", "content": question}, ] for _ in range(3): response = client.chat.completions.create( model="gpt-5.4-mini", messages=messages, tools=TOOLS ) message = response.choices[0].message if not message.tool_calls: return message.content or "" messages.append(message.model_dump(exclude_none=True)) for call in message.tool_calls: result = lookup_order(**json.loads(call.function.arguments)) messages.append({"role": "tool", "tool_call_id": call.id, "content": result}) return ""
tracer = trace.get_tracer(__name__)try: with tracer.start_as_current_span("support_request"): print(answer("What is the status of order A-1042?"))finally: provider.shutdown()Run the script.
uv run --no-project python support.pyOpen Traces, select the Agent by its current name, and find lookup_order under What the agent did. Select View raw trace data to inspect the GenAI attributes.
The wrapping support_request span gives the trace a root, so the whole request appears as one trace. Name it for the step your users see.
5. Create your first test
Section titled “5. Create your first test”- Wait for the trace to finish, then click Create test from this trace.
- Set Name to
Looks up the orderand clear Checks a trace when so the test checks each new trace for this Agent. - Keep the Tool call check, with Tool name
lookup_orderand Must call. Remove the other checks and click Save test. - Run the script again. The new trace shows the result under Test results.
What Mibo reads from the trace
Section titled “What Mibo reads from the trace”| Data | Source |
|---|---|
| User input and final reply | gen_ai.input.messages and gen_ai.output.messages on the chat span |
| Tool calls | tool_call parts in the model’s output message. Mibo creates a tool span for each call. |
| Tool results | tool_call_response parts in the next call’s input messages |
| Token usage | gen_ai.usage.input_tokens and gen_ai.usage.output_tokens |
| Model and provider | gen_ai.request.model and the provider name |
| HTTP status and error | http.response.status_code and error.type when present |
Known gaps:
- The instrumentation does not emit reasoning-token or cache-read token counts, so checks on those counts have no data.
- Tool calls exist only as message parts. Mibo infers the tool span from the model’s output, so it records no tool error, and the span reuses the chat call’s time range.
- A tool result appears only after your app sends it back in a later model call. A last call that requests a tool and never receives its result has no result to check.
- Without content capture, Mibo has no input or reply to check.
Troubleshooting
Section titled “Troubleshooting”| Problem | Likely cause | Solution |
|---|---|---|
ImportError when importing the instrumentation |
opentelemetry-util-genai 1.0 or later is installed, or httpx is missing |
Install opentelemetry-util-genai==0.4b0 and httpx==0.28.1. |
| The trace has no prompts or replies | Content capture is off | Set OTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental and OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=span_only before the process starts. |
| No tool calls appear | The model made no tool call, or capture is off | Check gen_ai.output.messages in the raw trace data for a tool_call part. |
| HTTP 401 | Missing, invalid, or revoked key | Set MIBO_API_KEY to an active key. An expired key returns API_KEY_EXPIRED. |
404 or connection error |
Wrong host or path | Set MIBO_OTLP_TRACES_ENDPOINT to https://api.mibo-ai.com/public/traces. |
| HTTP 400 with “OTLP traces need the ‘service.name’ resource attribute” | The export has no service.name |
Set OTEL_SERVICE_NAME and pass it as a resource attribute as shown above. |
| Export fails with a gRPC transport error | The exporter uses OTLP/gRPC | Use the opentelemetry.exporter.otlp.proto.http exporter. |
| The script prints a reply but no trace appears | Spans were buffered when the process exited | Call provider.shutdown() before exit. |
403 AGENT_LIMIT_REACHED |
Your plan has reached its Agent limit | See Agent limit reached. |
| HTTP 403 with “Monthly trace ingestion limit reached” | The workspace used its monthly trace quota | Wait for the next period or upgrade, then rerun the script. |
| HTTP 413 with “Trace payload too large (max 10 MB)” | The batch exceeds 10 MB | Reduce max_export_batch_size. See Payload size limits. |
What’s next
Section titled “What’s next”- OpenLLMetry quickstart for the Traceloop instrumentation.
- OpenTelemetry (OTLP) for routing, batching, and response codes.
- Assertion reference for checks you can add to your test.