Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update request-id recipe to use contextvars #2404

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

EricGoulart
Copy link

Summary of Changes

Migrate the request-id handling from threading.local() to contextvars to improve compatibility with async coroutines and avoid issues with threading. This change ensures that request-id is properly tracked in asynchronous environments, providing more robust handling in both sync and async contexts.
Previously, threading.local() was used, which does not handle coroutines effectively. By using contextvars, we ensure that the request-id remains consistent across async calls.

Related Issues

Closes #2260.

EricGoulart and others added 10 commits October 20, 2024 14:19
Migrate the request-id handling from threading.local() to contextvars to improve compatibility
with async coroutines and avoid issues with threading. This change ensures that request-id
is properly tracked in asynchronous environments, providing more robust handling in both
sync and async contexts.
Previously, threading.local() was used, which does not handle coroutines effectively.
By using contextvars, we ensure that the request-id remains consistent across async calls.

Closes falconry#2260
Add tests to verify that request_id is unique per request and correctly set in response headers. Tests include checks for isolation in async calls and persistence in synchronous requests.

Related to issue falconry#2260
Add tests to verify that request_id is unique per request and correctly set in response headers. Tests include checks for isolation in async calls and persistence in synchronous requests.

Related to issue falconry#2260
@EricGoulart
Copy link
Author

I wanted to apologize for the confusion with my pull request. This is my first time contributing to a public repository, and I made a mistake with the process. I had previously opened a PR with the changes, but I accidentally closed it and am unable to reopen it.

Copy link

codecov bot commented Nov 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (11076b8) to head (e3bb626).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2404   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           64        64           
  Lines         7728      7728           
  Branches      1071      1071           
=========================================
  Hits          7728      7728           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

CaselIT
CaselIT previously approved these changes Nov 23, 2024

response = client.simulate_get('/test')
assert 'X-Request-ID' in response.headers
assert response.headers['X-Request-ID'] == response.json['request_id']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could also test that's the same as the value returned in the body

Copy link
Member

@vytas7 vytas7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good now at first glance. 👍

However, many CI gates are still failing, could you please take a look?
(To save some time on review and CI rounds, it may be more efficient to run tox locally first on your side, and make sure everything is green.)

Sorry for the above comment, all these failures were caused by a new version of the websockets test dependency, I have already fixed that. So all the checks are ostensibly green now.

However, there test issue that I pointed to before still remains. We don't run these tests upon every PR, but we do for master merge.
If you have access to Docker, could you pip install cibuildwheel, and run:

CIBW_BUILD=cp312-manylinux_x86_64 cibuildwheel

⬇️

<...>
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

    # middleware.py
    
    from uuid import uuid4
    
>   from .request_id_context import ctx
E   ModuleNotFoundError: No module named 'examples'

/project/examples/recipes/request_id_middleware.py:5: ModuleNotFoundError
=========================== short test summary info ============================
ERROR ../../../project/tests/test_recipes.py::TestRequestIDContext::test_request_id_isolated
ERROR ../../../project/tests/test_recipes.py::TestRequestIDContext::test_request_id_persistence
ERROR ../../../project/tests/test_recipes.py::TestRequestIDContext::test_request_id_in_response_header
================= 3570 passed, 463 skipped, 3 errors in 31.92s =================

⬆️ I'm still getting new errors specifically from your tests, so we must address them in any case.

@@ -10,14 +10,14 @@ to every log entry.

If you wish to trace each request throughout your application, including
from within components that are deeply nested or otherwise live outside of the
normal request context, you can use a `thread-local`_ context object to store
normal request context, you can use a `contextvars`_ object to store
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could simply replace this with an Intersphinx link to the contextvars module, but this applies to the current version too, so not a requirement for merging this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update request-id recipe to use contextvars instead of threading.local()
3 participants