Skip to content

Commit 5707866

Browse files
authored
Merge pull request #16 from community-of-python/feature/litestar-sentry-sdk
Use LitestarIntegration for sentry
2 parents 1eefa98 + 67e128e commit 5707866

File tree

4 files changed

+16
-47
lines changed

4 files changed

+16
-47
lines changed

microbootstrap/bootstrappers/litestar.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33

44
import litestar
55
import litestar.types
6-
import sentry_sdk
76
import typing_extensions
8-
from litestar import openapi, status_codes
7+
from litestar import openapi
98
from litestar.config.app import AppConfig as LitestarConfig
109
from litestar.config.cors import CORSConfig as LitestarCorsConfig
1110
from litestar.contrib.opentelemetry.config import OpenTelemetryConfig as LitestarOpentelemetryConfig
1211
from litestar.contrib.prometheus import PrometheusConfig as LitestarPrometheusConfig
1312
from litestar.contrib.prometheus import PrometheusController
14-
from litestar.exceptions.http_exceptions import HTTPException
1513
from litestar_offline_docs import generate_static_files_config
14+
from sentry_sdk.integrations.litestar import LitestarIntegration
1615

1716
from microbootstrap.bootstrappers.base import ApplicationBootstrapper
1817
from microbootstrap.instruments.cors_instrument import CorsInstrument
@@ -41,19 +40,13 @@ def bootstrap_before(self: typing_extensions.Self) -> dict[str, typing.Any]:
4140

4241
@LitestarBootstrapper.use_instrument()
4342
class LitestarSentryInstrument(SentryInstrument):
44-
@staticmethod
45-
async def sentry_exception_catcher_hook(
46-
exception: Exception,
47-
_request_scope: litestar.types.Scope,
48-
) -> None:
49-
if (
50-
not isinstance(exception, HTTPException)
51-
or exception.status_code >= status_codes.HTTP_500_INTERNAL_SERVER_ERROR
52-
):
53-
sentry_sdk.capture_exception(exception)
54-
55-
def bootstrap_before(self) -> dict[str, typing.Any]:
56-
return {"after_exception": [self.sentry_exception_catcher_hook]}
43+
def bootstrap(self) -> None:
44+
for sentry_integration in self.instrument_config.sentry_integrations:
45+
if isinstance(sentry_integration, LitestarIntegration):
46+
break
47+
else:
48+
self.instrument_config.sentry_integrations.append(LitestarIntegration())
49+
super().bootstrap()
5750

5851

5952
@LitestarBootstrapper.use_instrument()

poetry.lock

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/instruments/test_opentelemetry.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ async def test_litestar_opentelemetry_bootstrap_working(
7676

7777
opentelemetry_middleware = opentelemetry_bootstrap_result["middleware"][0]
7878
assert isinstance(opentelemetry_middleware, DefineMiddleware)
79+
async_mock.__name__ = "test-name"
7980
opentelemetry_middleware.middleware.__call__ = async_mock
8081

8182
@litestar.get("/test-handler")

tests/instruments/test_sentry.py

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import typing
2-
from unittest.mock import MagicMock
32

43
import litestar
54
from litestar.testing import AsyncTestClient
@@ -37,30 +36,8 @@ def test_sentry_teardown(
3736

3837
def test_litestar_sentry_bootstrap(minimal_sentry_config: SentryConfig) -> None:
3938
sentry_instrument: typing.Final = LitestarSentryInstrument(minimal_sentry_config)
40-
4139
sentry_instrument.bootstrap()
42-
assert sentry_instrument.bootstrap_before() == {
43-
"after_exception": [sentry_instrument.sentry_exception_catcher_hook],
44-
}
45-
46-
47-
async def test_litestar_sentry_bootstrap_working(minimal_sentry_config: SentryConfig, magic_mock: MagicMock) -> None:
48-
sentry_instrument: typing.Final = LitestarSentryInstrument(minimal_sentry_config)
49-
sentry_instrument.sentry_exception_catcher_hook = magic_mock # type: ignore[method-assign]
50-
51-
@litestar.get("/test-error-handler")
52-
async def error_handler() -> None:
53-
raise ValueError("I'm test error")
54-
55-
sentry_instrument.bootstrap()
56-
litestar_application: typing.Final = litestar.Litestar(
57-
route_handlers=[error_handler],
58-
**sentry_instrument.bootstrap_before(),
59-
)
60-
61-
async with AsyncTestClient(app=litestar_application) as async_client:
62-
await async_client.get("/test-error-handler")
63-
assert magic_mock.called
40+
assert sentry_instrument.bootstrap_before() == {}
6441

6542

6643
async def test_litestar_sentry_bootstrap_catch_exception(
@@ -73,10 +50,7 @@ async def error_handler() -> None:
7350
raise ValueError("I'm test error")
7451

7552
sentry_instrument.bootstrap()
76-
litestar_application: typing.Final = litestar.Litestar(
77-
route_handlers=[error_handler],
78-
**sentry_instrument.bootstrap_before(),
79-
)
53+
litestar_application: typing.Final = litestar.Litestar(route_handlers=[error_handler])
8054

8155
async with AsyncTestClient(app=litestar_application) as async_client:
8256
await async_client.get("/test-error-handler")

0 commit comments

Comments
 (0)