$38 GRAYBYTE WORDPRESS FILE MANAGER $68

SERVER : premium201.web-hosting.com #1 SMP Wed Mar 26 12:08:09 UTC 2025
SERVER IP : 172.67.217.254 | ADMIN IP 216.73.216.157
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : mail

/opt/alt/python38/lib/python3.8/site-packages/sentry_sdk/integrations/

HOME
Current File : /opt/alt/python38/lib/python3.8/site-packages/sentry_sdk/integrations//celery.py
from __future__ import absolute_import

import sys

from celery.exceptions import SoftTimeLimitExceeded, Retry  # type: ignore

from sentry_sdk.hub import Hub
from sentry_sdk.utils import capture_internal_exceptions, event_from_exception
from sentry_sdk._compat import reraise
from sentry_sdk.integrations import Integration
from sentry_sdk.integrations.logging import ignore_logger


class CeleryIntegration(Integration):
    identifier = "celery"

    @staticmethod
    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")


def _wrap_tracer(task, f):
    # Need to wrap tracer for pushing the scope before prerun is sent, and
    # popping it after postrun is sent.
    #
    # This is the reason we don't use signals for hooking in the first place.
    # Also because in Celery 3, signal dispatch returns early if one handler
    # crashes.
    def _inner(*args, **kwargs):
        hub = Hub.current
        if hub.get_integration(CeleryIntegration) is None:
            return f(*args, **kwargs)

        with hub.push_scope() as scope:
            scope._name = "celery"
            scope.clear_breadcrumbs()
            scope.add_event_processor(_make_event_processor(task, *args, **kwargs))

            return f(*args, **kwargs)

    return _inner


def _wrap_task_call(task, f):
    # Need to wrap task call because the exception is caught before we get to
    # see it. Also celery's reported stacktrace is untrustworthy.
    def _inner(*args, **kwargs):
        try:
            return f(*args, **kwargs)
        except Exception:
            exc_info = sys.exc_info()
            with capture_internal_exceptions():
                _capture_exception(task, exc_info)
            reraise(*exc_info)

    return _inner


def _make_event_processor(task, uuid, args, kwargs, request=None):
    def event_processor(event, hint):
        with capture_internal_exceptions():
            event["transaction"] = task.name

        with capture_internal_exceptions():
            extra = event.setdefault("extra", {})
            extra["celery-job"] = {
                "task_name": task.name,
                "args": args,
                "kwargs": kwargs,
            }

        if "exc_info" in hint:
            with capture_internal_exceptions():
                if issubclass(hint["exc_info"][0], SoftTimeLimitExceeded):
                    event["fingerprint"] = [
                        "celery",
                        "SoftTimeLimitExceeded",
                        getattr(task, "name", task),
                    ]

        return event

    return event_processor


def _capture_exception(task, exc_info):
    hub = Hub.current

    if hub.get_integration(CeleryIntegration) is None:
        return
    if isinstance(exc_info[1], Retry):
        return
    if hasattr(task, "throws") and isinstance(exc_info[1], task.throws):
        return

    event, hint = event_from_exception(
        exc_info,
        client_options=hub.client.options,
        mechanism={"type": "celery", "handled": False},
    )

    hub.capture_event(event, hint=hint)


def _patch_worker_exit():
    # Need to flush queue before worker shutdown because a crashing worker will
    # call os._exit
    from billiard.pool import Worker  # type: ignore

    old_workloop = Worker.workloop

    def sentry_workloop(*args, **kwargs):
        try:
            return old_workloop(*args, **kwargs)
        finally:
            with capture_internal_exceptions():
                hub = Hub.current
                if hub.get_integration(CeleryIntegration) is not None:
                    hub.flush()

    Worker.workloop = sentry_workloop


Current_dir [ NOT WRITEABLE ] Document_root [ NOT WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
21 Mar 2024 10.24 AM
root / linksafe
0755
__pycache__
--
21 Mar 2024 10.24 AM
root / linksafe
0755
django
--
21 Mar 2024 10.24 AM
root / linksafe
0755
__init__.py
4.137 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
_wsgi_common.py
3.58 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
aiohttp.py
3.798 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
argv.py
0.873 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
atexit.py
1.455 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
aws_lambda.py
6.6 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
bottle.py
5.397 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
celery.py
4.319 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
dedupe.py
1.06 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
excepthook.py
1.304 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
flask.py
7.102 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
gnu_backtrace.py
2.938 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
logging.py
5.878 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
modules.py
1.21 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
pyramid.py
5.775 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
rq.py
3.196 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
sanic.py
6.899 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
serverless.py
1.209 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
stdlib.py
1.825 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
threading.py
1.56 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
tornado.py
6.237 KB
5 Apr 2019 11.15 PM
root / linksafe
0644
wsgi.py
7.128 KB
5 Apr 2019 11.15 PM
root / linksafe
0644

GRAYBYTE WORDPRESS FILE MANAGER @ 2025 CONTACT ME
Static GIF