graypy crashing when passed a django WSGIRequest #6

Closed
severb opened this Issue Jan 30, 2012 · 8 comments

Projects

None yet

3 participants

Owner
severb commented Jan 30, 2012

stelios reported that

When django throws an error 500 graypy throws TypeError when doing
json.dumps.
Here's (part of) the stacktrace:

Traceback (most recent call last):
 File "/usr/lib/python2.6/logging/handlers.py", line 541, in emit
   s = self.makePickle(record)
 File "/usr/local/lib/python2.6/dist-packages/graypy/handler.py",
line 28, in makePickle
   return zlib.compress(json.dumps(message_dict))
 File "/usr/lib/python2.6/json/__init__.py", line 230, in dumps
   return _default_encoder.encode(obj)
 File "/usr/lib/python2.6/json/encoder.py", line 367, in encode
   chunks = list(self.iterencode(o))
 File "/usr/lib/python2.6/json/encoder.py", line 309, in _iterencode
   for chunk in self._iterencode_dict(o, markers):
 File "/usr/lib/python2.6/json/encoder.py", line 275, in
_iterencode_dict
   for chunk in self._iterencode(value, markers):
 File "/usr/lib/python2.6/json/encoder.py", line 317, in _iterencode
   for chunk in self._iterencode_default(o, markers):
 File "/usr/lib/python2.6/json/encoder.py", line 323, in
_iterencode_default
   newobj = self.default(o)
 File "/usr/lib/python2.6/json/encoder.py", line 344, in default
   raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <WSGIRequest

This is "normal" since WSGIRequest is not JSON serializable but how
can you then send error 500 errors from
Django to graylog2. Hope I'm not missing something obvious...

Contributor

Any ideas for this?

Owner
severb commented Oct 18, 2012

I'm not sure how to reproduce this.

Contributor

Just have a view throw a 500 and then trigger that (and set the logger django.request's handler to graypy.GELFHandler)

Owner
severb commented Oct 18, 2012

I'll take a look. Do you have any ideas how this should be handled?

Contributor

I'm not quite sure. I did the following in your add_extra_fields function:

for key, value in record.__dict__.items():
    if key not in skip_list and not key.startswith('_'):
        try:
            json.dumps(value)  # will check if serializable
            message_dict['_%s' % key] = value
        except TypeError:
            message_dict['_%s' % key] = str(value)

return message_dict

But that might slow down things a bit. I think the proper way to handle this might actually be to create an error middleware in Django to catch the error and log what you need.

lethain commented Nov 12, 2012

I can confirm that @domino14 's fix is working for us in our django v1.4 deploy. (Although not a point of pride, we're monkeypatching the function graypy.handlers.add_extra_fields with the implementation in the above comment.)

Owner
severb commented Nov 13, 2012

Ok, a fix for this will be included soon. Thanks!

Owner
severb commented Nov 13, 2012

Thanks @domino14 and @lethain for helping me get to the bottom of this.

@severb severb closed this Nov 13, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment