Skip to content
This repository was archived by the owner on Jun 11, 2018. It is now read-only.
This repository was archived by the owner on Jun 11, 2018. It is now read-only.

Recipe: Capture exceptions during celery/kombu's serialization #179

@michael-k

Description

@michael-k

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions