|
| 1 | +OpenTelemetry OpenAI Agents Instrumentation |
| 2 | +=========================================== |
| 3 | + |
| 4 | +|pypi| |
| 5 | + |
| 6 | +.. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation-openai-agents-v2.svg |
| 7 | + :target: https://pypi.org/project/opentelemetry-instrumentation-openai-agents-v2/ |
| 8 | + |
| 9 | +This library provides the official OpenTelemetry instrumentation for the |
| 10 | +`openai-agents SDK <https://pypi.org/project/openai-agents/>`_. It converts |
| 11 | +the rich trace data emitted by the Agents runtime |
| 12 | +into the GenAI semantic conventions, enriches spans with request/response payload |
| 13 | +metadata, and records duration/token usage metrics. |
| 14 | + |
| 15 | +Features |
| 16 | +-------- |
| 17 | + |
| 18 | +* Generates spans for agents, tools, generations, guardrails, and handoffs using |
| 19 | + the OpenTelemetry GenAI semantic conventions. |
| 20 | +* Captures prompts, responses, tool arguments, and system instructions when content |
| 21 | + capture is enabled. |
| 22 | +* Publishes duration and token metrics for every operation. |
| 23 | +* Supports environment overrides so you can configure agent metadata or disable |
| 24 | + telemetry without code changes. |
| 25 | + |
| 26 | +Installation |
| 27 | +------------ |
| 28 | + |
| 29 | +If your application is already configured with OpenTelemetry, install the package |
| 30 | +and its optional instruments: |
| 31 | + |
| 32 | +.. code-block:: console |
| 33 | +
|
| 34 | + pip install opentelemetry-instrumentation-openai-agents-v2 |
| 35 | + pip install openai-agents |
| 36 | +
|
| 37 | +Usage |
| 38 | +----- |
| 39 | + |
| 40 | +Instrumentation automatically wires the Agents tracing processor into the SDK. |
| 41 | +Configure OpenTelemetry as usual, then call :class:`OpenAIAgentsInstrumentor`. |
| 42 | + |
| 43 | +.. code-block:: python |
| 44 | +
|
| 45 | + from agents import Agent, Runner, function_tool |
| 46 | + from opentelemetry import trace |
| 47 | + from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter |
| 48 | + from opentelemetry.instrumentation.openai_agents import OpenAIAgentsInstrumentor |
| 49 | + from opentelemetry.sdk.trace import TracerProvider |
| 50 | + from opentelemetry.sdk.trace.export import BatchSpanProcessor |
| 51 | +
|
| 52 | +
|
| 53 | + def configure_otel() -> None: |
| 54 | + provider = TracerProvider() |
| 55 | + provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter())) |
| 56 | + trace.set_tracer_provider(provider) |
| 57 | +
|
| 58 | + OpenAIAgentsInstrumentor().instrument(tracer_provider=provider) |
| 59 | +
|
| 60 | +
|
| 61 | + @function_tool |
| 62 | + def get_weather(city: str) -> str: |
| 63 | + return f"The forecast for {city} is sunny with pleasant temperatures." |
| 64 | +
|
| 65 | +
|
| 66 | + assistant = Agent( |
| 67 | + name="Travel Concierge", |
| 68 | + instructions="You are a concise travel concierge.", |
| 69 | + tools=[get_weather], |
| 70 | + ) |
| 71 | +
|
| 72 | + result = Runner.run_sync(assistant, "I'm visiting Barcelona this weekend. How should I pack?") |
| 73 | + print(result.final_output) |
| 74 | +
|
| 75 | +Configuration |
| 76 | +------------- |
| 77 | + |
| 78 | +The instrumentor exposes runtime toggles through keyword arguments and environment |
| 79 | +variables: |
| 80 | + |
| 81 | +* ``OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT`` or |
| 82 | + ``OTEL_INSTRUMENTATION_OPENAI_AGENTS_CAPTURE_CONTENT`` – controls how much |
| 83 | + message content is captured. Valid values map to |
| 84 | + :class:`opentelemetry.instrumentation.openai_agents.ContentCaptureMode` |
| 85 | + (``span_only``, ``event_only``, ``span_and_event``, ``no_content``). |
| 86 | +* ``OTEL_INSTRUMENTATION_OPENAI_AGENTS_CAPTURE_METRICS`` – set to ``false`` to |
| 87 | + disable duration/token metrics. |
| 88 | +* ``OTEL_INSTRUMENTATION_OPENAI_AGENTS_SYSTEM`` – overrides the ``gen_ai.system`` |
| 89 | + attribute when your deployment is not the default OpenAI platform. |
| 90 | + |
| 91 | +You can also override agent metadata directly when calling |
| 92 | +``OpenAIAgentsInstrumentor().instrument(...)`` using ``agent_name``, ``agent_id``, |
| 93 | +``agent_description``, ``base_url``, ``server_address``, and ``server_port``. |
| 94 | + |
| 95 | +Examples |
| 96 | +-------- |
| 97 | + |
| 98 | +The :mod:`examples` directory contains runnable scenarios, including: |
| 99 | + |
| 100 | +* ``examples/manual`` – manual OpenTelemetry configuration for a single agent run. |
| 101 | +* ``examples/content-capture`` – demonstrates span and event content capture. |
| 102 | +* ``examples/zero-code`` – end-to-end setup using environment configuration only. |
| 103 | + |
| 104 | +References |
| 105 | +---------- |
| 106 | + |
| 107 | +* `OpenTelemetry Python Contrib <https://github.com/open-telemetry/opentelemetry-python-contrib>`_ |
| 108 | +* `OpenTelemetry GenAI semantic conventions <https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-spans/>`_ |
| 109 | +* `OpenAI Agents SDK <https://github.com/openai/openai-agents-python>`_ |
0 commit comments