11import pytest
22import structlog
33from faststream .redis import RedisBroker , TestRedisBroker
4+ from faststream .redis .opentelemetry import RedisTelemetryMiddleware
45from faststream .redis .prometheus import RedisPrometheusMiddleware
56from opentelemetry .sdk .trace .export import ConsoleSpanExporter
67from starlette import status
@@ -20,6 +21,7 @@ def broker() -> RedisBroker:
2021
2122async def test_faststream_bootstrap (broker : RedisBroker ) -> None :
2223 prometheus_metrics_path = "/test-metrics-path"
24+ health_check_path = "/custom-health-check-path"
2325 bootstrapper = FastStreamBootstrapper (
2426 bootstrap_config = FastStreamConfig (
2527 broker = broker ,
@@ -30,16 +32,47 @@ async def test_faststream_bootstrap(broker: RedisBroker) -> None:
3032 opentelemetry_endpoint = "otl" ,
3133 opentelemetry_instrumentors = [CustomInstrumentor ()],
3234 opentelemetry_span_exporter = ConsoleSpanExporter (),
35+ opentelemetry_middleware_cls = RedisTelemetryMiddleware ,
3336 prometheus_metrics_path = prometheus_metrics_path ,
3437 prometheus_middleware_cls = RedisPrometheusMiddleware ,
3538 sentry_dsn = "https://testdsn@localhost/1" ,
36- health_checks_path = "/health/" ,
39+ health_checks_path = health_check_path ,
3740 logging_buffer_capacity = 0 ,
3841 ),
3942 )
4043 application = bootstrapper .bootstrap ()
4144 logger .info ("testing logging" , key = "value" )
45+ test_client = TestClient (app = application )
4246
4347 async with TestRedisBroker (broker ):
44- response = TestClient ( app = application ) .get (prometheus_metrics_path )
48+ response = test_client .get (prometheus_metrics_path )
4549 assert response .status_code == status .HTTP_200_OK
50+
51+ response = test_client .get (health_check_path )
52+ assert response .status_code == status .HTTP_200_OK
53+ assert response .json () == {"health_status" : True , "service_name" : "microservice" , "service_version" : "2.0.0" }
54+
55+
56+ async def test_faststream_bootstrap_health_check_wo_broker () -> None :
57+ health_check_path = "/custom-health-check-path"
58+ bootstrapper = FastStreamBootstrapper (
59+ bootstrap_config = FastStreamConfig (
60+ service_name = "microservice" ,
61+ service_version = "2.0.0" ,
62+ service_environment = "test" ,
63+ service_debug = False ,
64+ opentelemetry_endpoint = "otl" ,
65+ opentelemetry_instrumentors = [CustomInstrumentor ()],
66+ opentelemetry_span_exporter = ConsoleSpanExporter (),
67+ sentry_dsn = "https://testdsn@localhost/1" ,
68+ health_checks_path = health_check_path ,
69+ logging_buffer_capacity = 0 ,
70+ ),
71+ )
72+ application = bootstrapper .bootstrap ()
73+ logger .info ("testing logging" , key = "value" )
74+ test_client = TestClient (app = application )
75+
76+ response = test_client .get (health_check_path )
77+ assert response .status_code == status .HTTP_500_INTERNAL_SERVER_ERROR
78+ assert response .text == "Service is unhealthy"
0 commit comments