getsentry / sentry

Developer-first error tracking and performance monitoring
https://sentry.io
Other
39.18k stars 4.2k forks source link

Timezone issue with Django 1.4 and Sentry #292

Closed victorhooi closed 11 years ago

victorhooi commented 12 years ago

Hi,

I'm attempting to run Django Sentry on Django 1.4 (alpha), and I seem to be hitting an issue with timezone awareness.

Originally, when I ran it, I got the rather nondescript message:

    No handlers could be found for logger "sentry.errors.client"

As per the advice here (https://groups.google.com/d/topic/disqus-opensource/kULFXm42eQg/discussion), I added the following to my settings.py:

    import logging
    logger = logging.getLogger('sentry.errors')
    logger.setLevel(logging.INFO)
    logger.addHandler(logging.StreamHandler())

I then get the following on stderr:

    Unable to process log entry: can't subtract offset-naive and offset-aware datetimes
    Traceback (most recent call last):
      File "/home/victorhooi/.virtualenvs/aegnexus/src/sentry/sentry/manager.py", line 321, in from_kwargs
        group, is_new, is_sample = self._create_group(event, **group_kwargs)
      File "/home/victorhooi/.virtualenvs/aegnexus/src/sentry/sentry/manager.py", line 365, in _create_group
        silence_timedelta = date - group.last_seen
    TypeError: can't subtract offset-naive and offset-aware datetimes

Cheers, Victor

dcramer commented 12 years ago

If you can possibly write a test case for me I'll gladly fix this, but I haven't quite figured out how this happens yet.

(e.g. if I pass a tz aware datetime into from_kwargs it works just as well as a non-tz aware dt)

Also is this in Sentry 2 or still 1.x?

dcramer commented 12 years ago

Nevermind TB shows its 2.0 :)

victorhooi commented 12 years ago

Hi,

Yup, this is Sentry 2.0 on Django 1.4 (Alpha).

These are the steps I take to replicate the issue:

  1. Create a new virtualenv, and install Django-trunk, Sentry, and django-south
  2. Create a new Django project/app with "django-admin.py startproject testsentry"
  3. Edit settings.py, as per https://gist.github.com/1581222
  4. Run "./manage.py syncdb"
  5. Run "./manage.py migrate"
  6. Run "./manage.py runserver".

I then try to access a garbage URL on my app to trigger an error.

With "DEBUG = False" in settings.py, I get the normal Django debugging stack trace for the 404. however, if I then try to change it to "DEBUG = True", I get the following output on stdout:

https://gist.github.com/1581217

I also noticed that during the migration, I got the following error about receiving native datetime, whilst time-zone support is active:

http://gist.github.com/1581249

Any idea on what might be happening?

Cheers, Victor

Kronuz commented 12 years ago

The problem comes from date = kwargs.pop('timestamp', None) or datetime.datetime.now() in sentry.manager.GroupManager.from_kwargs(), it shouldn't use datetime.datetime.now() but timezone.now() instead... timestamp in kwargs should also be a timezone aware datetime, but it's being created by process_data_timestamp() in sentry.coreapi. Basically, sentry needs to use timezone.now() everywhere where it currently uses datetime.datetime.now(), and also, where it creates datetime objects (like in process_data_timestamp()) it needs to create timezone aware datetimes, otherwise django complains

dcramer commented 12 years ago

I'm wondering if there's a way we can solve this in the bigger picture.

I'm actually seeing issues in 1.3 currently with timezones and them being off. Here's what I'm thinking we'll do:

  1. All clients still must send UTC timestamps.
  2. The server coerces (and uses) tz-aware datetimes everywhere.
  3. For compatibility, we'll change all of our DateTime database fields to coerce to tz-aware datetimes.

Thoughts?

dcramer commented 12 years ago

Rethinking this based on feedback. Currently implementation is all clients send in UTC (no TZ), server uses UTC everywhere (no TZ), and at some point, we'll fix USE_TZ scenarios.

tadeck commented 12 years ago

I have the same / similar issue. Details say:

TypeError at /sentry/
can't compare offset-naive and offset-aware datetimes

Exception Location: /usr/local/lib/python2.7/dist-packages/sentry/templatetags/sentry_helpers.py
    in timesince, line 143

I am using Sentry 1.3.5.

tadeck commented 12 years ago

@dcramer: Any updates regarding fixing USE_TZ scenarios? I would like to use django-sentry for the project, but since the project is based on Django 1.4 and uses time zones, I am unable to do that.

dcramer commented 12 years ago

You don't need to use Django 1.4 to use Sentry (in fact, we don't even officially support it). If you're running it as a client/server, things will just work.

heynemann commented 11 years ago

Guessing this can be safely closed.