Nicolas Lusa
Nicolas Lusa

Reputation: 1

Sentry with django integration throws KeyError request

I am going nuts with this issue which I believe is coming from sentry-sdk for python, probably in combination with some other dependencies. I have a project in Django 4.2 with the sentry-sdk 2.13.0 which throws a KeyError request each time something is triggered on sentry, meaning if an error is reported I get X more errors reported about KeyError request (yes it is not just 1 for each report done). What confuses me is that the original error is shown correctly on sentry so I cannot say that sentry-sdk is failing reporting the issue. Moreover this happens also whenever I manually trigger a info/warning report from code to sentry.

To note, this started happening after upgrading the project dependencies, especially Django to 4.2.

I also thought it could have been a context_processor issue but context_processor for request is in the settings (see code below)

Any help or suggestion is much appreciated.

This is my settings for sentry:

import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration

sentry_sdk.init(
    dsn="XXXX",
    integrations=[DjangoIntegration()],
    server_name='XXXX',

    send_default_pii=True
)

And this is the stacktrace for the error:

KeyError
'request'

django/template/context.py in __getitem__ at line 83

cms/templatetags/cms_tags.py in _get_empty_context at line 636

cms/templatetags/cms_tags.py in get_context at line 829

cms/templatetags/cms_tags.py in render_tag at line 810

classytags/core.py in render at line 142

django/template/base.py in render_annotated at line 966

django/template/base.py in render at line 1005

django/template/loader_tags.py in render at line 54

django/template/base.py in render_annotated at line 966

django/template/base.py in render at line 1005

django/template/base.py in _render at line 167

django/template/base.py in render at line 177

django/template/loader_tags.py in render at line 208

django/template/base.py in render_annotated at line 966

django/template/base.py in render at line 1005

django/template/loader_tags.py in render at line 63

django/template/base.py in render_annotated at line 966

django/template/base.py in render at line 1005

cms/templatetags/cms_tags.py in render_tag at line 426

classytags/core.py in render at line 142

django/template/base.py in render_annotated at line 966

django/template/base.py in render at line 1005

sekizai/templatetags/sekizai_tags.py in render_tag at line 86

classytags/core.py in render at line 142

django/template/base.py in render_annotated at line 966

django/template/base.py in render at line 1005

django/template/base.py in _render at line 167

django/template/loader_tags.py in render at line 157

django/template/base.py in render_annotated at line 966

django/template/base.py in render at line 1005

django/template/base.py in _render at line 167

django/template/loader_tags.py in render at line 157

django/template/base.py in render_annotated at line 966

django/template/base.py in render at line 1005

django/template/base.py in _render at line 167

django/template/base.py in render at line 175

django/template/backends/django.py in render at line 61

django/views/defaults.py in server_error at line 99

django/utils/decorators.py in _wrapper_view at line 134

django/core/handlers/exception.py in handle_uncaught_exception at line 185

django/core/handlers/exception.py in response_for_exception at line 140

django/core/handlers/exception.py in inner at line 57

django/utils/deprecation.py in __call__ at line 134

django/core/handlers/exception.py in inner at line 55

Context processors definition:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            ...
        ],
        'OPTIONS': {
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.request',
                'sekizai.context_processors.sekizai',
                'cms.context_processors.cms_settings',
                'website.context_processors.google_analytics'
            ],
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
                'admin_tools.template_loaders.Loader',
            ],
        },
    }
]

If I try to reproduce the original error that triggered all the KeyErrors without reporting with sentry I would only see the original error and no KeyError.

I checked stacktrace more into details but I did not manage to figure out much. Only that cms packages are involved.

(idk if that can be relevant but I am using django-cms 3.11.6)

EDIT: reported directly on their repo issues section if anyone interested: https://github.com/getsentry/sentry-python/issues/3469

Upvotes: 0

Views: 164

Answers (1)

sentrivana
sentrivana

Reputation: 1

Looking at the stacktrace, it looks like something is overwriting the template context. sentry-sdk does some template patching but if it's touching the context, it's just adding tracing info as far as I can see.

What you can do to narrow down the issue:

  • Try downgrading the individual dependencies to the original versions to pinpoint which one specifically started causing this. If you have a hunch that it was Django itself, you could start with that. Once you know, you can either poke around the dependency's changelog or share this info with the sentry-sdk devs so that they can take a look if there's anything they need to adjust. Which brings me to:
  • Consider opening an issue at the sentry-sdk repo -- it's a bit easier for the devs to help with debugging there.

Upvotes: 0

Related Questions