Thanks to visit codestin.com
Credit goes to github.com

Skip to content

aiohttp integration ability to use contextvars in logger #670

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

Closed
H--o-l opened this issue Apr 16, 2020 · 2 comments · Fixed by #674
Closed

aiohttp integration ability to use contextvars in logger #670

H--o-l opened this issue Apr 16, 2020 · 2 comments · Fixed by #674

Comments

@H--o-l
Copy link
Contributor

H--o-l commented Apr 16, 2020

Hi!

I was looking to add a custom logging filter to my aiohttp server during exception that are catched here.

It's useful and easy to do, but, it occurs that, in my custom logging filter, I can't find contextvars that I have created in my request handler task.

Two things caused this problem:

Python documentation on Tasks here. One important thing is that they reset contextvars.

To summarize:

   aiohttp                               logging exception
      +                                        ^
      |                                        |
      | asyncio.create_task(handle_request())  | contextvars didn't go up again
      |                                        | (it's fixed now)
      |                                        |
      v                                        |
   Sentry                                      |
      +                                        |
      |                                        |
      | asyncio.create_task(handle_request())  | contextvars don't go up
      |                                        |
      v                                        |
I set contextvars        +---------------------+

                                   Exception

As long as the issue is not fixed in Sentry, I still can't use contextvars to log custom data using the standard Python logging library.
The only solution is to disable Sentry, then logging works OK with contextvars.

Any idea how to fix this in Sentry-aiohttp code?
I'd be happy to open a PR, but I'm not familiar enough with Sentry code, or Python in general, thus I need some help at least.

@untitaker
Copy link
Member

I see, it seems the double-task thing may not be necessary at all assuming all aiohttp versions we care about already create a task. I would be fine with a PR that does not wrap anything in a task at all. This is very likely just cargo-culted from the Sanic integration.

@H--o-l
Copy link
Contributor Author

H--o-l commented Apr 20, 2020

Hi @untitaker

Thanks for your quick answer.
I open a PR which does, I think, what you proposed: #674

untitaker pushed a commit that referenced this issue Apr 20, 2020
aiohttp integration currently re-create a task to encapsulate the request
handler.
But:
- aiohttp already does it.
- contextvars created in it can't be read by aiohttp.
  It's an issue for users custom logger.

Fix #670
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 a pull request may close this issue.

3 participants