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

Django ASGI not supported #165

Closed
HiveTraum opened this issue Aug 19, 2020 · 10 comments · Fixed by #391
Closed

Django ASGI not supported #165

HiveTraum opened this issue Aug 19, 2020 · 10 comments · Fixed by #391
Labels

Comments

@HiveTraum
Copy link
Contributor

Describe your environment
Python 3.8
Docker(python:3.8 image)
daphne-2.5.0/uvicorn 0.11.8/hypercorn 0.10.2
Django-3.1.0

Steps to reproduce

git clone https://github.com/HiveTraum/opentelemetry-django-asgi-example
cd opentelemetry-django-asgi-example
docker-compose up -d
curl -I 127.0.0.1:8000/api/
curl -I 127.0.0.1:8001/api/
curl -I 127.0.0.1:8002/api/

What is the expected behavior?
Correct request processing

What is the actual behavior?
KeyError when trying to get wsgi.url_scheme from environ

Additional context

Every wsgi server fill this variable inside request.META, but every asgi doesn't. Actually I don't know precisely to whom this bug can be addressed. For now opentelemetry-instrumentation-django relies on opentelemetry-instrumentation-wsgi, but ignores opentelemetry-instrumentation-asgi, maybe this can be key to solve this issue

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/asgiref/sync.py", line 330, in thread_handler
    raise exc_info[1]
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 38, in inner
    response = await get_response(request)
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 221, in _get_response_async
    response = await middleware_method(request, callback, callback_args, callback_kwargs)
  File "/usr/local/lib/python3.8/site-packages/asgiref/sync.py", line 296, in __call__
    ret = await asyncio.wait_for(future, timeout=None)
  File "/usr/local/lib/python3.8/asyncio/tasks.py", line 455, in wait_for
    return await fut
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.8/site-packages/asgiref/sync.py", line 334, in thread_handler
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/opentelemetry/instrumentation/django/middleware.py", line 73, in process_view
    attributes = collect_request_attributes(environ)
  File "/usr/local/lib/python3.8/site-packages/opentelemetry/instrumentation/wsgi/__init__.py", line 111, in collect_request_attributes
    result["http.url"] = wsgiref_util.request_uri(environ)
  File "/usr/local/lib/python3.8/wsgiref/util.py", line 72, in request_uri
    url = application_uri(environ)
  File "/usr/local/lib/python3.8/wsgiref/util.py", line 52, in application_uri
    url = environ['wsgi.url_scheme']+'://'
KeyError: 'wsgi.url_scheme'
@vladimirmyshkovski
Copy link

The same error

@lzchen lzchen transferred this issue from open-telemetry/opentelemetry-python Nov 9, 2020
adamantike added a commit to adamantike/opentelemetry-python-contrib that referenced this issue Mar 31, 2021
Still a WIP, this diff adds `asgi` as an extra, and uses its methods if
the current request is an `ASGIRequest`.

I still need to dig deeper in the current test suite, to find a way to
duplicate the tests in
`instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py`,
but using an
[`AsyncClient`](https://docs.djangoproject.com/en/3.1/topics/testing/tools/#testing-asynchronous-code).

Fixes open-telemetry#165, open-telemetry#185, open-telemetry#280, open-telemetry#334.
@adamantike adamantike mentioned this issue Mar 31, 2021
7 tasks
adamantike added a commit to adamantike/opentelemetry-python-contrib that referenced this issue Mar 31, 2021
Still a WIP, this diff adds `asgi` as an extra, and uses its methods if
the current request is an `ASGIRequest`.

I still need to dig deeper in the current test suite, to find a way to
duplicate the tests in
`instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py`,
but using an
[`AsyncClient`](https://docs.djangoproject.com/en/3.1/topics/testing/tools/#testing-asynchronous-code).

Fixes open-telemetry#165, open-telemetry#185, open-telemetry#280, open-telemetry#334.
@github-actions
Copy link

github-actions bot commented Apr 6, 2021

This issue was marked stale due to lack of activity. It will be closed in 30 days.

adamantike added a commit to adamantike/opentelemetry-python-contrib that referenced this issue Apr 6, 2021
Still a WIP, this diff adds `asgi` as an extra, and uses its methods if
the current request is an `ASGIRequest`.

I still need to dig deeper in the current test suite, to find a way to
duplicate the tests in
`instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py`,
but using an
[`AsyncClient`](https://docs.djangoproject.com/en/3.1/topics/testing/tools/#testing-asynchronous-code).

Fixes open-telemetry#165, open-telemetry#185, open-telemetry#280, open-telemetry#334.
adamantike added a commit to adamantike/opentelemetry-python-contrib that referenced this issue May 30, 2021
Still a WIP, this diff adds `asgi` as an extra, and uses its methods if
the current request is an `ASGIRequest`.

I still need to dig deeper in the current test suite, to find a way to
duplicate the tests in
`instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py`,
but using an
[`AsyncClient`](https://docs.djangoproject.com/en/3.1/topics/testing/tools/#testing-asynchronous-code).

Fixes open-telemetry#165, open-telemetry#185, open-telemetry#280, open-telemetry#334.
adamantike added a commit to adamantike/opentelemetry-python-contrib that referenced this issue Jun 3, 2021
This diff adds `asgi` as an extra, and uses its methods if the current
request is an `ASGIRequest`.

I still need to dig deeper in the current test suite, to find a way to
duplicate the tests in
`instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py`,
but using an
[`AsyncClient`](https://docs.djangoproject.com/en/3.1/topics/testing/tools/#testing-asynchronous-code).

Fixes open-telemetry#165, open-telemetry#185, open-telemetry#280, open-telemetry#334.
@github-actions
Copy link

github-actions bot commented Jun 5, 2021

Closed as inactive. Feel free to reopen if this issue needs resolving.

@github-actions github-actions bot closed this as completed Jun 5, 2021
adamantike added a commit to adamantike/opentelemetry-python-contrib that referenced this issue Jul 6, 2021
This diff adds `asgi` as an extra, and uses its methods if the current
request is an `ASGIRequest`.

I still need to dig deeper in the current test suite, to find a way to
duplicate the tests in
`instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py`,
but using an
[`AsyncClient`](https://docs.djangoproject.com/en/3.1/topics/testing/tools/#testing-asynchronous-code).

Fixes open-telemetry#165, open-telemetry#185, open-telemetry#280, open-telemetry#334.
adamantike added a commit to adamantike/opentelemetry-python-contrib that referenced this issue Sep 21, 2021
This diff adds `asgi` as an extra, and uses its methods if the current
request is an `ASGIRequest`.

I still need to dig deeper in the current test suite, to find a way to
duplicate the tests in
`instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py`,
but using an
[`AsyncClient`](https://docs.djangoproject.com/en/3.1/topics/testing/tools/#testing-asynchronous-code).

Fixes open-telemetry#165, open-telemetry#185, open-telemetry#280, open-telemetry#334.
adamantike added a commit to adamantike/opentelemetry-python-contrib that referenced this issue Sep 28, 2021
This diff adds `asgi` as an extra, and uses its methods if the current
request is an `ASGIRequest`.

I still need to dig deeper in the current test suite, to find a way to
duplicate the tests in
`instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py`,
but using an
[`AsyncClient`](https://docs.djangoproject.com/en/3.1/topics/testing/tools/#testing-asynchronous-code).

Fixes open-telemetry#165, open-telemetry#185, open-telemetry#280, open-telemetry#334.
adamantike added a commit to adamantike/opentelemetry-python-contrib that referenced this issue Oct 11, 2021
This diff adds `asgi` as an extra, and uses its methods if the current
request is an `ASGIRequest`.

I still need to dig deeper in the current test suite, to find a way to
duplicate the tests in
`instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py`,
but using an
[`AsyncClient`](https://docs.djangoproject.com/en/3.1/topics/testing/tools/#testing-asynchronous-code).

Fixes open-telemetry#165, open-telemetry#185, open-telemetry#280, open-telemetry#334.
@adamantike
Copy link
Contributor

@HiveTraum @vladimirmyshkovski, as this issue was automatically closed, I'm pinging you to let you know that Django ASGI support has been added in #391, and released in version 0.25b0. It requires installing opentelemetry-instrumentation-django with the asgi extra.

@fynjah
Copy link

fynjah commented Nov 2, 2021

Still have same problem. 0.25b2 is installed with asgi extra.

pip install -U opentelemetry-instrumentation-django[asgi] --no-cache-dir
:/

@adamantike
Copy link
Contributor

@fynjah, do you have an application example I can use to test why it fails? Or, at least, which Python/Django versions are being installed, and how the project is instrumented and executed?

@fynjah
Copy link

fynjah commented Nov 2, 2021

sys.version
Out[12]: '3.8.10 (default, May  4 2021, 00:00:00) \n[GCC 10.2.1 20201125 (Red Hat 10.2.1-9)]'
opentelemetry-api==1.6.2
opentelemetry-instrumentation==0.25b2
opentelemetry-instrumentation-asgi==0.25b2
opentelemetry-instrumentation-django==0.25b2
opentelemetry-instrumentation-wsgi==0.25b2
opentelemetry-sdk==1.6.2
opentelemetry-semantic-conventions==0.25b2
opentelemetry-util-http==0.25b2
django==3.2.8

@adamantike Unfortunately I don't have example app:/ I will try to repeat this issue on example project asap.
The project - huge REST API backend for mobile app.
Anyway, right now I'm in research and just trying to start using opentelemetry on local environment.

@akiross
Copy link

akiross commented May 23, 2022

I had a similar issue: had opentelemetry-instrumentation-django installed with asgi extras, but still received an error

  File "/usr/local/lib/python3.8/wsgiref/util.py", line 72, in request_uri
    url = application_uri(environ)
  File "/usr/local/lib/python3.8/wsgiref/util.py", line 52, in application_uri
    url = environ['wsgi.url_scheme']+'://'
KeyError: 'wsgi.url_scheme'

Since otel was added to an existing application using django 4 and django-channels 3, it wasn't immediate to find the cause of this, but in the end it was due to misconfiguration of the routing, which did not handle http requests with asgi.

Documentation for asgi and django/websocket is still missing though.

@pilhuhn
Copy link

pilhuhn commented Oct 27, 2022

@akiross Can you elaborate on what you did to resolve this?

@akiross
Copy link

akiross commented Nov 2, 2022

@pilhuhn sure, I didn't add details since I wasn't sure this might be useful in other situations, but in our application/routing.py we missed a couple of lines:

from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application

from webapp import routing

application = ProtocolTypeRouter(
    {
        "http": get_asgi_application(),  # this one was missing
        "websocket": URLRouter(routing.websocket_urlpatterns),
    }
)

HIH

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

Successfully merging a pull request may close this issue.

6 participants