Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
feat(sentry): disabled by default if environment "local" or "test"
Browse files Browse the repository at this point in the history
  • Loading branch information
peterschutt committed Jan 15, 2023
1 parent 3a09219 commit 7830dd0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
9 changes: 7 additions & 2 deletions src/starlite_saqlalchemy/init_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/starlite_saqlalchemy/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 2 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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")
Expand Down
23 changes: 22 additions & 1 deletion tests/unit/test_init_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 7830dd0

Please sign in to comment.