|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Bridges agents-library tracing callbacks to opentelemetry-util-genai. |
| 5 | +
|
| 6 | +The agents library exposes a public extension API |
| 7 | +(:func:`agents.tracing.add_trace_processor`) for plugging custom |
| 8 | +:class:`TracingProcessor` implementations into its own tracing system. |
| 9 | +``Trace.start()`` / ``Span.start()`` invoke the registered processors' |
| 10 | +``on_*_start`` callbacks *synchronously* on whichever asyncio task |
| 11 | +started the agents-library span: |
| 12 | +
|
| 13 | +* Workflow (``Trace``) and agent (``AgentSpanData``) spans start in the |
| 14 | + ``Runner.run`` task itself. |
| 15 | +* Function tool (``FunctionSpanData``) spans start inside the per-tool |
| 16 | + ``asyncio.Task`` the agents library creates for tool dispatch. That |
| 17 | + sub-task inherits a snapshot of the run-loop context (so workflow + |
| 18 | + agent are already active in OTel contextvars). |
| 19 | +
|
| 20 | +Because every ``*_end`` callback fires on the same task as its |
| 21 | +matching ``*_start``, util-genai's auto-``attach()`` / ``detach()`` of |
| 22 | +OTel context is balanced and no context tokens leak across tasks. |
| 23 | +OTel's natural parent tracking nests the tree: |
| 24 | +
|
| 25 | + workflow > invoke_agent > [chat from openai instrumentation, |
| 26 | + execute_tool] |
| 27 | +
|
| 28 | +LLM-level spans (``chat`` / ``responses`` / ``embeddings``) are not |
| 29 | +emitted here — ``opentelemetry-instrumentation-genai-openai`` patches |
| 30 | +the openai SDK directly and produces those. |
| 31 | +""" |
| 32 | + |
| 33 | +from __future__ import annotations |
| 34 | + |
| 35 | +import weakref |
| 36 | +from typing import Any |
| 37 | + |
| 38 | +from agents.tracing import Span, Trace, TracingProcessor |
| 39 | +from agents.tracing.span_data import ( |
| 40 | + AgentSpanData, |
| 41 | + FunctionSpanData, |
| 42 | +) |
| 43 | + |
| 44 | +from opentelemetry.semconv._incubating.attributes import ( |
| 45 | + gen_ai_attributes as GenAI, |
| 46 | +) |
| 47 | +from opentelemetry.util.genai.handler import TelemetryHandler |
| 48 | +from opentelemetry.util.genai.invocation import ( |
| 49 | + GenAIInvocation, |
| 50 | + ToolInvocation, |
| 51 | +) |
| 52 | + |
| 53 | +# Non-semconv attribute: surfaces the workflow name on the workflow span |
| 54 | +# so callers can query/filter by it. util-genai's WorkflowInvocation |
| 55 | +# only puts the name in the span name, not as an attribute. |
| 56 | +_WORKFLOW_NAME_ATTR = "gen_ai.workflow.name" |
| 57 | + |
| 58 | + |
| 59 | +class GenAITracingProcessor(TracingProcessor): |
| 60 | + """Translate agents-library tracing into util-genai invocations. |
| 61 | +
|
| 62 | + Stateful only for span lifetime: each in-flight Trace/Span has one |
| 63 | + entry in a :class:`weakref.WeakKeyDictionary` keyed by the |
| 64 | + agents-library object itself. Entries are removed on ``*_end`` or |
| 65 | + garbage-collected with the agents-library span/trace if the library |
| 66 | + drops it before ``end`` (which it shouldn't, but the weak reference |
| 67 | + is belt-and-suspenders against any future leak). |
| 68 | + """ |
| 69 | + |
| 70 | + def __init__(self, handler: TelemetryHandler, provider: str) -> None: |
| 71 | + self._handler = handler |
| 72 | + self._provider = provider |
| 73 | + self._invocations: weakref.WeakKeyDictionary[ |
| 74 | + Any, GenAIInvocation |
| 75 | + ] = weakref.WeakKeyDictionary() |
| 76 | + |
| 77 | + def on_trace_start(self, trace: Trace) -> None: |
| 78 | + # ``trace.name`` comes from ``RunConfig.workflow_name`` (default |
| 79 | + # "Agent workflow"). Callers customize it via the agents library's |
| 80 | + # own ``Runner.run(..., run_config=RunConfig(workflow_name=...))``; |
| 81 | + # we don't expose a second knob. |
| 82 | + invocation = self._handler.workflow(name=trace.name) |
| 83 | + if trace.name: |
| 84 | + invocation.attributes[_WORKFLOW_NAME_ATTR] = trace.name |
| 85 | + self._invocations[trace] = invocation |
| 86 | + |
| 87 | + def on_trace_end(self, trace: Trace) -> None: |
| 88 | + invocation = self._invocations.pop(trace, None) |
| 89 | + if invocation is not None: |
| 90 | + invocation.stop() |
| 91 | + |
| 92 | + def on_span_start(self, span: Span[Any]) -> None: |
| 93 | + span_data = span.span_data |
| 94 | + if isinstance(span_data, AgentSpanData): |
| 95 | + invocation = self._handler.invoke_local_agent( |
| 96 | + provider=self._provider, |
| 97 | + agent_name=span_data.name, |
| 98 | + ) |
| 99 | + self._invocations[span] = invocation |
| 100 | + return |
| 101 | + if isinstance(span_data, FunctionSpanData): |
| 102 | + invocation = self._handler.tool( |
| 103 | + name=span_data.name, |
| 104 | + arguments=span_data.input, |
| 105 | + tool_type="function", |
| 106 | + ) |
| 107 | + # ToolInvocation does not include provider in metric attributes |
| 108 | + # by default; set it so gen_ai.client.operation.duration carries |
| 109 | + # the required gen_ai.provider.name attribute. |
| 110 | + invocation.metric_attributes[GenAI.GEN_AI_PROVIDER_NAME] = ( |
| 111 | + self._provider |
| 112 | + ) |
| 113 | + self._invocations[span] = invocation |
| 114 | + return |
| 115 | + # Other span_data types (GenerationSpanData, ResponseSpanData, |
| 116 | + # HandoffSpanData, GuardrailSpanData, Speech/TranscriptionSpanData) |
| 117 | + # are intentionally ignored. LLM-level spans come from the openai |
| 118 | + # instrumentation; the rest have no semconv yet. |
| 119 | + |
| 120 | + def on_span_end(self, span: Span[Any]) -> None: |
| 121 | + invocation = self._invocations.pop(span, None) |
| 122 | + if invocation is None: |
| 123 | + return |
| 124 | + if isinstance(invocation, ToolInvocation) and isinstance( |
| 125 | + span.span_data, FunctionSpanData |
| 126 | + ): |
| 127 | + output = span.span_data.output |
| 128 | + if output is not None: |
| 129 | + invocation.tool_result = ( |
| 130 | + output if isinstance(output, str) else str(output) |
| 131 | + ) |
| 132 | + invocation.stop() |
| 133 | + |
| 134 | + def shutdown(self) -> None: |
| 135 | + for invocation in list(self._invocations.values()): |
| 136 | + try: |
| 137 | + invocation.stop() |
| 138 | + except Exception: # pylint: disable=broad-except |
| 139 | + pass |
| 140 | + self._invocations.clear() |
| 141 | + |
| 142 | + def force_flush(self) -> None: # pragma: no cover - nothing to flush |
| 143 | + pass |
0 commit comments