Skip to content

Commit b8e064f

Browse files
authored
Add _run_config attribute to trace classes
1 parent 9841c5e commit b8e064f

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

src/agents/tracing/traces.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import abc
44
import contextvars
5-
from typing import Any
5+
from typing import TYPE_CHECKING, Any
66

77
from ..logger import logger
88
from . import util
99
from .processor_interface import TracingProcessor
1010
from .scope import Scope
1111

12+
if TYPE_CHECKING:
13+
from ..run import RunConfig
1214

1315
class Trace:
1416
"""
@@ -66,7 +68,6 @@ def export(self) -> dict[str, Any] | None:
6668
"""
6769
pass
6870

69-
7071
class NoOpTrace(Trace):
7172
"""
7273
A no-op trace that will not be recorded.
@@ -75,16 +76,15 @@ class NoOpTrace(Trace):
7576
def __init__(self):
7677
self._started = False
7778
self._prev_context_token: contextvars.Token[Trace | None] | None = None
79+
self._run_config: RunConfig | None = None
7880

7981
def __enter__(self) -> Trace:
8082
if self._started:
8183
if not self._prev_context_token:
8284
logger.error("Trace already started but no context token set")
8385
return self
84-
8586
self._started = True
8687
self.start(mark_as_current=True)
87-
8888
return self
8989

9090
def __exit__(self, exc_type, exc_val, exc_tb):
@@ -97,7 +97,7 @@ def start(self, mark_as_current: bool = False):
9797
def finish(self, reset_current: bool = False):
9898
if reset_current and self._prev_context_token is not None:
9999
Scope.reset_current_trace(self._prev_context_token)
100-
self._prev_context_token = None
100+
self._prev_context_token = None
101101

102102
@property
103103
def trace_id(self) -> str:
@@ -110,10 +110,8 @@ def name(self) -> str:
110110
def export(self) -> dict[str, Any] | None:
111111
return None
112112

113-
114113
NO_OP_TRACE = NoOpTrace()
115114

116-
117115
class TraceImpl(Trace):
118116
"""
119117
A trace that will be recorded by the tracing library.
@@ -127,6 +125,7 @@ class TraceImpl(Trace):
127125
"_prev_context_token",
128126
"_processor",
129127
"_started",
128+
"_run_config",
130129
)
131130

132131
def __init__(
@@ -144,6 +143,7 @@ def __init__(
144143
self._prev_context_token: contextvars.Token[Trace | None] | None = None
145144
self._processor = processor
146145
self._started = False
146+
self._run_config: RunConfig | None = None
147147

148148
@property
149149
def trace_id(self) -> str:
@@ -156,29 +156,24 @@ def name(self) -> str:
156156
def start(self, mark_as_current: bool = False):
157157
if self._started:
158158
return
159-
160159
self._started = True
161160
self._processor.on_trace_start(self)
162-
163161
if mark_as_current:
164162
self._prev_context_token = Scope.set_current_trace(self)
165163

166164
def finish(self, reset_current: bool = False):
167165
if not self._started:
168166
return
169-
170167
self._processor.on_trace_end(self)
171-
172168
if reset_current and self._prev_context_token is not None:
173169
Scope.reset_current_trace(self._prev_context_token)
174-
self._prev_context_token = None
170+
self._prev_context_token = None
175171

176172
def __enter__(self) -> Trace:
177173
if self._started:
178174
if not self._prev_context_token:
179175
logger.error("Trace already started but no context token set")
180176
return self
181-
182177
self.start(mark_as_current=True)
183178
return self
184179

0 commit comments

Comments
 (0)