Skip to content

Commit 3264255

Browse files
authored
Merge pull request #129 from community-of-python/feature/add-tracing-to-faststream-asgi-health-check
Add tracing to faststream asgi health-check
2 parents 958f4db + 9d101c1 commit 3264255

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

microbootstrap/bootstrappers/faststream.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from faststream.asgi import AsgiFastStream, AsgiResponse
99
from faststream.asgi import get as handle_get
1010
from faststream.specification import AsyncAPI
11+
from opentelemetry import trace
1112

1213
from microbootstrap.bootstrappers.base import ApplicationBootstrapper
1314
from microbootstrap.config.faststream import FastStreamConfig
@@ -23,6 +24,9 @@
2324
from microbootstrap.settings import FastStreamSettings
2425

2526

27+
tracer: typing.Final = trace.get_tracer(__name__)
28+
29+
2630
class KwargsAsgiFastStream(AsgiFastStream):
2731
def __init__(self, **kwargs: typing.Any) -> None: # noqa: ANN401
2832
# `broker` argument is positional-only
@@ -119,6 +123,11 @@ async def check_health(scope: typing.Any) -> AsgiResponse: # noqa: ANN401, ARG0
119123
else AsgiResponse(b"Service is unhealthy", 500, headers={"content-type": "application/json"})
120124
)
121125

126+
if self.instrument_config.opentelemetry_generate_health_check_spans:
127+
check_health = tracer.start_as_current_span(f"GET {self.instrument_config.health_checks_path}")(
128+
check_health,
129+
)
130+
122131
return {"asgi_routes": ((self.instrument_config.health_checks_path, check_health),)}
123132

124133
async def define_health_status(self) -> bool:

microbootstrap/instruments/health_checks_instrument.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class HealthChecksConfig(BaseInstrumentConfig):
1919
health_checks_path: str = "/health/"
2020
health_checks_include_in_schema: bool = False
2121

22+
# Cross-instrument parameter, comes from opentelemetry
23+
opentelemetry_generate_health_check_spans: bool = True
24+
2225

2326
class HealthChecksInstrument(Instrument[HealthChecksConfig]):
2427
instrument_name = "Health checks"

0 commit comments

Comments
 (0)