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,7 +22,8 @@ default_sentry_dsn=None, flask=True, sentry_integrations=None, - extra_sentry_kwargs={}, + extra_sentry_kwargs=None, + disable_logging_event_reporting=True, ): # 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, + disable_logging_event_reporting=disable_logging_event_reporting, ) 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 @@ -20,16 +20,22 @@ def test_post_fork_with_dsn_env(): flask_integration = object() # unique object to check for equality + logging_integration = object() # unique object to check for equality with patch( "sentry_sdk.integrations.flask.FlaskIntegration", new=lambda: flask_integration + ), patch( + "sentry_sdk.integrations.logging.LoggingIntegration", + new=lambda level, event_level: logging_integration, + ), patch( + "sentry_sdk.init" + ) as sentry_sdk_init, patch.dict( + os.environ, {"SWH_SENTRY_DSN": "test_dsn"} ): - with patch("sentry_sdk.init") as sentry_sdk_init: - with patch.dict(os.environ, {"SWH_SENTRY_DSN": "test_dsn"}): - gunicorn_config.post_fork(None, None) + gunicorn_config.post_fork(None, None) sentry_sdk_init.assert_called_once_with( dsn="test_dsn", - integrations=[flask_integration], + integrations=[flask_integration, logging_integration], debug=False, release=None, environment=None, @@ -37,26 +43,31 @@ def test_post_fork_with_package_env(): - flask_integration = object() # unique object to check for equality + flask_integration = object() + logging_integration = object() + with patch( "sentry_sdk.integrations.flask.FlaskIntegration", new=lambda: flask_integration + ), patch( + "sentry_sdk.integrations.logging.LoggingIntegration", + new=lambda level, event_level: logging_integration, + ), patch( + "sentry_sdk.init" + ) as sentry_sdk_init, patch.dict( + os.environ, + { + "SWH_SENTRY_DSN": "test_dsn", + "SWH_SENTRY_ENVIRONMENT": "tests", + "SWH_MAIN_PACKAGE": "swh.core", + }, ): - with patch("sentry_sdk.init") as sentry_sdk_init: - with patch.dict( - os.environ, - { - "SWH_SENTRY_DSN": "test_dsn", - "SWH_SENTRY_ENVIRONMENT": "tests", - "SWH_MAIN_PACKAGE": "swh.core", - }, - ): - gunicorn_config.post_fork(None, None) + gunicorn_config.post_fork(None, None) version = pkg_resources.get_distribution("swh.core").version sentry_sdk_init.assert_called_once_with( dsn="test_dsn", - integrations=[flask_integration], + integrations=[flask_integration, logging_integration], debug=False, release="swh.core@" + version, environment="tests", @@ -64,19 +75,24 @@ def test_post_fork_debug(): - flask_integration = object() # unique object to check for equality + flask_integration = object() + logging_integration = object() + with patch( "sentry_sdk.integrations.flask.FlaskIntegration", new=lambda: flask_integration + ), patch( + "sentry_sdk.integrations.logging.LoggingIntegration", + new=lambda level, event_level: logging_integration, + ), patch( + "sentry_sdk.init" + ) as sentry_sdk_init, patch.dict( + os.environ, {"SWH_SENTRY_DSN": "test_dsn", "SWH_SENTRY_DEBUG": "1"} ): - with patch("sentry_sdk.init") as sentry_sdk_init: - with patch.dict( - os.environ, {"SWH_SENTRY_DSN": "test_dsn", "SWH_SENTRY_DEBUG": "1"} - ): - gunicorn_config.post_fork(None, None) + gunicorn_config.post_fork(None, None) sentry_sdk_init.assert_called_once_with( dsn="test_dsn", - integrations=[flask_integration], + integrations=[flask_integration, logging_integration], debug=True, release=None, environment=None, @@ -84,13 +100,17 @@ def test_post_fork_no_flask(): - with patch("sentry_sdk.init") as sentry_sdk_init: - with patch.dict(os.environ, {"SWH_SENTRY_DSN": "test_dsn"}): - gunicorn_config.post_fork(None, None, flask=False) + logging_integration = object() + + with patch("sentry_sdk.init") as sentry_sdk_init, patch( + "sentry_sdk.integrations.logging.LoggingIntegration", + new=lambda level, event_level: logging_integration, + ), patch.dict(os.environ, {"SWH_SENTRY_DSN": "test_dsn"}): + gunicorn_config.post_fork(None, None, flask=False) sentry_sdk_init.assert_called_once_with( dsn="test_dsn", - integrations=[], + integrations=[logging_integration], debug=False, release=None, environment=None, @@ -109,6 +129,7 @@ None, sentry_integrations=["foo"], extra_sentry_kwargs={"bar": "baz"}, + disable_logging_event_reporting=False, ) sentry_sdk_init.assert_called_once_with( 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 logging import os import pkg_resources @@ -17,7 +18,14 @@ return None -def init_sentry(sentry_dsn, *, debug=None, integrations=[], extra_kwargs={}): +def init_sentry( + sentry_dsn, + *, + debug=None, + integrations=None, + extra_kwargs=None, + disable_logging_event_reporting=False, +): if debug is None: debug = bool(os.environ.get("SWH_SENTRY_DEBUG")) sentry_dsn = sentry_dsn or os.environ.get("SWH_SENTRY_DSN") @@ -26,6 +34,19 @@ if sentry_dsn: import sentry_sdk + integrations = integrations or [] + extra_kwargs = extra_kwargs or {} + + if disable_logging_event_reporting: + from sentry_sdk.integrations.logging import LoggingIntegration + + # Explicitly disable event reporting + sentry_logging = LoggingIntegration( + level=logging.INFO, + event_level=None, + ) + integrations.append(sentry_logging) + sentry_sdk.init( release=get_sentry_release(), environment=environment,