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
{{ message }}
This repository was archived by the owner on Aug 13, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 13, 2026. It is now read-only.
Currently the RequestMiddleware for django uses threading.local to pass the request to the logging code. As a consequence it appears the middleware can only be used by a wsgi app. Without the middleware headers are unavailable to the logging code and things like traceparent logging won't happen. I don't know much about it but I've seen references to asgiref as the way to store thread locals for an asgi app.
I request you add support for asgi, maybe by making an asgiref version of RequestMiddleware.
Notes
When I run an example app using asgi, I get errors like:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/usr/local/lib/python3.10/site-packages/django/utils/deprecation.py", line 136, in __call__
response = self.process_response(request, response)
File "/usr/local/lib/python3.10/site-packages/django/middleware/clickjacking.py", line 27, in process_response
if response.get("X-Frame-Options") is not None:
AttributeError: 'coroutine' object has no attribute 'get'
[2022-08-15 15:21:54 +0000] [77] [ERROR] Exception in ASGI application
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/usr/local/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
return await self.app(scope, receive, send)
File "/usr/local/lib/python3.10/site-packages/django/core/handlers/asgi.py", line 155, in __call__
await self.handle(scope, receive, send)
File "/usr/local/lib/python3.10/site-packages/django/core/handlers/asgi.py", line 177, in handle
response = await self.get_response_async(request)
File "/usr/local/lib/python3.10/site-packages/django/core/handlers/base.py", line 162, in get_response_async
response = await self._middleware_chain(request)
TypeError: object HttpResponse can't be used in 'await' expression
# added at the bottom of settings.pyimportgoogle.cloud.logginggoogle.cloud.logging.Client().setup_logging()
MIDDLEWARE.append('google.cloud.logging_v2.handlers.middleware.request.RequestMiddleware')
Activity
daniel-sanche commentedon Aug 23, 2022
Thanks for reporting this, I'll try to take a look soon
daniel-sanche commentedon Sep 9, 2022
I've attempted to address this on a branch here: https://github.com/googleapis/python-logging/pull/625/files
This change seems to fix the exception you were seeing, but I'm going to need to consult with a django expert before moving forward