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

No request data with django #267

Closed
bluesurfer opened this issue Feb 19, 2019 · 18 comments · Fixed by #393
Closed

No request data with django #267

bluesurfer opened this issue Feb 19, 2019 · 18 comments · Fixed by #393
Labels
Type: Bug Something isn't working

Comments

@bluesurfer
Copy link

I recently decided to switch from Raven to sentry-sdk version 0.7.3.

When an event was reported using Raven I use to have many useful tags (the URLs originating the error, the user browser/device, OS information and much more). All this comes out of the box without the need to include any scripts in the frontend.

Despite sentry-sdk has simplified the configuration process I find myself with a weaker error tracking system (fewer information on each event).

I am working on a Django project and followed the docs

Am I missing something?

@bluesurfer bluesurfer changed the title Where are all the Raven (deprecated) default tags? Where are all the Raven default tags? Feb 19, 2019
@untitaker
Copy link
Member

The tags you see are generated serverside and are inferred from HTTP headers and the current URL. All of this should be there when you turn on the Django integration. We're using this for Sentry itself and it generally is supposed to work.

From what you've told me it seems that you generally have no request data attached to your events. Could you paste the exact code you're using, and more importantly, where do you call init of the SDK?

@untitaker
Copy link
Member

@bluesurfer any update on this? This seems like a bug in the SDK, even if just a UX bug.

@bluesurfer
Copy link
Author

bluesurfer commented Feb 24, 2019

@untitaker oops, sorry for delay.. I just put the docs code (with init sentry) at the end of my settings file and I haven't defined any LOGGING configuration.

From what you've told me it seems that you generally have no request data attached to your events.

Yes, that's the issue. My previously working logging configuration with Raven was:

# Raven
# ------------------------------------------------------------------------------
INSTALLED_APPS += ['raven.contrib.django.raven_compat']  # noqa F405
MIDDLEWARE = ['raven.contrib.django.raven_compat.middleware.SentryResponseErrorIdMiddleware'] + MIDDLEWARE

# Sentry
# ------------------------------------------------------------------------------
SENTRY_DSN = env('DJANGO_SENTRY_DSN')
SENTRY_CLIENT = env('DJANGO_SENTRY_CLIENT', default='raven.contrib.django.raven_compat.DjangoClient')
LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'root': {
        'level': 'WARNING',
        'handlers': ['sentry'],
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s '
                      '%(process)d %(thread)d %(message)s'
        },
    },
    'handlers': {
        'sentry': {
            'level': 'ERROR',
            'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        }
    },
    'loggers': {
        'django.db.backends': {
            'level': 'ERROR',
            'handlers': ['console'],
            'propagate': False,
        },
        'raven': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': False,
        },
        'sentry.errors': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': False,
        },
        'django.security.DisallowedHost': {
            'level': 'WARNING',
            'handlers': ['console', 'sentry'],
            'propagate': False,
        },
    },
}

SENTRY_CELERY_LOGLEVEL = env.int('DJANGO_SENTRY_LOG_LEVEL', logging.INFO)
RAVEN_CONFIG = {
    'dsn': SENTRY_DSN
}

Than using sentry-sdk I simply replaced it with:

import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration

sentry_sdk.init(
    dsn=SENTRY_DSN,
    integrations=[DjangoIntegration()]
)

@untitaker untitaker added Type: Bug Something isn't working needs-information and removed question labels Feb 24, 2019
@untitaker
Copy link
Member

Which version of Django and Python are you using?

@bluesurfer
Copy link
Author

bluesurfer commented Feb 24, 2019

@untitaker Django 2.1.7 with python 3.6.7.

Also I am using Channels

@untitaker untitaker changed the title Where are all the Raven default tags? No request data with django Apr 7, 2019
@bcollazo
Copy link

bcollazo commented May 22, 2019

I am also experiencing the same thing. Very similar setup to the one described by @bluesurfer. The Raven Python setup included a 'Cookies' section as well as better 'User' information (these both seem to be Django request object information).

I am using Django 1.11.20, Python 3.6.7 and was trying Sentry 0.8.0.

@bluesurfer
Copy link
Author

@bcollazo @untitaker I have some updates.

I am following cookiecutter-django repository to lay down my project. It seems that with recent updates (also from sentry-sdk) the following configuration to me is working:

# LOGGING
# ------------------------------------------------------------------------------
LOGGING = {
    "version": 1,
    "disable_existing_loggers": True,
    "formatters": {
        "verbose": {
            "format": "%(levelname)s %(asctime)s %(module)s "
                      "%(process)d %(thread)d %(message)s"
        }
    },
    "handlers": {
        "console": {
            "level": "DEBUG",
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        }
    },
    "loggers": {
        "django.db.backends": {
            "level": "ERROR",
            "handlers": ["console"],
            "propagate": False,
        },
        # Errors logged by the SDK itself
        "sentry_sdk": {"level": "ERROR", "handlers": ["console"], "propagate": False},
        "django.security.DisallowedHost": {
            "level": "ERROR",
            "handlers": ["console"],
            "propagate": False,
        },
    },
}

# Sentry
# ------------------------------------------------------------------------------
SENTRY_DSN = env("SENTRY_DSN")
SENTRY_LOG_LEVEL = env.int("DJANGO_SENTRY_LOG_LEVEL", logging.INFO)

sentry_logging = LoggingIntegration(
    level=SENTRY_LOG_LEVEL,  # Capture info and above as breadcrumbs
    event_level=logging.ERROR,  # Send no events from log messages
)

sentry_sdk.init(
    dsn=SENTRY_DSN,
    integrations=[sentry_logging, DjangoIntegration(), CeleryIntegration()],
)

Therefore to me this issue could be closed.

@untitaker
Copy link
Member

Ah that's neat, I wasn't aware of cookiecutter/cookiecutter-django#1820!

Tbh I think it's highly unlikely that @bluesurfer has the same actual problem as you, @bcollazo. Could you open a new issue with more information?

@anush0247
Copy link

We are also facing the same issue

We are not getting the Django request data info in sentry console with updated sentry SDK

With old raven Django client, there is no issue about getting request data in sentry

We are getting request data in sentry console, only if there is some exception raised before our program did access the request.body

We are getting can't read requst data once its already read error at sentry_sdk.integrations.django.DjangoRequestExtractor#raw_data

@untitaker
Copy link
Member

@anush0247 could you link an event that exhibits this behavior?

@anush0247
Copy link

@anush0247
Copy link

@untitaker any update ?

@untitaker
Copy link
Member

untitaker commented Jun 19, 2019 via email

@untitaker
Copy link
Member

PR at #393... I am pretty sure the issue is with django-rest-framework and not Django.

@untitaker
Copy link
Member

0.9.1 is out with this fix :) I hope it fixes all cases, but certainly should fix a few of them

@anush0247
Copy link

@untitaker I am getting circular settings import issue after upgrading to 0.9.1

please check below stack trace once

File ".venv/local/lib/python2.7/site-packages/sentry_sdk/hub.py", line 74, in init
    client = Client(*args, **kwargs)
  File ".venv/local/lib/python2.7/site-packages/sentry_sdk/client.py", line 88, in __init__
    options["integrations"], with_defaults=options["default_integrations"]
  File ".venv/local/lib/python2.7/site-packages/sentry_sdk/integrations/__init__.py", line 80, in setup_integrations
    type(integration).setup_once()
  File ".venv/local/lib/python2.7/site-packages/sentry_sdk/integrations/django/__init__.py", line 128, in setup_once
    from rest_framework.views import APIView  # type: ignore
  File ".venv/local/lib/python2.7/site-packages/rest_framework/views.py", line 18, in <module>
    from rest_framework import exceptions, status
  File ".venv/local/lib/python2.7/site-packages/rest_framework/exceptions.py", line 17, in <module>
    from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList
  File ".venv/local/lib/python2.7/site-packages/rest_framework/utils/serializer_helpers.py", line 9, in <module>
    from rest_framework.compat import unicode_to_repr
  File ".venv/local/lib/python2.7/site-packages/rest_framework/compat.py", line 156, in <module>
    from django.contrib.postgres import fields as postgres_fields
  File ".venv/local/lib/python2.7/site-packages/django/contrib/postgres/fields/__init__.py", line 1, in <module>
    from .array import *  # NOQA
  File ".venv/local/lib/python2.7/site-packages/django/contrib/postgres/fields/array.py", line 3, in <module>
    from django.contrib.postgres import lookups
  File ".venv/local/lib/python2.7/site-packages/django/contrib/postgres/lookups.py", line 4, in <module>
    from .search import SearchVector, SearchVectorExact, SearchVectorField
  File ".venv/local/lib/python2.7/site-packages/django/contrib/postgres/search.py", line 47, in <module>
    class SearchVector(SearchVectorCombinable, Func):
  File ".venv/local/lib/python2.7/site-packages/django/contrib/postgres/search.py", line 50, in SearchVector
    _output_field = SearchVectorField()
  File ".venv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 172, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File ".venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File ".venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File ".venv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 129, in __init__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

@untitaker
Copy link
Member

@anush0247 the issue has been reported as #397 and is fixed in 0.9.2 :)

@anush0247
Copy link

@untitaker thank you it's working now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants