44
55from .config import TracingConfig
66from .create import get_current_trace , trace
7- from .traces import Trace
7+ from .traces import Trace , TraceState , _trace_id_was_started , reattach_trace
8+
9+
10+ def _trace_state_matches_effective_settings (
11+ * ,
12+ trace_state : TraceState ,
13+ workflow_name : str ,
14+ trace_id : str | None ,
15+ group_id : str | None ,
16+ metadata : dict [str , Any ] | None ,
17+ tracing : TracingConfig | None ,
18+ ) -> bool :
19+ if trace_state .trace_id is None or trace_state .trace_id != trace_id :
20+ return False
21+ if trace_state .workflow_name != workflow_name :
22+ return False
23+ if trace_state .group_id != group_id :
24+ return False
25+ if trace_state .metadata != metadata :
26+ return False
27+ tracing_api_key = tracing .get ("api_key" ) if tracing is not None else None
28+ return trace_state .tracing_api_key == tracing_api_key
29+
30+
31+ def create_trace_for_run (
32+ * ,
33+ workflow_name : str ,
34+ trace_id : str | None ,
35+ group_id : str | None ,
36+ metadata : dict [str , Any ] | None ,
37+ tracing : TracingConfig | None ,
38+ disabled : bool ,
39+ trace_state : TraceState | None = None ,
40+ reattach_resumed_trace : bool = False ,
41+ ) -> Trace | None :
42+ """Return a trace object for this run when one is not already active."""
43+ current_trace = get_current_trace ()
44+ if current_trace :
45+ return None
46+
47+ if (
48+ reattach_resumed_trace
49+ and not disabled
50+ and trace_state is not None
51+ and _trace_state_matches_effective_settings (
52+ trace_state = trace_state ,
53+ workflow_name = workflow_name ,
54+ trace_id = trace_id ,
55+ group_id = group_id ,
56+ metadata = metadata ,
57+ tracing = tracing ,
58+ )
59+ and _trace_id_was_started (trace_state .trace_id )
60+ ):
61+ return reattach_trace (trace_state )
62+
63+ return trace (
64+ workflow_name = workflow_name ,
65+ trace_id = trace_id ,
66+ group_id = group_id ,
67+ metadata = metadata ,
68+ tracing = tracing ,
69+ disabled = disabled ,
70+ )
871
972
1073class TraceCtxManager :
@@ -18,6 +81,8 @@ def __init__(
1881 metadata : dict [str , Any ] | None ,
1982 tracing : TracingConfig | None ,
2083 disabled : bool ,
84+ trace_state : TraceState | None = None ,
85+ reattach_resumed_trace : bool = False ,
2186 ):
2287 self .trace : Trace | None = None
2388 self .workflow_name = workflow_name
@@ -26,18 +91,21 @@ def __init__(
2691 self .metadata = metadata
2792 self .tracing = tracing
2893 self .disabled = disabled
94+ self .trace_state = trace_state
95+ self .reattach_resumed_trace = reattach_resumed_trace
2996
3097 def __enter__ (self ) -> TraceCtxManager :
31- current_trace = get_current_trace ()
32- if not current_trace :
33- self .trace = trace (
34- workflow_name = self .workflow_name ,
35- trace_id = self .trace_id ,
36- group_id = self .group_id ,
37- metadata = self .metadata ,
38- tracing = self .tracing ,
39- disabled = self .disabled ,
40- )
98+ self .trace = create_trace_for_run (
99+ workflow_name = self .workflow_name ,
100+ trace_id = self .trace_id ,
101+ group_id = self .group_id ,
102+ metadata = self .metadata ,
103+ tracing = self .tracing ,
104+ disabled = self .disabled ,
105+ trace_state = self .trace_state ,
106+ reattach_resumed_trace = self .reattach_resumed_trace ,
107+ )
108+ if self .trace :
41109 assert self .trace is not None
42110 self .trace .start (mark_as_current = True )
43111 return self
0 commit comments