@@ -51,11 +51,9 @@ def __init__(self, execution_id=None, span_id=None):
5151
5252
5353def _get_current_context ():
54- # First try to get from async context
5554 context = execution_context_var .get ()
5655 if context is not None :
5756 return context
58- # Fall back to Flask context for sync
5957 return ( # pragma: no cover
6058 flask .g .execution_id_context
6159 if flask .has_request_context () and "execution_id_context" in flask .g
@@ -64,8 +62,6 @@ def _get_current_context():
6462
6563
6664def _set_current_context (context ):
67- # Set in both contexts to support both sync and async
68- # Set in contextvars for async
6965 execution_context_var .set (context )
7066 # Also set in Flask context if available for sync
7167 if flask .has_request_context ():
@@ -82,8 +78,7 @@ def _generate_execution_id():
8278def _extract_context_from_headers (headers ):
8379 """Extract execution context from request headers."""
8480 execution_id = headers .get (EXECUTION_ID_REQUEST_HEADER )
85-
86- # Try to get span ID from trace context header
81+
8782 trace_context = re .match (
8883 _TRACE_CONTEXT_REGEX_PATTERN ,
8984 headers .get (TRACE_CONTEXT_REQUEST_HEADER , "" ),
@@ -113,7 +108,6 @@ def __init__(self, app):
113108
114109 async def __call__ (self , scope , receive , send ):
115110 if scope ["type" ] == "http" :
116- # Extract existing execution ID or generate a new one
117111 execution_id_header = b"function-execution-id"
118112 trace_context_header = b"x-cloud-trace-context"
119113 execution_id = None
@@ -127,22 +121,18 @@ async def __call__(self, scope, receive, send):
127121
128122 if not execution_id :
129123 execution_id = _generate_execution_id ()
130- # Add the execution ID to headers
131124 new_headers = list (scope .get ("headers" , []))
132125 new_headers .append (
133126 (execution_id_header , execution_id .encode ("latin-1" ))
134127 )
135128 scope ["headers" ] = new_headers
136129
137- # Store execution context in ASGI scope for recovery in case of context loss
138- # Parse trace context to extract span ID
139130 span_id = None
140131 if trace_context :
141132 trace_match = re .match (_TRACE_CONTEXT_REGEX_PATTERN , trace_context )
142133 if trace_match :
143134 span_id = trace_match .group ("span_id" )
144135
145- # Store in scope for potential recovery
146136 scope ["execution_context" ] = ExecutionContext (execution_id , span_id )
147137
148138 await self .app (scope , receive , send ) # pragma: no cover
@@ -169,9 +159,7 @@ def wrapper(*args, **kwargs):
169159
170160 with stderr_redirect , stdout_redirect :
171161 result = view_function (* args , ** kwargs )
172-
173162 # Context cleanup happens automatically via Flask's request context
174- # No need to manually clean up flask.g
175163 return result
176164
177165 return wrapper
@@ -195,14 +183,10 @@ def set_execution_context_async(enable_id_logging=False):
195183 def decorator (view_function ):
196184 @functools .wraps (view_function )
197185 async def async_wrapper (request , * args , ** kwargs ):
198- # Extract execution context from headers
199186 context = _extract_context_from_headers (request .headers )
200-
201- # Set context using contextvars
202187 token = execution_context_var .set (context )
203188
204189 with stderr_redirect , stdout_redirect :
205- # Handle both sync and async functions
206190 if inspect .iscoroutinefunction (view_function ):
207191 result = await view_function (request , * args , ** kwargs )
208192 else :
@@ -215,10 +199,7 @@ async def async_wrapper(request, *args, **kwargs):
215199
216200 @functools .wraps (view_function )
217201 def sync_wrapper (request , * args , ** kwargs ): # pragma: no cover
218- # For sync functions, we still need to set up the context
219202 context = _extract_context_from_headers (request .headers )
220-
221- # Set context using contextvars
222203 token = execution_context_var .set (context )
223204
224205 with stderr_redirect , stdout_redirect :
@@ -229,7 +210,6 @@ def sync_wrapper(request, *args, **kwargs): # pragma: no cover
229210 execution_context_var .reset (token )
230211 return result
231212
232- # Return appropriate wrapper based on whether the function is async
233213 if inspect .iscoroutinefunction (view_function ):
234214 return async_wrapper
235215 else :
0 commit comments