diff --git a/swh/core/api/gunicorn_config.py b/swh/core/api/gunicorn_config.py --- a/swh/core/api/gunicorn_config.py +++ b/swh/core/api/gunicorn_config.py @@ -22,6 +22,7 @@ default_sentry_dsn=None, flask=True, sentry_integrations=None, + ignored_exceptions=(), extra_sentry_kwargs={}, ): # Initializes sentry as soon as possible in gunicorn's worker processes. @@ -36,4 +37,5 @@ default_sentry_dsn, integrations=sentry_integrations, extra_kwargs=extra_sentry_kwargs, + ignored_exceptions=ignored_exceptions, ) diff --git a/swh/core/api/tests/test_gunicorn.py b/swh/core/api/tests/test_gunicorn.py --- a/swh/core/api/tests/test_gunicorn.py +++ b/swh/core/api/tests/test_gunicorn.py @@ -1,4 +1,4 @@ -# Copyright (C) 2019 The Software Heritage developers +# Copyright (C) 2019-2022 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information @@ -27,12 +27,17 @@ with patch.dict(os.environ, {"SWH_SENTRY_DSN": "test_dsn"}): gunicorn_config.post_fork(None, None) + # before_send is obtained from functools.partial, and we cannot compare these + # with equality + before_send = sentry_sdk_init.call_args[1]["before_send"] + sentry_sdk_init.assert_called_once_with( dsn="test_dsn", integrations=[flask_integration], debug=False, release=None, environment=None, + before_send=before_send, ) @@ -54,12 +59,17 @@ version = pkg_resources.get_distribution("swh.core").version + # before_send is obtained from functools.partial, and we cannot compare these + # with equality + before_send = sentry_sdk_init.call_args[1]["before_send"] + sentry_sdk_init.assert_called_once_with( dsn="test_dsn", integrations=[flask_integration], debug=False, release="swh.core@" + version, environment="tests", + before_send=before_send, ) @@ -74,12 +84,17 @@ ): gunicorn_config.post_fork(None, None) + # before_send is obtained from functools.partial, and we cannot compare these + # with equality + before_send = sentry_sdk_init.call_args[1]["before_send"] + sentry_sdk_init.assert_called_once_with( dsn="test_dsn", integrations=[flask_integration], debug=True, release=None, environment=None, + before_send=before_send, ) @@ -88,12 +103,17 @@ with patch.dict(os.environ, {"SWH_SENTRY_DSN": "test_dsn"}): gunicorn_config.post_fork(None, None, flask=False) + # before_send is obtained from functools.partial, and we cannot compare these + # with equality + before_send = sentry_sdk_init.call_args[1]["before_send"] + sentry_sdk_init.assert_called_once_with( dsn="test_dsn", integrations=[], debug=False, release=None, environment=None, + before_send=before_send, ) @@ -111,6 +131,10 @@ extra_sentry_kwargs={"bar": "baz"}, ) + # before_send is obtained from functools.partial, and we cannot compare these + # with equality + before_send = sentry_sdk_init.call_args[1]["before_send"] + sentry_sdk_init.assert_called_once_with( dsn="test_dsn", integrations=["foo", flask_integration], @@ -118,4 +142,5 @@ bar="baz", release=None, environment=None, + before_send=before_send, ) diff --git a/swh/core/sentry.py b/swh/core/sentry.py --- a/swh/core/sentry.py +++ b/swh/core/sentry.py @@ -3,6 +3,7 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +import functools import os import pkg_resources @@ -17,7 +18,17 @@ return None -def init_sentry(sentry_dsn, *, debug=None, integrations=[], extra_kwargs={}): +def before_send(ignored_exceptions, event, hint): + if "exc_info" in hint: + exc_type, exc_value, tb = hint["exc_info"] + if isinstance(exc_value, ignored_exceptions): + return None + return event + + +def init_sentry( + sentry_dsn, *, debug=None, integrations=[], ignored_exceptions=(), extra_kwargs={} +): if debug is None: debug = bool(os.environ.get("SWH_SENTRY_DEBUG")) sentry_dsn = sentry_dsn or os.environ.get("SWH_SENTRY_DSN") @@ -32,5 +43,8 @@ dsn=sentry_dsn, integrations=integrations, debug=debug, + before_send=functools.partial( + before_send, ignored_exceptions=ignored_exceptions + ), **extra_kwargs, )