opbeat_python icon indicating copy to clipboard operation
opbeat_python copied to clipboard

Recipe: Capture exceptions during celery/kombu's serialization

Open michael-k opened this issue 8 years ago • 0 comments

Example for pickle; works similar for JSON, YAML, …

def capture_serialization_exceptions():
    from pickle import dumps

    from kombu import serialization
    from kombu.exceptions import DecodeError
    from opbeat.contrib.django.models import client

    def loads(s):
        try:
            return serialization.unpickle(s)
        except LookupError as e:
            client.capture_exception()
            raise
        except DecodeError as e:
            client.capture_exception()
            raise
        except Exception as e:
            client.capture_exception()
            raise

    # See https://github.com/celery/kombu/blob/v3.0.37/kombu/serialization.py#L342-L360
    serialization.register(
        name='my_pickle',
        encoder=dumps,
        decoder=loads,
        content_type='application/x-python-serialize',
        content_encoding='binary',
    )

I did not figure out where to put this. If you can point me to a file, I'm happy to open a PR.

LookupError` might be enough, but we did not care enough to investigate. If someone from opbeat wants to see a logged example (closed source project; with a LookupError), I'm happy to provide a link via email.

We call the functions when doing https://opbeat.com/docs/articles/get-started-with-django/#logging-celery-tasks

michael-k avatar Aug 11 '17 14:08 michael-k