Skip to content

Commit

Permalink
Some changes for fastapi bootstrapper (#50)
Browse files Browse the repository at this point in the history
* allow overriding fastapi lifecycle methods

* use asgi middleware instead of fastapi integration for sentry

* increase test coverage

* increase test coverage

* rollback merging order
  • Loading branch information
lesnik512 authored Dec 8, 2024
1 parent 67632fd commit e381371
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
12 changes: 4 additions & 8 deletions microbootstrap/bootstrappers/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from health_checks.fastapi_healthcheck import build_fastapi_health_check_router
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from prometheus_fastapi_instrumentator import Instrumentator
from sentry_sdk.integrations.fastapi import FastApiIntegration
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware

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

@FastApiBootstrapper.use_instrument()
class FastApiSentryInstrument(SentryInstrument):
def bootstrap(self) -> None:
for sentry_integration in self.instrument_config.sentry_integrations:
if isinstance(sentry_integration, FastApiIntegration):
break
else:
self.instrument_config.sentry_integrations.append(FastApiIntegration())
super().bootstrap()
def bootstrap_after(self, application: ApplicationT) -> ApplicationT:
application.add_middleware(SentryAsgiMiddleware)
return application


@FastApiBootstrapper.use_instrument()
Expand Down
3 changes: 1 addition & 2 deletions microbootstrap/instruments/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def write_status(self, console_writer: ConsoleWriter) -> None:
)

@abc.abstractmethod
def is_ready(self) -> bool:
raise NotImplementedError
def is_ready(self) -> bool: ...

@classmethod
@abc.abstractmethod
Expand Down
2 changes: 2 additions & 0 deletions tests/instruments/test_sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ async def error_handler() -> None:
def test_fastapi_sentry_bootstrap(minimal_sentry_config: SentryConfig) -> None:
sentry_instrument: typing.Final = FastApiSentryInstrument(minimal_sentry_config)
sentry_instrument.bootstrap()
app = fastapi.FastAPI()
assert sentry_instrument.bootstrap_after(app) is app
assert sentry_instrument.bootstrap_before() == {}


Expand Down

0 comments on commit e381371

Please sign in to comment.