Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions microbootstrap/bootstrappers/faststream.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions microbootstrap/instruments/health_checks_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down