# Buggy code
def setup_once():
    import celery.app.trace as trace  # type: ignore

    old_build_tracer = trace.build_tracer

    def sentry_build_tracer(name, task, *args, **kwargs):
        # Need to patch both methods because older celery sometimes
        # short-circuits to task.run if it thinks it's safe.
        task.__call__ = _wrap_task_call(task.__call__)
        task.run = _wrap_task_call(task.run)
        return _wrap_tracer(task, old_build_tracer(name, task, *args, **kwargs))

    trace.build_tracer = sentry_build_tracer

    # This logger logs every status of every task that ran on the worker.
    # Meaning that every task's breadcrumbs are full of stuff like "Task
    # <foo> raised unexpected <bar>".
    ignore_logger("celery.worker.job")


# Traceback (RuntimeError)
[2019-03-08 21:24:21,117: ERROR/ForkPoolWorker-31] Task simple_task[d6e959b1-7253-4e55-861d-c1968ae14e1c] raised unexpected: RuntimeError('No active exception to reraise')
Traceback (most recent call last):
File "/Users/okomarov/.virtualenvs/myenv/lib/python3.7/site-packages/celery/app/trace.py", line 382, in trace_task
R = retval = fun(*args, **kwargs)
File "/Users/okomarov/Documents/repos/myproject/web/celery_factory.py", line 28, in __call__
return TaskBase.__call__(self, *args, **kwargs)
File "/Users/okomarov/.virtualenvs/myenv/lib/python3.7/site-packages/celery/app/trace.py", line 641, in __protected_call__
return self.run(*args, **kwargs)
File "/Users/okomarov/.virtualenvs/myenv/lib/python3.7/site-packages/sentry_sdk/integrations/celery.py", line 66, in _inner
reraise(*_capture_exception())
File "/Users/okomarov/.virtualenvs/myenv/lib/python3.7/site-packages/sentry_sdk/_compat.py", line 52, in reraise
raise value
File "/Users/okomarov/.virtualenvs/myenv/lib/python3.7/site-packages/sentry_sdk/integrations/celery.py", line 64, in _inner
return f(*args, **kwargs)
File "/Users/okomarov/.virtualenvs/myenv/lib/python3.7/site-packages/sentry_sdk/integrations/celery.py", line 66, in _inner
reraise(*_capture_exception())
File "/Users/okomarov/.virtualenvs/myenv/lib/python3.7/site-packages/sentry_sdk/_compat.py", line 52, in reraise
raise value
File "/Users/okomarov/.virtualenvs/myenv/lib/python3.7/site-packages/sentry_sdk/integrations/celery.py", line 64, in _inner
return f(*args, **kwargs)
File "/Users/okomarov/Documents/repos/myproject/web/simple_task.py", line 4, in simple_task
raise Exception("Testing Celery exception")
RuntimeError: No active exception to reraise

# Fixed code
def setup_once():
    import celery.app.trace as trace  # type: ignore

    old_build_tracer = trace.build_tracer

    def sentry_build_tracer(name, task, *args, **kwargs):
        # Need to patch both methods because older celery sometimes
        # short-circuits to task.run if it thinks it's safe.
        task.__call__ = _wrap_task_call(task, task.__call__)
        task.run = _wrap_task_call(task, task.run)
        return _wrap_tracer(task, old_build_tracer(name, task, *args, **kwargs))

    trace.build_tracer = sentry_build_tracer

    _patch_worker_exit()

    # This logger logs every status of every task that ran on the worker.
    # Meaning that every task's breadcrumbs are full of stuff like "Task
    # <foo> raised unexpected <bar>".
    ignore_logger("celery.worker.job")