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

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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