Skip to content

Commit e381371

Browse files
authored
Some changes for fastapi bootstrapper (#50)
* allow overriding fastapi lifecycle methods * use asgi middleware instead of fastapi integration for sentry * increase test coverage * increase test coverage * rollback merging order
1 parent 67632fd commit e381371

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
lines changed

microbootstrap/bootstrappers/fastapi.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from health_checks.fastapi_healthcheck import build_fastapi_health_check_router
77
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
88
from prometheus_fastapi_instrumentator import Instrumentator
9-
from sentry_sdk.integrations.fastapi import FastApiIntegration
9+
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
1010

1111
from microbootstrap.bootstrappers.base import ApplicationBootstrapper
1212
from microbootstrap.config.fastapi import FastApiConfig
@@ -40,13 +40,9 @@ def bootstrap_before(self) -> dict[str, typing.Any]:
4040

4141
@FastApiBootstrapper.use_instrument()
4242
class FastApiSentryInstrument(SentryInstrument):
43-
def bootstrap(self) -> None:
44-
for sentry_integration in self.instrument_config.sentry_integrations:
45-
if isinstance(sentry_integration, FastApiIntegration):
46-
break
47-
else:
48-
self.instrument_config.sentry_integrations.append(FastApiIntegration())
49-
super().bootstrap()
43+
def bootstrap_after(self, application: ApplicationT) -> ApplicationT:
44+
application.add_middleware(SentryAsgiMiddleware)
45+
return application
5046

5147

5248
@FastApiBootstrapper.use_instrument()

microbootstrap/instruments/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ def write_status(self, console_writer: ConsoleWriter) -> None:
4040
)
4141

4242
@abc.abstractmethod
43-
def is_ready(self) -> bool:
44-
raise NotImplementedError
43+
def is_ready(self) -> bool: ...
4544

4645
@classmethod
4746
@abc.abstractmethod

tests/instruments/test_sentry.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ async def error_handler() -> None:
6666
def test_fastapi_sentry_bootstrap(minimal_sentry_config: SentryConfig) -> None:
6767
sentry_instrument: typing.Final = FastApiSentryInstrument(minimal_sentry_config)
6868
sentry_instrument.bootstrap()
69+
app = fastapi.FastAPI()
70+
assert sentry_instrument.bootstrap_after(app) is app
6971
assert sentry_instrument.bootstrap_before() == {}
7072

7173

0 commit comments

Comments
 (0)