diff --git a/microbootstrap/bootstrappers/faststream.py b/microbootstrap/bootstrappers/faststream.py index 7cd725e..a4fa7a1 100644 --- a/microbootstrap/bootstrappers/faststream.py +++ b/microbootstrap/bootstrappers/faststream.py @@ -8,6 +8,7 @@ from faststream.asgi import AsgiFastStream, AsgiResponse from faststream.asgi import get as handle_get from faststream.specification import AsyncAPI +from opentelemetry import trace from microbootstrap.bootstrappers.base import ApplicationBootstrapper from microbootstrap.config.faststream import FastStreamConfig @@ -23,6 +24,9 @@ from microbootstrap.settings import FastStreamSettings +tracer: typing.Final = trace.get_tracer(__name__) + + class KwargsAsgiFastStream(AsgiFastStream): def __init__(self, **kwargs: typing.Any) -> None: # noqa: ANN401 # `broker` argument is positional-only @@ -119,6 +123,11 @@ async def check_health(scope: typing.Any) -> AsgiResponse: # noqa: ANN401, ARG0 else AsgiResponse(b"Service is unhealthy", 500, headers={"content-type": "application/json"}) ) + if self.instrument_config.opentelemetry_generate_health_check_spans: + check_health = tracer.start_as_current_span(f"GET {self.instrument_config.health_checks_path}")( + check_health, + ) + return {"asgi_routes": ((self.instrument_config.health_checks_path, check_health),)} async def define_health_status(self) -> bool: diff --git a/microbootstrap/instruments/health_checks_instrument.py b/microbootstrap/instruments/health_checks_instrument.py index 80fe33e..9b0ee3f 100644 --- a/microbootstrap/instruments/health_checks_instrument.py +++ b/microbootstrap/instruments/health_checks_instrument.py @@ -19,6 +19,9 @@ class HealthChecksConfig(BaseInstrumentConfig): health_checks_path: str = "/health/" health_checks_include_in_schema: bool = False + # Cross-instrument parameter, comes from opentelemetry + opentelemetry_generate_health_check_spans: bool = True + class HealthChecksInstrument(Instrument[HealthChecksConfig]): instrument_name = "Health checks"