Skip to content

Commit 54a6df1

Browse files
committed
chore: clean up impl.
1 parent 7491870 commit 54a6df1

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

src/functions_framework/aio/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@
1616
import contextvars
1717
import functools
1818
import inspect
19-
import json
2019
import logging
2120
import logging.config
2221
import os
23-
import re
24-
import sys
2522
import traceback
2623

2724
from typing import Any, Awaitable, Callable, Dict, Tuple, Union

src/functions_framework/execution_id.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,17 @@ async def __call__(self, scope, receive, send):
136136
await self.app(scope, receive, send) # pragma: no cover
137137

138138

139-
# Sets execution id and span id for the request
140139
def set_execution_context(request, enable_id_logging=False):
140+
"""Decorator for Flask/WSGI handlers that sets execution context.
141+
142+
Takes request object at decoration time (Flask pattern where request is available
143+
via thread-local context when decorator is applied).
144+
145+
Usage:
146+
@set_execution_context(request, enable_id_logging=True)
147+
def view_func(path):
148+
...
149+
"""
141150
if enable_id_logging:
142151
stdout_redirect = contextlib.redirect_stdout(
143152
LoggingHandlerAddExecutionId(sys.stdout)
@@ -165,6 +174,16 @@ def wrapper(*args, **kwargs):
165174

166175

167176
def set_execution_context_async(enable_id_logging=False):
177+
"""Decorator for ASGI/async handlers that sets execution context.
178+
179+
Unlike set_execution_context which takes request at decoration time (Flask pattern),
180+
this expects the decorated function to receive request as its first parameter (ASGI pattern).
181+
182+
Usage:
183+
@set_execution_context_async(enable_id_logging=True)
184+
async def handler(request, *args, **kwargs):
185+
...
186+
"""
168187
if enable_id_logging:
169188
stdout_redirect = contextlib.redirect_stdout(
170189
LoggingHandlerAddExecutionId(sys.stdout)

tests/test_execution_id_async.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import json
1616
import pathlib
1717
import re
18-
import sys
1918

2019
from functools import partial
2120
from unittest.mock import Mock, patch
@@ -297,14 +296,15 @@ async def test_maintains_execution_id_for_concurrent_requests(monkeypatch, capsy
297296

298297
def test_async_decorator_with_sync_function():
299298
"""Test that the async decorator handles sync functions properly."""
300-
from functions_framework.execution_id import set_execution_context_async
301299

302300
# Create a sync function
303301
def sync_func(request):
304302
return {"status": "ok"}
305303

306-
# Apply the async decorator
307-
wrapped = set_execution_context_async(enable_id_logging=False)(sync_func)
304+
# Apply the decorator
305+
wrapped = execution_id.set_execution_context_async(enable_id_logging=False)(
306+
sync_func
307+
)
308308

309309
# Create mock request
310310
request = Mock()

0 commit comments

Comments
 (0)