From 7830dd081b4f14b93dae3ef834944e0ad4a93afc Mon Sep 17 00:00:00 2001 From: Peter Schutt Date: Sun, 15 Jan 2023 10:22:26 +1000 Subject: [PATCH] feat(sentry): disabled by default if environment "local" or "test" --- src/starlite_saqlalchemy/init_plugin.py | 9 +++++++-- src/starlite_saqlalchemy/sentry.py | 2 +- tests/conftest.py | 8 ++------ tests/unit/test_init_plugin.py | 23 ++++++++++++++++++++++- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/starlite_saqlalchemy/init_plugin.py b/src/starlite_saqlalchemy/init_plugin.py index b4171c5d..4f79b092 100644 --- a/src/starlite_saqlalchemy/init_plugin.py +++ b/src/starlite_saqlalchemy/init_plugin.py @@ -132,7 +132,7 @@ class PluginConfig(BaseModel): Set the OpenAPI config object to [`AppConfig.openapi_config`][starlite.config.app.AppConfig.openapi_config]. """ - do_sentry: bool = True + do_sentry: bool | None = None """Configure sentry. Configure the application to initialize Sentry on startup. Adds a handler to @@ -330,7 +330,12 @@ def configure_sentry(self, app_config: AppConfig) -> None: Args: app_config: The Starlite application config object. """ - if self.config.do_sentry: + do_sentry = ( + self.config.do_sentry + if self.config.do_sentry is not None + else settings.app.ENVIRONMENT not in {"local", "test"} + ) + if do_sentry: app_config.on_startup.append(sentry.configure) def configure_sqlalchemy_plugin(self, app_config: AppConfig) -> None: diff --git a/src/starlite_saqlalchemy/sentry.py b/src/starlite_saqlalchemy/sentry.py index 4941d68b..d968fc63 100644 --- a/src/starlite_saqlalchemy/sentry.py +++ b/src/starlite_saqlalchemy/sentry.py @@ -37,7 +37,7 @@ def configure() -> None: See [SentrySettings][starlite_saqlalchemy.settings.SentrySettings]. """ - sentry_sdk.init( + sentry_sdk.init( # pragma: no cover dsn=settings.sentry.DSN, environment=settings.app.ENVIRONMENT, release=settings.app.BUILD_NUMBER, diff --git a/tests/conftest.py b/tests/conftest.py index b4e742b0..fa2db5ed 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -13,7 +13,7 @@ from structlog.testing import CapturingLogger import starlite_saqlalchemy -from starlite_saqlalchemy import ConfigureApp, PluginConfig, log +from starlite_saqlalchemy import ConfigureApp, log from tests.utils.domain import authors, books if TYPE_CHECKING: @@ -55,11 +55,7 @@ def fx_app() -> Starlite: Returns: The application instance. """ - return Starlite( - route_handlers=[], - on_app_init=[ConfigureApp(config=PluginConfig(do_sentry=False))], - openapi_config=None, - ) + return Starlite(route_handlers=[], on_app_init=[ConfigureApp()], openapi_config=None) @pytest.fixture(name="raw_authors") diff --git a/tests/unit/test_init_plugin.py b/tests/unit/test_init_plugin.py index a8a53bd2..0b0f3ec9 100644 --- a/tests/unit/test_init_plugin.py +++ b/tests/unit/test_init_plugin.py @@ -8,7 +8,8 @@ from starlite import Starlite from starlite.cache import SimpleCacheBackend -from starlite_saqlalchemy import init_plugin +from starlite_saqlalchemy import init_plugin, sentry, settings +from starlite_saqlalchemy.testing import modify_settings if TYPE_CHECKING: from typing import Any @@ -82,3 +83,23 @@ def test_ensure_list(in_: Any, out: Any) -> None: """Test _ensure_list() functionality.""" # pylint: disable=protected-access assert init_plugin.ConfigureApp._ensure_list(in_) == out + + +@pytest.mark.parametrize( + ("env", "exp"), + [ + ("dev", True), + ("prod", True), + ("local", False), + ("test", False), + ], +) +def test_sentry_environment_gate(env: str, exp: bool) -> None: + """Test that the sentry integration is configured under different + environment names.""" + with modify_settings((settings.app, {"ENVIRONMENT": env})): + app = Starlite(route_handlers=[], on_app_init=[init_plugin.ConfigureApp()]) + if exp is True: + assert sentry.configure in app.on_startup + else: + assert sentry.configure not in app.on_startup