You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add execution_id support for async stack (#377)
* feat: add execution_id support for async stack
- Add contextvars support to execution_id.py for async-safe context storage
- Create AsgiMiddleware class to inject execution_id into ASGI requests
- Add set_execution_context_async decorator for both sync and async functions
- Update LoggingHandlerAddExecutionId to support both Flask g and contextvars
- Integrate execution_id support in aio/__init__.py with proper exception handling
- Add comprehensive async tests matching sync test functionality
- Follow Starlette best practices for exception handling
The implementation enables automatic execution_id injection and logging for async
functions when LOG_EXECUTION_ID=true, matching the existing sync stack behavior.
* refactor: move exception logging to crash handler for cleaner code
- Remove try/catch blocks from wrapper functions
- Centralize exception logging in _crash_handler
- Extract execution_id directly from request headers in crash handler
- Temporarily set context when logging exceptions to ensure execution_id is included
- This approach is cleaner and more similar to Flask's centralized exception handling
* refactor: improve code organization based on feedback
- Move imports to top of file instead of inside functions
- Extract common header parsing logic into _extract_context_from_headers helper
- Reduce code duplication between sync and async decorators
- Add comment explaining why crash handler needs to extract context from headers
- This addresses the context reset issue where decorators clean up before exception handlers run
* fix: preserve execution context for exception handlers
- Don't reset context on exception, only on successful completion
- This allows exception handlers to access execution_id naturally
- Simplify crash handler since context is now available
- Rely on Python's automatic contextvar cleanup when task completes
- Each request runs in its own task, so no risk of context leakage
This is more correct and follows the principle that context should be
available throughout the entire request lifecycle, including error handling.
* style: apply black and isort formatting
- Format code with black for consistent style
- Sort imports with isort for better organization
- All linting checks now pass
* refactor: clean up async tests and remove redundant comments
* chore: remove uv.lock from version control
* style: fix black formatting
* fix: skip async execution_id tests on Python 3.7
* refactor: reuse _enable_execution_id_logging from main module
* chore: more cleanup.
* test: remove unnecessary pragma no cover for sync_wrapper
* test: improve coverage by removing unnecessary pragma no cover annotations
* style: fix black formatting
* style: fix isort import ordering
* test: add back pragma no cover for genuinely hard-to-test edge cases
* refactor: simplify async decorator by removing dead code branch
* fix: exclude async-specific code from py37 coverage
The AsgiMiddleware class and set_execution_context_async function in
execution_id.py require Python 3.8+ due to async dependencies. These
are now excluded from coverage calculations in Python 3.7 environments.
* fix: improve async execution ID context propagation using contextvars
- Use contextvars.copy_context() to properly propagate execution context in async functions
- Implement AsyncExecutionIdHandler to handle JSON logging with execution_id
- Redirect logging output from stderr to stdout for consistency
- Add build dependency to dev dependencies
- Update tests to reflect new logging output location
* feat: Add execution ID logging for async functions
Refactors the async logging implementation to align with the sync version, ensuring consistent execution ID logging across both stacks.
* chore: clean up impl.
* refactor: define custom exception handling middleware to avoid duplicate log of traceback.
* style: run black
* chore: clean up code a little more.
* fix: propagate context in ce fns.
* style: more nits.
* chore: remove unncessary debug flag.
* fix: respond to PR comments
* fix: respond to more PR comments
0 commit comments