openai-agents 0.0.17__py3-none-any.whl → 0.0.19__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of openai-agents might be problematic. Click here for more details.
- agents/__init__.py +8 -0
- agents/_run_impl.py +7 -5
- agents/agent.py +14 -0
- agents/extensions/models/litellm_model.py +11 -1
- agents/function_schema.py +7 -5
- agents/handoffs.py +2 -2
- agents/mcp/server.py +4 -4
- agents/model_settings.py +15 -0
- agents/models/interface.py +6 -0
- agents/models/openai_chatcompletions.py +9 -1
- agents/models/openai_responses.py +10 -0
- agents/prompts.py +76 -0
- agents/repl.py +65 -0
- agents/run.py +221 -97
- agents/tool.py +10 -4
- agents/tool_context.py +29 -0
- agents/tracing/__init__.py +11 -5
- agents/tracing/create.py +16 -16
- agents/tracing/provider.py +294 -0
- agents/tracing/setup.py +13 -206
- agents/tracing/util.py +9 -10
- {openai_agents-0.0.17.dist-info → openai_agents-0.0.19.dist-info}/METADATA +6 -3
- {openai_agents-0.0.17.dist-info → openai_agents-0.0.19.dist-info}/RECORD +25 -21
- {openai_agents-0.0.17.dist-info → openai_agents-0.0.19.dist-info}/WHEEL +0 -0
- {openai_agents-0.0.17.dist-info → openai_agents-0.0.19.dist-info}/licenses/LICENSE +0 -0
agents/tracing/create.py
CHANGED
|
@@ -4,7 +4,7 @@ from collections.abc import Mapping, Sequence
|
|
|
4
4
|
from typing import TYPE_CHECKING, Any
|
|
5
5
|
|
|
6
6
|
from ..logger import logger
|
|
7
|
-
from .setup import
|
|
7
|
+
from .setup import get_trace_provider
|
|
8
8
|
from .span_data import (
|
|
9
9
|
AgentSpanData,
|
|
10
10
|
CustomSpanData,
|
|
@@ -56,13 +56,13 @@ def trace(
|
|
|
56
56
|
Returns:
|
|
57
57
|
The newly created trace object.
|
|
58
58
|
"""
|
|
59
|
-
current_trace =
|
|
59
|
+
current_trace = get_trace_provider().get_current_trace()
|
|
60
60
|
if current_trace:
|
|
61
61
|
logger.warning(
|
|
62
62
|
"Trace already exists. Creating a new trace, but this is probably a mistake."
|
|
63
63
|
)
|
|
64
64
|
|
|
65
|
-
return
|
|
65
|
+
return get_trace_provider().create_trace(
|
|
66
66
|
name=workflow_name,
|
|
67
67
|
trace_id=trace_id,
|
|
68
68
|
group_id=group_id,
|
|
@@ -73,12 +73,12 @@ def trace(
|
|
|
73
73
|
|
|
74
74
|
def get_current_trace() -> Trace | None:
|
|
75
75
|
"""Returns the currently active trace, if present."""
|
|
76
|
-
return
|
|
76
|
+
return get_trace_provider().get_current_trace()
|
|
77
77
|
|
|
78
78
|
|
|
79
79
|
def get_current_span() -> Span[Any] | None:
|
|
80
80
|
"""Returns the currently active span, if present."""
|
|
81
|
-
return
|
|
81
|
+
return get_trace_provider().get_current_span()
|
|
82
82
|
|
|
83
83
|
|
|
84
84
|
def agent_span(
|
|
@@ -108,7 +108,7 @@ def agent_span(
|
|
|
108
108
|
Returns:
|
|
109
109
|
The newly created agent span.
|
|
110
110
|
"""
|
|
111
|
-
return
|
|
111
|
+
return get_trace_provider().create_span(
|
|
112
112
|
span_data=AgentSpanData(name=name, handoffs=handoffs, tools=tools, output_type=output_type),
|
|
113
113
|
span_id=span_id,
|
|
114
114
|
parent=parent,
|
|
@@ -141,7 +141,7 @@ def function_span(
|
|
|
141
141
|
Returns:
|
|
142
142
|
The newly created function span.
|
|
143
143
|
"""
|
|
144
|
-
return
|
|
144
|
+
return get_trace_provider().create_span(
|
|
145
145
|
span_data=FunctionSpanData(name=name, input=input, output=output),
|
|
146
146
|
span_id=span_id,
|
|
147
147
|
parent=parent,
|
|
@@ -183,7 +183,7 @@ def generation_span(
|
|
|
183
183
|
Returns:
|
|
184
184
|
The newly created generation span.
|
|
185
185
|
"""
|
|
186
|
-
return
|
|
186
|
+
return get_trace_provider().create_span(
|
|
187
187
|
span_data=GenerationSpanData(
|
|
188
188
|
input=input,
|
|
189
189
|
output=output,
|
|
@@ -215,7 +215,7 @@ def response_span(
|
|
|
215
215
|
trace/span as the parent.
|
|
216
216
|
disabled: If True, we will return a Span but the Span will not be recorded.
|
|
217
217
|
"""
|
|
218
|
-
return
|
|
218
|
+
return get_trace_provider().create_span(
|
|
219
219
|
span_data=ResponseSpanData(response=response),
|
|
220
220
|
span_id=span_id,
|
|
221
221
|
parent=parent,
|
|
@@ -246,7 +246,7 @@ def handoff_span(
|
|
|
246
246
|
Returns:
|
|
247
247
|
The newly created handoff span.
|
|
248
248
|
"""
|
|
249
|
-
return
|
|
249
|
+
return get_trace_provider().create_span(
|
|
250
250
|
span_data=HandoffSpanData(from_agent=from_agent, to_agent=to_agent),
|
|
251
251
|
span_id=span_id,
|
|
252
252
|
parent=parent,
|
|
@@ -278,7 +278,7 @@ def custom_span(
|
|
|
278
278
|
Returns:
|
|
279
279
|
The newly created custom span.
|
|
280
280
|
"""
|
|
281
|
-
return
|
|
281
|
+
return get_trace_provider().create_span(
|
|
282
282
|
span_data=CustomSpanData(name=name, data=data or {}),
|
|
283
283
|
span_id=span_id,
|
|
284
284
|
parent=parent,
|
|
@@ -306,7 +306,7 @@ def guardrail_span(
|
|
|
306
306
|
trace/span as the parent.
|
|
307
307
|
disabled: If True, we will return a Span but the Span will not be recorded.
|
|
308
308
|
"""
|
|
309
|
-
return
|
|
309
|
+
return get_trace_provider().create_span(
|
|
310
310
|
span_data=GuardrailSpanData(name=name, triggered=triggered),
|
|
311
311
|
span_id=span_id,
|
|
312
312
|
parent=parent,
|
|
@@ -344,7 +344,7 @@ def transcription_span(
|
|
|
344
344
|
Returns:
|
|
345
345
|
The newly created speech-to-text span.
|
|
346
346
|
"""
|
|
347
|
-
return
|
|
347
|
+
return get_trace_provider().create_span(
|
|
348
348
|
span_data=TranscriptionSpanData(
|
|
349
349
|
input=input,
|
|
350
350
|
input_format=input_format,
|
|
@@ -386,7 +386,7 @@ def speech_span(
|
|
|
386
386
|
trace/span as the parent.
|
|
387
387
|
disabled: If True, we will return a Span but the Span will not be recorded.
|
|
388
388
|
"""
|
|
389
|
-
return
|
|
389
|
+
return get_trace_provider().create_span(
|
|
390
390
|
span_data=SpeechSpanData(
|
|
391
391
|
model=model,
|
|
392
392
|
input=input,
|
|
@@ -419,7 +419,7 @@ def speech_group_span(
|
|
|
419
419
|
trace/span as the parent.
|
|
420
420
|
disabled: If True, we will return a Span but the Span will not be recorded.
|
|
421
421
|
"""
|
|
422
|
-
return
|
|
422
|
+
return get_trace_provider().create_span(
|
|
423
423
|
span_data=SpeechGroupSpanData(input=input),
|
|
424
424
|
span_id=span_id,
|
|
425
425
|
parent=parent,
|
|
@@ -447,7 +447,7 @@ def mcp_tools_span(
|
|
|
447
447
|
trace/span as the parent.
|
|
448
448
|
disabled: If True, we will return a Span but the Span will not be recorded.
|
|
449
449
|
"""
|
|
450
|
-
return
|
|
450
|
+
return get_trace_provider().create_span(
|
|
451
451
|
span_data=MCPListToolsSpanData(server=server, result=result),
|
|
452
452
|
span_id=span_id,
|
|
453
453
|
parent=parent,
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import threading
|
|
5
|
+
import uuid
|
|
6
|
+
from abc import ABC, abstractmethod
|
|
7
|
+
from datetime import datetime, timezone
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from ..logger import logger
|
|
11
|
+
from .processor_interface import TracingProcessor
|
|
12
|
+
from .scope import Scope
|
|
13
|
+
from .spans import NoOpSpan, Span, SpanImpl, TSpanData
|
|
14
|
+
from .traces import NoOpTrace, Trace, TraceImpl
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class SynchronousMultiTracingProcessor(TracingProcessor):
|
|
18
|
+
"""
|
|
19
|
+
Forwards all calls to a list of TracingProcessors, in order of registration.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __init__(self):
|
|
23
|
+
# Using a tuple to avoid race conditions when iterating over processors
|
|
24
|
+
self._processors: tuple[TracingProcessor, ...] = ()
|
|
25
|
+
self._lock = threading.Lock()
|
|
26
|
+
|
|
27
|
+
def add_tracing_processor(self, tracing_processor: TracingProcessor):
|
|
28
|
+
"""
|
|
29
|
+
Add a processor to the list of processors. Each processor will receive all traces/spans.
|
|
30
|
+
"""
|
|
31
|
+
with self._lock:
|
|
32
|
+
self._processors += (tracing_processor,)
|
|
33
|
+
|
|
34
|
+
def set_processors(self, processors: list[TracingProcessor]):
|
|
35
|
+
"""
|
|
36
|
+
Set the list of processors. This will replace the current list of processors.
|
|
37
|
+
"""
|
|
38
|
+
with self._lock:
|
|
39
|
+
self._processors = tuple(processors)
|
|
40
|
+
|
|
41
|
+
def on_trace_start(self, trace: Trace) -> None:
|
|
42
|
+
"""
|
|
43
|
+
Called when a trace is started.
|
|
44
|
+
"""
|
|
45
|
+
for processor in self._processors:
|
|
46
|
+
processor.on_trace_start(trace)
|
|
47
|
+
|
|
48
|
+
def on_trace_end(self, trace: Trace) -> None:
|
|
49
|
+
"""
|
|
50
|
+
Called when a trace is finished.
|
|
51
|
+
"""
|
|
52
|
+
for processor in self._processors:
|
|
53
|
+
processor.on_trace_end(trace)
|
|
54
|
+
|
|
55
|
+
def on_span_start(self, span: Span[Any]) -> None:
|
|
56
|
+
"""
|
|
57
|
+
Called when a span is started.
|
|
58
|
+
"""
|
|
59
|
+
for processor in self._processors:
|
|
60
|
+
processor.on_span_start(span)
|
|
61
|
+
|
|
62
|
+
def on_span_end(self, span: Span[Any]) -> None:
|
|
63
|
+
"""
|
|
64
|
+
Called when a span is finished.
|
|
65
|
+
"""
|
|
66
|
+
for processor in self._processors:
|
|
67
|
+
processor.on_span_end(span)
|
|
68
|
+
|
|
69
|
+
def shutdown(self) -> None:
|
|
70
|
+
"""
|
|
71
|
+
Called when the application stops.
|
|
72
|
+
"""
|
|
73
|
+
for processor in self._processors:
|
|
74
|
+
logger.debug(f"Shutting down trace processor {processor}")
|
|
75
|
+
processor.shutdown()
|
|
76
|
+
|
|
77
|
+
def force_flush(self):
|
|
78
|
+
"""
|
|
79
|
+
Force the processors to flush their buffers.
|
|
80
|
+
"""
|
|
81
|
+
for processor in self._processors:
|
|
82
|
+
processor.force_flush()
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class TraceProvider(ABC):
|
|
86
|
+
"""Interface for creating traces and spans."""
|
|
87
|
+
|
|
88
|
+
@abstractmethod
|
|
89
|
+
def register_processor(self, processor: TracingProcessor) -> None:
|
|
90
|
+
"""Add a processor that will receive all traces and spans."""
|
|
91
|
+
|
|
92
|
+
@abstractmethod
|
|
93
|
+
def set_processors(self, processors: list[TracingProcessor]) -> None:
|
|
94
|
+
"""Replace the list of processors with ``processors``."""
|
|
95
|
+
|
|
96
|
+
@abstractmethod
|
|
97
|
+
def get_current_trace(self) -> Trace | None:
|
|
98
|
+
"""Return the currently active trace, if any."""
|
|
99
|
+
|
|
100
|
+
@abstractmethod
|
|
101
|
+
def get_current_span(self) -> Span[Any] | None:
|
|
102
|
+
"""Return the currently active span, if any."""
|
|
103
|
+
|
|
104
|
+
@abstractmethod
|
|
105
|
+
def set_disabled(self, disabled: bool) -> None:
|
|
106
|
+
"""Enable or disable tracing globally."""
|
|
107
|
+
|
|
108
|
+
@abstractmethod
|
|
109
|
+
def time_iso(self) -> str:
|
|
110
|
+
"""Return the current time in ISO 8601 format."""
|
|
111
|
+
|
|
112
|
+
@abstractmethod
|
|
113
|
+
def gen_trace_id(self) -> str:
|
|
114
|
+
"""Generate a new trace identifier."""
|
|
115
|
+
|
|
116
|
+
@abstractmethod
|
|
117
|
+
def gen_span_id(self) -> str:
|
|
118
|
+
"""Generate a new span identifier."""
|
|
119
|
+
|
|
120
|
+
@abstractmethod
|
|
121
|
+
def gen_group_id(self) -> str:
|
|
122
|
+
"""Generate a new group identifier."""
|
|
123
|
+
|
|
124
|
+
@abstractmethod
|
|
125
|
+
def create_trace(
|
|
126
|
+
self,
|
|
127
|
+
name: str,
|
|
128
|
+
trace_id: str | None = None,
|
|
129
|
+
group_id: str | None = None,
|
|
130
|
+
metadata: dict[str, Any] | None = None,
|
|
131
|
+
disabled: bool = False,
|
|
132
|
+
) -> Trace:
|
|
133
|
+
"""Create a new trace."""
|
|
134
|
+
|
|
135
|
+
@abstractmethod
|
|
136
|
+
def create_span(
|
|
137
|
+
self,
|
|
138
|
+
span_data: TSpanData,
|
|
139
|
+
span_id: str | None = None,
|
|
140
|
+
parent: Trace | Span[Any] | None = None,
|
|
141
|
+
disabled: bool = False,
|
|
142
|
+
) -> Span[TSpanData]:
|
|
143
|
+
"""Create a new span."""
|
|
144
|
+
|
|
145
|
+
@abstractmethod
|
|
146
|
+
def shutdown(self) -> None:
|
|
147
|
+
"""Clean up any resources used by the provider."""
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
class DefaultTraceProvider(TraceProvider):
|
|
151
|
+
def __init__(self) -> None:
|
|
152
|
+
self._multi_processor = SynchronousMultiTracingProcessor()
|
|
153
|
+
self._disabled = os.environ.get("OPENAI_AGENTS_DISABLE_TRACING", "false").lower() in (
|
|
154
|
+
"true",
|
|
155
|
+
"1",
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
def register_processor(self, processor: TracingProcessor):
|
|
159
|
+
"""
|
|
160
|
+
Add a processor to the list of processors. Each processor will receive all traces/spans.
|
|
161
|
+
"""
|
|
162
|
+
self._multi_processor.add_tracing_processor(processor)
|
|
163
|
+
|
|
164
|
+
def set_processors(self, processors: list[TracingProcessor]):
|
|
165
|
+
"""
|
|
166
|
+
Set the list of processors. This will replace the current list of processors.
|
|
167
|
+
"""
|
|
168
|
+
self._multi_processor.set_processors(processors)
|
|
169
|
+
|
|
170
|
+
def get_current_trace(self) -> Trace | None:
|
|
171
|
+
"""
|
|
172
|
+
Returns the currently active trace, if any.
|
|
173
|
+
"""
|
|
174
|
+
return Scope.get_current_trace()
|
|
175
|
+
|
|
176
|
+
def get_current_span(self) -> Span[Any] | None:
|
|
177
|
+
"""
|
|
178
|
+
Returns the currently active span, if any.
|
|
179
|
+
"""
|
|
180
|
+
return Scope.get_current_span()
|
|
181
|
+
|
|
182
|
+
def set_disabled(self, disabled: bool) -> None:
|
|
183
|
+
"""
|
|
184
|
+
Set whether tracing is disabled.
|
|
185
|
+
"""
|
|
186
|
+
self._disabled = disabled
|
|
187
|
+
|
|
188
|
+
def time_iso(self) -> str:
|
|
189
|
+
"""Return the current time in ISO 8601 format."""
|
|
190
|
+
return datetime.now(timezone.utc).isoformat()
|
|
191
|
+
|
|
192
|
+
def gen_trace_id(self) -> str:
|
|
193
|
+
"""Generate a new trace ID."""
|
|
194
|
+
return f"trace_{uuid.uuid4().hex}"
|
|
195
|
+
|
|
196
|
+
def gen_span_id(self) -> str:
|
|
197
|
+
"""Generate a new span ID."""
|
|
198
|
+
return f"span_{uuid.uuid4().hex[:24]}"
|
|
199
|
+
|
|
200
|
+
def gen_group_id(self) -> str:
|
|
201
|
+
"""Generate a new group ID."""
|
|
202
|
+
return f"group_{uuid.uuid4().hex[:24]}"
|
|
203
|
+
|
|
204
|
+
def create_trace(
|
|
205
|
+
self,
|
|
206
|
+
name: str,
|
|
207
|
+
trace_id: str | None = None,
|
|
208
|
+
group_id: str | None = None,
|
|
209
|
+
metadata: dict[str, Any] | None = None,
|
|
210
|
+
disabled: bool = False,
|
|
211
|
+
) -> Trace:
|
|
212
|
+
"""
|
|
213
|
+
Create a new trace.
|
|
214
|
+
"""
|
|
215
|
+
if self._disabled or disabled:
|
|
216
|
+
logger.debug(f"Tracing is disabled. Not creating trace {name}")
|
|
217
|
+
return NoOpTrace()
|
|
218
|
+
|
|
219
|
+
trace_id = trace_id or self.gen_trace_id()
|
|
220
|
+
|
|
221
|
+
logger.debug(f"Creating trace {name} with id {trace_id}")
|
|
222
|
+
|
|
223
|
+
return TraceImpl(
|
|
224
|
+
name=name,
|
|
225
|
+
trace_id=trace_id,
|
|
226
|
+
group_id=group_id,
|
|
227
|
+
metadata=metadata,
|
|
228
|
+
processor=self._multi_processor,
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
def create_span(
|
|
232
|
+
self,
|
|
233
|
+
span_data: TSpanData,
|
|
234
|
+
span_id: str | None = None,
|
|
235
|
+
parent: Trace | Span[Any] | None = None,
|
|
236
|
+
disabled: bool = False,
|
|
237
|
+
) -> Span[TSpanData]:
|
|
238
|
+
"""
|
|
239
|
+
Create a new span.
|
|
240
|
+
"""
|
|
241
|
+
if self._disabled or disabled:
|
|
242
|
+
logger.debug(f"Tracing is disabled. Not creating span {span_data}")
|
|
243
|
+
return NoOpSpan(span_data)
|
|
244
|
+
|
|
245
|
+
if not parent:
|
|
246
|
+
current_span = Scope.get_current_span()
|
|
247
|
+
current_trace = Scope.get_current_trace()
|
|
248
|
+
if current_trace is None:
|
|
249
|
+
logger.error(
|
|
250
|
+
"No active trace. Make sure to start a trace with `trace()` first"
|
|
251
|
+
"Returning NoOpSpan."
|
|
252
|
+
)
|
|
253
|
+
return NoOpSpan(span_data)
|
|
254
|
+
elif isinstance(current_trace, NoOpTrace) or isinstance(current_span, NoOpSpan):
|
|
255
|
+
logger.debug(
|
|
256
|
+
f"Parent {current_span} or {current_trace} is no-op, returning NoOpSpan"
|
|
257
|
+
)
|
|
258
|
+
return NoOpSpan(span_data)
|
|
259
|
+
|
|
260
|
+
parent_id = current_span.span_id if current_span else None
|
|
261
|
+
trace_id = current_trace.trace_id
|
|
262
|
+
|
|
263
|
+
elif isinstance(parent, Trace):
|
|
264
|
+
if isinstance(parent, NoOpTrace):
|
|
265
|
+
logger.debug(f"Parent {parent} is no-op, returning NoOpSpan")
|
|
266
|
+
return NoOpSpan(span_data)
|
|
267
|
+
trace_id = parent.trace_id
|
|
268
|
+
parent_id = None
|
|
269
|
+
elif isinstance(parent, Span):
|
|
270
|
+
if isinstance(parent, NoOpSpan):
|
|
271
|
+
logger.debug(f"Parent {parent} is no-op, returning NoOpSpan")
|
|
272
|
+
return NoOpSpan(span_data)
|
|
273
|
+
parent_id = parent.span_id
|
|
274
|
+
trace_id = parent.trace_id
|
|
275
|
+
|
|
276
|
+
logger.debug(f"Creating span {span_data} with id {span_id}")
|
|
277
|
+
|
|
278
|
+
return SpanImpl(
|
|
279
|
+
trace_id=trace_id,
|
|
280
|
+
span_id=span_id or self.gen_span_id(),
|
|
281
|
+
parent_id=parent_id,
|
|
282
|
+
processor=self._multi_processor,
|
|
283
|
+
span_data=span_data,
|
|
284
|
+
)
|
|
285
|
+
|
|
286
|
+
def shutdown(self) -> None:
|
|
287
|
+
if self._disabled:
|
|
288
|
+
return
|
|
289
|
+
|
|
290
|
+
try:
|
|
291
|
+
logger.debug("Shutting down trace provider")
|
|
292
|
+
self._multi_processor.shutdown()
|
|
293
|
+
except Exception as e:
|
|
294
|
+
logger.error(f"Error shutting down trace provider: {e}")
|
agents/tracing/setup.py
CHANGED
|
@@ -1,214 +1,21 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import threading
|
|
5
|
-
from typing import Any
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
from . import
|
|
9
|
-
from .processor_interface import TracingProcessor
|
|
10
|
-
from .scope import Scope
|
|
11
|
-
from .spans import NoOpSpan, Span, SpanImpl, TSpanData
|
|
12
|
-
from .traces import NoOpTrace, Trace, TraceImpl
|
|
5
|
+
if TYPE_CHECKING:
|
|
6
|
+
from .provider import TraceProvider
|
|
13
7
|
|
|
8
|
+
GLOBAL_TRACE_PROVIDER: TraceProvider | None = None
|
|
14
9
|
|
|
15
|
-
class SynchronousMultiTracingProcessor(TracingProcessor):
|
|
16
|
-
"""
|
|
17
|
-
Forwards all calls to a list of TracingProcessors, in order of registration.
|
|
18
|
-
"""
|
|
19
10
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
11
|
+
def set_trace_provider(provider: TraceProvider) -> None:
|
|
12
|
+
"""Set the global trace provider used by tracing utilities."""
|
|
13
|
+
global GLOBAL_TRACE_PROVIDER
|
|
14
|
+
GLOBAL_TRACE_PROVIDER = provider
|
|
24
15
|
|
|
25
|
-
def add_tracing_processor(self, tracing_processor: TracingProcessor):
|
|
26
|
-
"""
|
|
27
|
-
Add a processor to the list of processors. Each processor will receive all traces/spans.
|
|
28
|
-
"""
|
|
29
|
-
with self._lock:
|
|
30
|
-
self._processors += (tracing_processor,)
|
|
31
16
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
""
|
|
36
|
-
|
|
37
|
-
self._processors = tuple(processors)
|
|
38
|
-
|
|
39
|
-
def on_trace_start(self, trace: Trace) -> None:
|
|
40
|
-
"""
|
|
41
|
-
Called when a trace is started.
|
|
42
|
-
"""
|
|
43
|
-
for processor in self._processors:
|
|
44
|
-
processor.on_trace_start(trace)
|
|
45
|
-
|
|
46
|
-
def on_trace_end(self, trace: Trace) -> None:
|
|
47
|
-
"""
|
|
48
|
-
Called when a trace is finished.
|
|
49
|
-
"""
|
|
50
|
-
for processor in self._processors:
|
|
51
|
-
processor.on_trace_end(trace)
|
|
52
|
-
|
|
53
|
-
def on_span_start(self, span: Span[Any]) -> None:
|
|
54
|
-
"""
|
|
55
|
-
Called when a span is started.
|
|
56
|
-
"""
|
|
57
|
-
for processor in self._processors:
|
|
58
|
-
processor.on_span_start(span)
|
|
59
|
-
|
|
60
|
-
def on_span_end(self, span: Span[Any]) -> None:
|
|
61
|
-
"""
|
|
62
|
-
Called when a span is finished.
|
|
63
|
-
"""
|
|
64
|
-
for processor in self._processors:
|
|
65
|
-
processor.on_span_end(span)
|
|
66
|
-
|
|
67
|
-
def shutdown(self) -> None:
|
|
68
|
-
"""
|
|
69
|
-
Called when the application stops.
|
|
70
|
-
"""
|
|
71
|
-
for processor in self._processors:
|
|
72
|
-
logger.debug(f"Shutting down trace processor {processor}")
|
|
73
|
-
processor.shutdown()
|
|
74
|
-
|
|
75
|
-
def force_flush(self):
|
|
76
|
-
"""
|
|
77
|
-
Force the processors to flush their buffers.
|
|
78
|
-
"""
|
|
79
|
-
for processor in self._processors:
|
|
80
|
-
processor.force_flush()
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
class TraceProvider:
|
|
84
|
-
def __init__(self):
|
|
85
|
-
self._multi_processor = SynchronousMultiTracingProcessor()
|
|
86
|
-
self._disabled = os.environ.get("OPENAI_AGENTS_DISABLE_TRACING", "false").lower() in (
|
|
87
|
-
"true",
|
|
88
|
-
"1",
|
|
89
|
-
)
|
|
90
|
-
|
|
91
|
-
def register_processor(self, processor: TracingProcessor):
|
|
92
|
-
"""
|
|
93
|
-
Add a processor to the list of processors. Each processor will receive all traces/spans.
|
|
94
|
-
"""
|
|
95
|
-
self._multi_processor.add_tracing_processor(processor)
|
|
96
|
-
|
|
97
|
-
def set_processors(self, processors: list[TracingProcessor]):
|
|
98
|
-
"""
|
|
99
|
-
Set the list of processors. This will replace the current list of processors.
|
|
100
|
-
"""
|
|
101
|
-
self._multi_processor.set_processors(processors)
|
|
102
|
-
|
|
103
|
-
def get_current_trace(self) -> Trace | None:
|
|
104
|
-
"""
|
|
105
|
-
Returns the currently active trace, if any.
|
|
106
|
-
"""
|
|
107
|
-
return Scope.get_current_trace()
|
|
108
|
-
|
|
109
|
-
def get_current_span(self) -> Span[Any] | None:
|
|
110
|
-
"""
|
|
111
|
-
Returns the currently active span, if any.
|
|
112
|
-
"""
|
|
113
|
-
return Scope.get_current_span()
|
|
114
|
-
|
|
115
|
-
def set_disabled(self, disabled: bool) -> None:
|
|
116
|
-
"""
|
|
117
|
-
Set whether tracing is disabled.
|
|
118
|
-
"""
|
|
119
|
-
self._disabled = disabled
|
|
120
|
-
|
|
121
|
-
def create_trace(
|
|
122
|
-
self,
|
|
123
|
-
name: str,
|
|
124
|
-
trace_id: str | None = None,
|
|
125
|
-
group_id: str | None = None,
|
|
126
|
-
metadata: dict[str, Any] | None = None,
|
|
127
|
-
disabled: bool = False,
|
|
128
|
-
) -> Trace:
|
|
129
|
-
"""
|
|
130
|
-
Create a new trace.
|
|
131
|
-
"""
|
|
132
|
-
if self._disabled or disabled:
|
|
133
|
-
logger.debug(f"Tracing is disabled. Not creating trace {name}")
|
|
134
|
-
return NoOpTrace()
|
|
135
|
-
|
|
136
|
-
trace_id = trace_id or util.gen_trace_id()
|
|
137
|
-
|
|
138
|
-
logger.debug(f"Creating trace {name} with id {trace_id}")
|
|
139
|
-
|
|
140
|
-
return TraceImpl(
|
|
141
|
-
name=name,
|
|
142
|
-
trace_id=trace_id,
|
|
143
|
-
group_id=group_id,
|
|
144
|
-
metadata=metadata,
|
|
145
|
-
processor=self._multi_processor,
|
|
146
|
-
)
|
|
147
|
-
|
|
148
|
-
def create_span(
|
|
149
|
-
self,
|
|
150
|
-
span_data: TSpanData,
|
|
151
|
-
span_id: str | None = None,
|
|
152
|
-
parent: Trace | Span[Any] | None = None,
|
|
153
|
-
disabled: bool = False,
|
|
154
|
-
) -> Span[TSpanData]:
|
|
155
|
-
"""
|
|
156
|
-
Create a new span.
|
|
157
|
-
"""
|
|
158
|
-
if self._disabled or disabled:
|
|
159
|
-
logger.debug(f"Tracing is disabled. Not creating span {span_data}")
|
|
160
|
-
return NoOpSpan(span_data)
|
|
161
|
-
|
|
162
|
-
if not parent:
|
|
163
|
-
current_span = Scope.get_current_span()
|
|
164
|
-
current_trace = Scope.get_current_trace()
|
|
165
|
-
if current_trace is None:
|
|
166
|
-
logger.error(
|
|
167
|
-
"No active trace. Make sure to start a trace with `trace()` first"
|
|
168
|
-
"Returning NoOpSpan."
|
|
169
|
-
)
|
|
170
|
-
return NoOpSpan(span_data)
|
|
171
|
-
elif isinstance(current_trace, NoOpTrace) or isinstance(current_span, NoOpSpan):
|
|
172
|
-
logger.debug(
|
|
173
|
-
f"Parent {current_span} or {current_trace} is no-op, returning NoOpSpan"
|
|
174
|
-
)
|
|
175
|
-
return NoOpSpan(span_data)
|
|
176
|
-
|
|
177
|
-
parent_id = current_span.span_id if current_span else None
|
|
178
|
-
trace_id = current_trace.trace_id
|
|
179
|
-
|
|
180
|
-
elif isinstance(parent, Trace):
|
|
181
|
-
if isinstance(parent, NoOpTrace):
|
|
182
|
-
logger.debug(f"Parent {parent} is no-op, returning NoOpSpan")
|
|
183
|
-
return NoOpSpan(span_data)
|
|
184
|
-
trace_id = parent.trace_id
|
|
185
|
-
parent_id = None
|
|
186
|
-
elif isinstance(parent, Span):
|
|
187
|
-
if isinstance(parent, NoOpSpan):
|
|
188
|
-
logger.debug(f"Parent {parent} is no-op, returning NoOpSpan")
|
|
189
|
-
return NoOpSpan(span_data)
|
|
190
|
-
parent_id = parent.span_id
|
|
191
|
-
trace_id = parent.trace_id
|
|
192
|
-
|
|
193
|
-
logger.debug(f"Creating span {span_data} with id {span_id}")
|
|
194
|
-
|
|
195
|
-
return SpanImpl(
|
|
196
|
-
trace_id=trace_id,
|
|
197
|
-
span_id=span_id,
|
|
198
|
-
parent_id=parent_id,
|
|
199
|
-
processor=self._multi_processor,
|
|
200
|
-
span_data=span_data,
|
|
201
|
-
)
|
|
202
|
-
|
|
203
|
-
def shutdown(self) -> None:
|
|
204
|
-
if self._disabled:
|
|
205
|
-
return
|
|
206
|
-
|
|
207
|
-
try:
|
|
208
|
-
logger.debug("Shutting down trace provider")
|
|
209
|
-
self._multi_processor.shutdown()
|
|
210
|
-
except Exception as e:
|
|
211
|
-
logger.error(f"Error shutting down trace provider: {e}")
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
GLOBAL_TRACE_PROVIDER = TraceProvider()
|
|
17
|
+
def get_trace_provider() -> TraceProvider:
|
|
18
|
+
"""Get the global trace provider used by tracing utilities."""
|
|
19
|
+
if GLOBAL_TRACE_PROVIDER is None:
|
|
20
|
+
raise RuntimeError("Trace provider not set")
|
|
21
|
+
return GLOBAL_TRACE_PROVIDER
|
agents/tracing/util.py
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
|
-
import
|
|
2
|
-
from datetime import datetime, timezone
|
|
1
|
+
from .setup import get_trace_provider
|
|
3
2
|
|
|
4
3
|
|
|
5
4
|
def time_iso() -> str:
|
|
6
|
-
"""
|
|
7
|
-
return
|
|
5
|
+
"""Return the current time in ISO 8601 format."""
|
|
6
|
+
return get_trace_provider().time_iso()
|
|
8
7
|
|
|
9
8
|
|
|
10
9
|
def gen_trace_id() -> str:
|
|
11
|
-
"""
|
|
12
|
-
return
|
|
10
|
+
"""Generate a new trace ID."""
|
|
11
|
+
return get_trace_provider().gen_trace_id()
|
|
13
12
|
|
|
14
13
|
|
|
15
14
|
def gen_span_id() -> str:
|
|
16
|
-
"""
|
|
17
|
-
return
|
|
15
|
+
"""Generate a new span ID."""
|
|
16
|
+
return get_trace_provider().gen_span_id()
|
|
18
17
|
|
|
19
18
|
|
|
20
19
|
def gen_group_id() -> str:
|
|
21
|
-
"""
|
|
22
|
-
return
|
|
20
|
+
"""Generate a new group ID."""
|
|
21
|
+
return get_trace_provider().gen_group_id()
|