@@ -51,11 +51,9 @@ def __init__(self, execution_id=None, span_id=None):
51
51
52
52
53
53
def _get_current_context ():
54
- # First try to get from async context
55
54
context = execution_context_var .get ()
56
55
if context is not None :
57
56
return context
58
- # Fall back to Flask context for sync
59
57
return ( # pragma: no cover
60
58
flask .g .execution_id_context
61
59
if flask .has_request_context () and "execution_id_context" in flask .g
@@ -64,8 +62,6 @@ def _get_current_context():
64
62
65
63
66
64
def _set_current_context (context ):
67
- # Set in both contexts to support both sync and async
68
- # Set in contextvars for async
69
65
execution_context_var .set (context )
70
66
# Also set in Flask context if available for sync
71
67
if flask .has_request_context ():
@@ -82,8 +78,7 @@ def _generate_execution_id():
82
78
def _extract_context_from_headers (headers ):
83
79
"""Extract execution context from request headers."""
84
80
execution_id = headers .get (EXECUTION_ID_REQUEST_HEADER )
85
-
86
- # Try to get span ID from trace context header
81
+
87
82
trace_context = re .match (
88
83
_TRACE_CONTEXT_REGEX_PATTERN ,
89
84
headers .get (TRACE_CONTEXT_REQUEST_HEADER , "" ),
@@ -113,7 +108,6 @@ def __init__(self, app):
113
108
114
109
async def __call__ (self , scope , receive , send ):
115
110
if scope ["type" ] == "http" :
116
- # Extract existing execution ID or generate a new one
117
111
execution_id_header = b"function-execution-id"
118
112
trace_context_header = b"x-cloud-trace-context"
119
113
execution_id = None
@@ -127,22 +121,18 @@ async def __call__(self, scope, receive, send):
127
121
128
122
if not execution_id :
129
123
execution_id = _generate_execution_id ()
130
- # Add the execution ID to headers
131
124
new_headers = list (scope .get ("headers" , []))
132
125
new_headers .append (
133
126
(execution_id_header , execution_id .encode ("latin-1" ))
134
127
)
135
128
scope ["headers" ] = new_headers
136
129
137
- # Store execution context in ASGI scope for recovery in case of context loss
138
- # Parse trace context to extract span ID
139
130
span_id = None
140
131
if trace_context :
141
132
trace_match = re .match (_TRACE_CONTEXT_REGEX_PATTERN , trace_context )
142
133
if trace_match :
143
134
span_id = trace_match .group ("span_id" )
144
135
145
- # Store in scope for potential recovery
146
136
scope ["execution_context" ] = ExecutionContext (execution_id , span_id )
147
137
148
138
await self .app (scope , receive , send ) # pragma: no cover
@@ -169,9 +159,7 @@ def wrapper(*args, **kwargs):
169
159
170
160
with stderr_redirect , stdout_redirect :
171
161
result = view_function (* args , ** kwargs )
172
-
173
162
# Context cleanup happens automatically via Flask's request context
174
- # No need to manually clean up flask.g
175
163
return result
176
164
177
165
return wrapper
@@ -195,14 +183,10 @@ def set_execution_context_async(enable_id_logging=False):
195
183
def decorator (view_function ):
196
184
@functools .wraps (view_function )
197
185
async def async_wrapper (request , * args , ** kwargs ):
198
- # Extract execution context from headers
199
186
context = _extract_context_from_headers (request .headers )
200
-
201
- # Set context using contextvars
202
187
token = execution_context_var .set (context )
203
188
204
189
with stderr_redirect , stdout_redirect :
205
- # Handle both sync and async functions
206
190
if inspect .iscoroutinefunction (view_function ):
207
191
result = await view_function (request , * args , ** kwargs )
208
192
else :
@@ -215,10 +199,7 @@ async def async_wrapper(request, *args, **kwargs):
215
199
216
200
@functools .wraps (view_function )
217
201
def sync_wrapper (request , * args , ** kwargs ): # pragma: no cover
218
- # For sync functions, we still need to set up the context
219
202
context = _extract_context_from_headers (request .headers )
220
-
221
- # Set context using contextvars
222
203
token = execution_context_var .set (context )
223
204
224
205
with stderr_redirect , stdout_redirect :
@@ -229,7 +210,6 @@ def sync_wrapper(request, *args, **kwargs): # pragma: no cover
229
210
execution_context_var .reset (token )
230
211
return result
231
212
232
- # Return appropriate wrapper based on whether the function is async
233
213
if inspect .iscoroutinefunction (view_function ):
234
214
return async_wrapper
235
215
else :
0 commit comments