googleapis / googleapis/google-cloud-python

Async Support for the threading Local Request store

Open
#15,413 3 comments 3 reactions 1 assignee Claimed by @gkevinzheng View on GitHub
api: logging priority: p3 type: question
Dominant language
Python
Stars
5.4k
Forks
1.8k
Avg merge
3d 4h
Merged PRs (30d)
122

Description

At this moment the code in https://github.com/googleapis/python-logging/blob/main/google/cloud/logging_v2/handlers/middleware/request.py uses a threading.Local to store the latest request for usage with the structured Logging.

This global singleton approach does not seem safe for Async Code, where one would either rather use contextvars library, or even easier for the current codebase do something along this:

```python
try:
from asgiref.local import Local
except ImportError:
from threading import local as Local

_thread_locals = Local()
```

Sidenote: every code I see using this technique imports threading on top anyway, I am not sure if this has some deeper significance, so I omitted it.

I do think, that to support async correctly in newer Django Versions, this change should be made, or it might ending up sending wrong logs in certain situations.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.