Skip to content


graypy crashing when passed a django WSGIRequest #6

severb opened this Issue · 8 comments

3 participants


stelios reported that

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

Traceback (most recent call last):
 File "/usr/lib/python2.6/logging/", line 541, in emit
   s = self.makePickle(record)
 File "/usr/local/lib/python2.6/dist-packages/graypy/",
line 28, in makePickle
   return zlib.compress(json.dumps(message_dict))
 File "/usr/lib/python2.6/json/", line 230, in dumps
   return _default_encoder.encode(obj)
 File "/usr/lib/python2.6/json/", line 367, in encode
   chunks = list(self.iterencode(o))
 File "/usr/lib/python2.6/json/", line 309, in _iterencode
   for chunk in self._iterencode_dict(o, markers):
 File "/usr/lib/python2.6/json/", line 275, in
   for chunk in self._iterencode(value, markers):
 File "/usr/lib/python2.6/json/", line 317, in _iterencode
   for chunk in self._iterencode_default(o, markers):
 File "/usr/lib/python2.6/json/", line 323, in
   newobj = self.default(o)
 File "/usr/lib/python2.6/json/", 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...


Any ideas for this?


I'm not sure how to reproduce this.


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


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


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('_'):
            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.


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.)


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


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

@severb severb closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.