prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  0.7.10 CeleryIntegration captures task Ignore exception
  Similar to #252, I've noticed that the Sentry client with the CeleryIntegration is capturing task Ignore exceptions, which are often used with dynamic tasks (see also https://github.com/celery/celery/issues/3437). I believe that since Retries are ignored, the `Ignore` exception should also be ignored.
  
  The exception in Sentry is showing `celery/app/task.py in replace at line 894`:
  ```python
  raise Ignore('Replaced by new task')
  ```
  
  celery: 4.3
  sentry-sdk: 0.7.10
  python: 3.6
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `sentry_sdk/integrations/celery.py`
  Content:
  ```
  1 from __future__ import absolute_import
  2 
  3 import sys
  4 
  5 from celery.exceptions import SoftTimeLimitExceeded, Retry  # type: ignore
  6 
  7 from sentry_sdk.hub import Hub
  8 from sentry_sdk.utils import capture_internal_exceptions, event_from_exception
  9 from sentry_sdk._compat import reraise
  10 from sentry_sdk.integrations import Integration
  11 from sentry_sdk.integrations.logging import ignore_logger
  12 
  13 
  14 class CeleryIntegration(Integration):
  15     identifier = "celery"
  16 
  17     @staticmethod
  18     def setup_once():
  19         import celery.app.trace as trace  # type: ignore
  20 
  21         old_build_tracer = trace.build_tracer
  22 
  23         def sentry_build_tracer(name, task, *args, **kwargs):
  24             # Need to patch both methods because older celery sometimes
  25             # short-circuits to task.run if it thinks it's safe.
  26             task.__call__ = _wrap_task_call(task, task.__call__)
  27             task.run = _wrap_task_call(task, task.run)
  28             return _wrap_tracer(task, old_build_tracer(name, task, *args, **kwargs))
  29 
  30         trace.build_tracer = sentry_build_tracer
  31 
  32         _patch_worker_exit()
  33 
  34         # This logger logs every status of every task that ran on the worker.
  35         # Meaning that every task's breadcrumbs are full of stuff like "Task
  36         # <foo> raised unexpected <bar>".
  37         ignore_logger("celery.worker.job")
  38 
  39 
  40 def _wrap_tracer(task, f):
  41     # Need to wrap tracer for pushing the scope before prerun is sent, and
  42     # popping it after postrun is sent.
  43     #
  44     # This is the reason we don't use signals for hooking in the first place.
  45     # Also because in Celery 3, signal dispatch returns early if one handler
  46     # crashes.
  47     def _inner(*args, **kwargs):
  48         hub = Hub.current
  49         if hub.get_integration(CeleryIntegration) is None:
  50             return f(*args, **kwargs)
  51 
  52         with hub.push_scope() as scope:
  53             scope._name = "celery"
  54             scope.clear_breadcrumbs()
  55             scope.add_event_processor(_make_event_processor(task, *args, **kwargs))
  56 
  57             return f(*args, **kwargs)
  58 
  59     return _inner
  60 
  61 
  62 def _wrap_task_call(task, f):
  63     # Need to wrap task call because the exception is caught before we get to
  64     # see it. Also celery's reported stacktrace is untrustworthy.
  65     def _inner(*args, **kwargs):
  66         try:
  67             return f(*args, **kwargs)
  68         except Exception:
  69             exc_info = sys.exc_info()
  70             with capture_internal_exceptions():
  71                 _capture_exception(task, exc_info)
  72             reraise(*exc_info)
  73 
  74     return _inner
  75 
  76 
  77 def _make_event_processor(task, uuid, args, kwargs, request=None):
  78     def event_processor(event, hint):
  79         with capture_internal_exceptions():
  80             event["transaction"] = task.name
  81 
  82         with capture_internal_exceptions():
  83             extra = event.setdefault("extra", {})
  84             extra["celery-job"] = {
  85                 "task_name": task.name,
  86                 "args": args,
  87                 "kwargs": kwargs,
  88             }
  89 
  90         if "exc_info" in hint:
  91             with capture_internal_exceptions():
  92                 if issubclass(hint["exc_info"][0], SoftTimeLimitExceeded):
  93                     event["fingerprint"] = [
  94                         "celery",
  95                         "SoftTimeLimitExceeded",
  96                         getattr(task, "name", task),
  97                     ]
  98 
  99         return event
  100 
  101     return event_processor
  102 
  103 
  104 def _capture_exception(task, exc_info):
  105     hub = Hub.current
  106 
  107     if hub.get_integration(CeleryIntegration) is None:
  108         return
  109     if isinstance(exc_info[1], Retry):
  110         return
  111     if hasattr(task, "throws") and isinstance(exc_info[1], task.throws):
  112         return
  113 
  114     event, hint = event_from_exception(
  115         exc_info,
  116         client_options=hub.client.options,
  117         mechanism={"type": "celery", "handled": False},
  118     )
  119 
  120     hub.capture_event(event, hint=hint)
  121 
  122 
  123 def _patch_worker_exit():
  124     # Need to flush queue before worker shutdown because a crashing worker will
  125     # call os._exit
  126     from billiard.pool import Worker  # type: ignore
  127 
  128     old_workloop = Worker.workloop
  129 
  130     def sentry_workloop(*args, **kwargs):
  131         try:
  132             return old_workloop(*args, **kwargs)
  133         finally:
  134             with capture_internal_exceptions():
  135                 hub = Hub.current
  136                 if hub.get_integration(CeleryIntegration) is not None:
  137                     hub.flush()
  138 
  139     Worker.workloop = sentry_workloop
  140
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/sentry_sdk/integrations/celery.py b/sentry_sdk/integrations/celery.py
  --- a/sentry_sdk/integrations/celery.py
  +++ b/sentry_sdk/integrations/celery.py
  @@ -2,7 +2,12 @@
   
   import sys
   
  -from celery.exceptions import SoftTimeLimitExceeded, Retry  # type: ignore
  +from celery.exceptions import (  # type: ignore
  +    SoftTimeLimitExceeded,
  +    Retry,
  +    Ignore,
  +    Reject,
  +)
   
   from sentry_sdk.hub import Hub
   from sentry_sdk.utils import capture_internal_exceptions, event_from_exception
  @@ -11,6 +16,9 @@
   from sentry_sdk.integrations.logging import ignore_logger
   
   
  +CELERY_CONTROL_FLOW_EXCEPTIONS = (Retry, Ignore, Reject)
  +
  +
   class CeleryIntegration(Integration):
       identifier = "celery"
   
  @@ -106,7 +114,7 @@
   
       if hub.get_integration(CeleryIntegration) is None:
           return
  -    if isinstance(exc_info[1], Retry):
  +    if isinstance(exc_info[1], CELERY_CONTROL_FLOW_EXCEPTIONS):
           return
       if hasattr(task, "throws") and isinstance(exc_info[1], task.throws):
           return
  
verification_info: {"golden_diff": "diff --git a/sentry_sdk/integrations/celery.py b/sentry_sdk/integrations/celery.py\n--- a/sentry_sdk/integrations/celery.py\n+++ b/sentry_sdk/integrations/celery.py\n@@ -2,7 +2,12 @@\n \n import sys\n \n-from celery.exceptions import SoftTimeLimitExceeded, Retry  # type: ignore\n+from celery.exceptions import (  # type: ignore\n+    SoftTimeLimitExceeded,\n+    Retry,\n+    Ignore,\n+    Reject,\n+)\n \n from sentry_sdk.hub import Hub\n from sentry_sdk.utils import capture_internal_exceptions, event_from_exception\n@@ -11,6 +16,9 @@\n from sentry_sdk.integrations.logging import ignore_logger\n \n \n+CELERY_CONTROL_FLOW_EXCEPTIONS = (Retry, Ignore, Reject)\n+\n+\n class CeleryIntegration(Integration):\n     identifier = \"celery\"\n \n@@ -106,7 +114,7 @@\n \n     if hub.get_integration(CeleryIntegration) is None:\n         return\n-    if isinstance(exc_info[1], Retry):\n+    if isinstance(exc_info[1], CELERY_CONTROL_FLOW_EXCEPTIONS):\n         return\n     if hasattr(task, \"throws\") and isinstance(exc_info[1], task.throws):\n         return\n", "issue": "0.7.10 CeleryIntegration captures task Ignore exception\nSimilar to #252, I've noticed that the Sentry client with the CeleryIntegration is capturing task Ignore exceptions, which are often used with dynamic tasks (see also https://github.com/celery/celery/issues/3437). I believe that since Retries are ignored, the `Ignore` exception should also be ignored.\r\n\r\nThe exception in Sentry is showing `celery/app/task.py in replace at line 894`:\r\n```python\r\nraise Ignore('Replaced by new task')\r\n```\r\n\r\ncelery: 4.3\r\nsentry-sdk: 0.7.10\r\npython: 3.6\n", "before_files": [{"content": "from __future__ import absolute_import\n\nimport sys\n\nfrom celery.exceptions import SoftTimeLimitExceeded, Retry  # type: ignore\n\nfrom sentry_sdk.hub import Hub\nfrom sentry_sdk.utils import capture_internal_exceptions, event_from_exception\nfrom sentry_sdk._compat import reraise\nfrom sentry_sdk.integrations import Integration\nfrom sentry_sdk.integrations.logging import ignore_logger\n\n\nclass CeleryIntegration(Integration):\n    identifier = \"celery\"\n\n    @staticmethod\n    def setup_once():\n        import celery.app.trace as trace  # type: ignore\n\n        old_build_tracer = trace.build_tracer\n\n        def sentry_build_tracer(name, task, *args, **kwargs):\n            # Need to patch both methods because older celery sometimes\n            # short-circuits to task.run if it thinks it's safe.\n            task.__call__ = _wrap_task_call(task, task.__call__)\n            task.run = _wrap_task_call(task, task.run)\n            return _wrap_tracer(task, old_build_tracer(name, task, *args, **kwargs))\n\n        trace.build_tracer = sentry_build_tracer\n\n        _patch_worker_exit()\n\n        # This logger logs every status of every task that ran on the worker.\n        # Meaning that every task's breadcrumbs are full of stuff like \"Task\n        # <foo> raised unexpected <bar>\".\n        ignore_logger(\"celery.worker.job\")\n\n\ndef _wrap_tracer(task, f):\n    # Need to wrap tracer for pushing the scope before prerun is sent, and\n    # popping it after postrun is sent.\n    #\n    # This is the reason we don't use signals for hooking in the first place.\n    # Also because in Celery 3, signal dispatch returns early if one handler\n    # crashes.\n    def _inner(*args, **kwargs):\n        hub = Hub.current\n        if hub.get_integration(CeleryIntegration) is None:\n            return f(*args, **kwargs)\n\n        with hub.push_scope() as scope:\n            scope._name = \"celery\"\n            scope.clear_breadcrumbs()\n            scope.add_event_processor(_make_event_processor(task, *args, **kwargs))\n\n            return f(*args, **kwargs)\n\n    return _inner\n\n\ndef _wrap_task_call(task, f):\n    # Need to wrap task call because the exception is caught before we get to\n    # see it. Also celery's reported stacktrace is untrustworthy.\n    def _inner(*args, **kwargs):\n        try:\n            return f(*args, **kwargs)\n        except Exception:\n            exc_info = sys.exc_info()\n            with capture_internal_exceptions():\n                _capture_exception(task, exc_info)\n            reraise(*exc_info)\n\n    return _inner\n\n\ndef _make_event_processor(task, uuid, args, kwargs, request=None):\n    def event_processor(event, hint):\n        with capture_internal_exceptions():\n            event[\"transaction\"] = task.name\n\n        with capture_internal_exceptions():\n            extra = event.setdefault(\"extra\", {})\n            extra[\"celery-job\"] = {\n                \"task_name\": task.name,\n                \"args\": args,\n                \"kwargs\": kwargs,\n            }\n\n        if \"exc_info\" in hint:\n            with capture_internal_exceptions():\n                if issubclass(hint[\"exc_info\"][0], SoftTimeLimitExceeded):\n                    event[\"fingerprint\"] = [\n                        \"celery\",\n                        \"SoftTimeLimitExceeded\",\n                        getattr(task, \"name\", task),\n                    ]\n\n        return event\n\n    return event_processor\n\n\ndef _capture_exception(task, exc_info):\n    hub = Hub.current\n\n    if hub.get_integration(CeleryIntegration) is None:\n        return\n    if isinstance(exc_info[1], Retry):\n        return\n    if hasattr(task, \"throws\") and isinstance(exc_info[1], task.throws):\n        return\n\n    event, hint = event_from_exception(\n        exc_info,\n        client_options=hub.client.options,\n        mechanism={\"type\": \"celery\", \"handled\": False},\n    )\n\n    hub.capture_event(event, hint=hint)\n\n\ndef _patch_worker_exit():\n    # Need to flush queue before worker shutdown because a crashing worker will\n    # call os._exit\n    from billiard.pool import Worker  # type: ignore\n\n    old_workloop = Worker.workloop\n\n    def sentry_workloop(*args, **kwargs):\n        try:\n            return old_workloop(*args, **kwargs)\n        finally:\n            with capture_internal_exceptions():\n                hub = Hub.current\n                if hub.get_integration(CeleryIntegration) is not None:\n                    hub.flush()\n\n    Worker.workloop = sentry_workloop\n", "path": "sentry_sdk/integrations/celery.py"}], "after_files": [{"content": "from __future__ import absolute_import\n\nimport sys\n\nfrom celery.exceptions import (  # type: ignore\n    SoftTimeLimitExceeded,\n    Retry,\n    Ignore,\n    Reject,\n)\n\nfrom sentry_sdk.hub import Hub\nfrom sentry_sdk.utils import capture_internal_exceptions, event_from_exception\nfrom sentry_sdk._compat import reraise\nfrom sentry_sdk.integrations import Integration\nfrom sentry_sdk.integrations.logging import ignore_logger\n\n\nCELERY_CONTROL_FLOW_EXCEPTIONS = (Retry, Ignore, Reject)\n\n\nclass CeleryIntegration(Integration):\n    identifier = \"celery\"\n\n    @staticmethod\n    def setup_once():\n        import celery.app.trace as trace  # type: ignore\n\n        old_build_tracer = trace.build_tracer\n\n        def sentry_build_tracer(name, task, *args, **kwargs):\n            # Need to patch both methods because older celery sometimes\n            # short-circuits to task.run if it thinks it's safe.\n            task.__call__ = _wrap_task_call(task, task.__call__)\n            task.run = _wrap_task_call(task, task.run)\n            return _wrap_tracer(task, old_build_tracer(name, task, *args, **kwargs))\n\n        trace.build_tracer = sentry_build_tracer\n\n        _patch_worker_exit()\n\n        # This logger logs every status of every task that ran on the worker.\n        # Meaning that every task's breadcrumbs are full of stuff like \"Task\n        # <foo> raised unexpected <bar>\".\n        ignore_logger(\"celery.worker.job\")\n\n\ndef _wrap_tracer(task, f):\n    # Need to wrap tracer for pushing the scope before prerun is sent, and\n    # popping it after postrun is sent.\n    #\n    # This is the reason we don't use signals for hooking in the first place.\n    # Also because in Celery 3, signal dispatch returns early if one handler\n    # crashes.\n    def _inner(*args, **kwargs):\n        hub = Hub.current\n        if hub.get_integration(CeleryIntegration) is None:\n            return f(*args, **kwargs)\n\n        with hub.push_scope() as scope:\n            scope._name = \"celery\"\n            scope.clear_breadcrumbs()\n            scope.add_event_processor(_make_event_processor(task, *args, **kwargs))\n\n            return f(*args, **kwargs)\n\n    return _inner\n\n\ndef _wrap_task_call(task, f):\n    # Need to wrap task call because the exception is caught before we get to\n    # see it. Also celery's reported stacktrace is untrustworthy.\n    def _inner(*args, **kwargs):\n        try:\n            return f(*args, **kwargs)\n        except Exception:\n            exc_info = sys.exc_info()\n            with capture_internal_exceptions():\n                _capture_exception(task, exc_info)\n            reraise(*exc_info)\n\n    return _inner\n\n\ndef _make_event_processor(task, uuid, args, kwargs, request=None):\n    def event_processor(event, hint):\n        with capture_internal_exceptions():\n            event[\"transaction\"] = task.name\n\n        with capture_internal_exceptions():\n            extra = event.setdefault(\"extra\", {})\n            extra[\"celery-job\"] = {\n                \"task_name\": task.name,\n                \"args\": args,\n                \"kwargs\": kwargs,\n            }\n\n        if \"exc_info\" in hint:\n            with capture_internal_exceptions():\n                if issubclass(hint[\"exc_info\"][0], SoftTimeLimitExceeded):\n                    event[\"fingerprint\"] = [\n                        \"celery\",\n                        \"SoftTimeLimitExceeded\",\n                        getattr(task, \"name\", task),\n                    ]\n\n        return event\n\n    return event_processor\n\n\ndef _capture_exception(task, exc_info):\n    hub = Hub.current\n\n    if hub.get_integration(CeleryIntegration) is None:\n        return\n    if isinstance(exc_info[1], CELERY_CONTROL_FLOW_EXCEPTIONS):\n        return\n    if hasattr(task, \"throws\") and isinstance(exc_info[1], task.throws):\n        return\n\n    event, hint = event_from_exception(\n        exc_info,\n        client_options=hub.client.options,\n        mechanism={\"type\": \"celery\", \"handled\": False},\n    )\n\n    hub.capture_event(event, hint=hint)\n\n\ndef _patch_worker_exit():\n    # Need to flush queue before worker shutdown because a crashing worker will\n    # call os._exit\n    from billiard.pool import Worker  # type: ignore\n\n    old_workloop = Worker.workloop\n\n    def sentry_workloop(*args, **kwargs):\n        try:\n            return old_workloop(*args, **kwargs)\n        finally:\n            with capture_internal_exceptions():\n                hub = Hub.current\n                if hub.get_integration(CeleryIntegration) is not None:\n                    hub.flush()\n\n    Worker.workloop = sentry_workloop\n", "path": "sentry_sdk/integrations/celery.py"}]}