question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A subsequent exception is encountered when django instrumentation is handling the original exception

See original GitHub issue

Describe your environment opentelemetry-sdk==v0.16b1 opentelemetry-instrumentation==v0.16b1 opentelemetry-instrumentation-django==v0.16b1 opentelemetry-instrumentation-dbapi==v0.16b1 opentelemetry-instrumentation-requests==v0.16b1 opentelemetry-instrumentation-redis==v0.16b1 opentelemetry-instrumentation-celery==v0.16b1

Django==2.2.14

Note This issue is not present when running Django-3.1.5. Things seem to work as expected.

Steps to reproduce Make it so one of your views will always raise an Exception. Launch the server and then make a request to the views endpoint.

The raised exception will cause the following subsequent stack trace to occur:

Traceback (most recent call last):
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/stschenk/code/django-sandbox/mysite/polls/views.py", line 5, in index
    raise Exception()
Exception

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 858, in _resolve_lookup
    current = current()
TypeError: __call__() missing 1 required positional argument: 'func'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/utils/deprecation.py", line 94, in __call__
    response = response or self.get_response(request)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/core/handlers/exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/core/handlers/exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/core/handlers/exception.py", line 125, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/views/debug.py", line 94, in technical_500_response
    html = reporter.get_traceback_html()
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/views/debug.py", line 334, in get_traceback_html
    return t.render(c)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/defaulttags.py", line 309, in render
    return nodelist.render(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 987, in render
    output = self.filter_expression.resolve(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 671, in resolve
    obj = self.var.resolve(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 796, in resolve
    value = self._resolve_lookup(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 861, in _resolve_lookup
    getcallargs(current)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/inspect.py", line 1335, in getcallargs
    f_name = func.__name__
AttributeError: '_GeneratorContextManager' object has no attribute '__name__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 858, in _resolve_lookup
    current = current()
TypeError: __call__() missing 1 required positional argument: 'func'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/utils/deprecation.py", line 94, in __call__
    response = response or self.get_response(request)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/core/handlers/exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/core/handlers/exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/core/handlers/exception.py", line 125, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/views/debug.py", line 94, in technical_500_response
    html = reporter.get_traceback_html()
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/views/debug.py", line 334, in get_traceback_html
    return t.render(c)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/defaulttags.py", line 309, in render
    return nodelist.render(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 987, in render
    output = self.filter_expression.resolve(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 671, in resolve
    obj = self.var.resolve(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 796, in resolve
    value = self._resolve_lookup(context)
  File "/Users/stschenk/.virtualenvs/django-sandbox/lib/python3.7/site-packages/django/template/base.py", line 861, in _resolve_lookup
    getcallargs(current)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/inspect.py", line 1335, in getcallargs
    f_name = func.__name__
AttributeError: '_GeneratorContextManager' object has no attribute '__name__'

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
rafaelfbscommented, Aug 12, 2021

The same error happened to me, using the following environment:

python==3.8.11

Django==2.0.13 opentelemetry-api==1.4.1 opentelemetry-sdk==1.4.1 opentelemetry-exporter-jaeger==1.4.1 opentelemetry-propagator-b3==1.4.1 opentelemetry-instrumentation-django==0.23b2 opentelemetry-instrumentation-logging==0.23b2 opentelemetry-instrumentation-requests==0.23b2 opentelemetry-instrumentation-sqlite3==0.23b2

Another workaround that worked for me:

def fix_django_req(_, req):
    activation_key = "opentelemetry-instrumentor-django.activation_key"
    if activation_key in req.META:
        req.META[activation_key].__name__ = ""

DjangoInstrumentor().instrument(request_hook=fix_django_req)
1reaction
stschenkcommented, Feb 18, 2021

Yeah, the stack trace is in common django code, but it is being caused by the contextmanager from Trace.use_span() not having a __name__ property. I do not fully understand why or if the context manager really should have __name__. But Django 2.2.14 is expecting to find it.

I have worked around the issue by subclassing the otel middleware:

class OTELWorkAroundFixMiddleware(_DjangoMiddleware):
    """
    Extension of OTEL django middleware that fixes an issue encounted when processing an
    unexpected exception during the handling of a request.  Another exception is encoutered
    during the handling of the exception.  There is an issue open for this at: https://github.com/open-telemetry/opentelemetry-python-contrib/issues/288

    The cause of the subsiquent exception is that the use_span context manager does not have
    a __name_ property.  I am extending the middleware to add this property.
    """

    def process_request(self, request):
        super().process_request(request)
        if self._environ_activation_key in request.META:
            # Add __name__ property to context manager so that it will not trigger an exception
            # during the processing of a proceeding exception when handling the request.
            request.META[self._environ_activation_key].__name__ = ""

Hope this help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django Exceptions
The SuspiciousOperation exception is raised when a user has performed an operation that should be considered suspicious from a security perspective, such as ......
Read more >
Learning (not) to Handle Exceptions - Python for the Lab
We are going to cover from the basics of error handling to defining your own exceptions. You will learn why sometimes it is...
Read more >
Exceptions - Django REST framework
You can implement custom exception handling by creating a handler function that converts exceptions raised in your API views into response objects. This...
Read more >
Professional Error Handling With Python - Code
The call to open_db_connection() may not return a connection or raise an exception itself. In this case there is no need to close...
Read more >
How to Define Custom Exception Classes in Python
The construction of custom exception classes can enrich our class design. A custom error class could logs errors, inspect an object. It is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found