2
2
3
3
import abc
4
4
import contextvars
5
- from typing import Any
5
+ from typing import TYPE_CHECKING , Any
6
6
7
7
from ..logger import logger
8
8
from . import util
9
9
from .processor_interface import TracingProcessor
10
10
from .scope import Scope
11
11
12
+ if TYPE_CHECKING :
13
+ from ..run import RunConfig
12
14
13
15
class Trace :
14
16
"""
@@ -66,7 +68,6 @@ def export(self) -> dict[str, Any] | None:
66
68
"""
67
69
pass
68
70
69
-
70
71
class NoOpTrace (Trace ):
71
72
"""
72
73
A no-op trace that will not be recorded.
@@ -75,16 +76,15 @@ class NoOpTrace(Trace):
75
76
def __init__ (self ):
76
77
self ._started = False
77
78
self ._prev_context_token : contextvars .Token [Trace | None ] | None = None
79
+ self ._run_config : RunConfig | None = None
78
80
79
81
def __enter__ (self ) -> Trace :
80
82
if self ._started :
81
83
if not self ._prev_context_token :
82
84
logger .error ("Trace already started but no context token set" )
83
85
return self
84
-
85
86
self ._started = True
86
87
self .start (mark_as_current = True )
87
-
88
88
return self
89
89
90
90
def __exit__ (self , exc_type , exc_val , exc_tb ):
@@ -97,7 +97,7 @@ def start(self, mark_as_current: bool = False):
97
97
def finish (self , reset_current : bool = False ):
98
98
if reset_current and self ._prev_context_token is not None :
99
99
Scope .reset_current_trace (self ._prev_context_token )
100
- self ._prev_context_token = None
100
+ self ._prev_context_token = None
101
101
102
102
@property
103
103
def trace_id (self ) -> str :
@@ -110,10 +110,8 @@ def name(self) -> str:
110
110
def export (self ) -> dict [str , Any ] | None :
111
111
return None
112
112
113
-
114
113
NO_OP_TRACE = NoOpTrace ()
115
114
116
-
117
115
class TraceImpl (Trace ):
118
116
"""
119
117
A trace that will be recorded by the tracing library.
@@ -127,6 +125,7 @@ class TraceImpl(Trace):
127
125
"_prev_context_token" ,
128
126
"_processor" ,
129
127
"_started" ,
128
+ "_run_config" ,
130
129
)
131
130
132
131
def __init__ (
@@ -144,6 +143,7 @@ def __init__(
144
143
self ._prev_context_token : contextvars .Token [Trace | None ] | None = None
145
144
self ._processor = processor
146
145
self ._started = False
146
+ self ._run_config : RunConfig | None = None
147
147
148
148
@property
149
149
def trace_id (self ) -> str :
@@ -156,29 +156,24 @@ def name(self) -> str:
156
156
def start (self , mark_as_current : bool = False ):
157
157
if self ._started :
158
158
return
159
-
160
159
self ._started = True
161
160
self ._processor .on_trace_start (self )
162
-
163
161
if mark_as_current :
164
162
self ._prev_context_token = Scope .set_current_trace (self )
165
163
166
164
def finish (self , reset_current : bool = False ):
167
165
if not self ._started :
168
166
return
169
-
170
167
self ._processor .on_trace_end (self )
171
-
172
168
if reset_current and self ._prev_context_token is not None :
173
169
Scope .reset_current_trace (self ._prev_context_token )
174
- self ._prev_context_token = None
170
+ self ._prev_context_token = None
175
171
176
172
def __enter__ (self ) -> Trace :
177
173
if self ._started :
178
174
if not self ._prev_context_token :
179
175
logger .error ("Trace already started but no context token set" )
180
176
return self
181
-
182
177
self .start (mark_as_current = True )
183
178
return self
184
179
0 commit comments