2929from lite_bootstrap .types import UNSET
3030from tests .conftest import (
3131 HEALTH_PROBE_CASES ,
32+ HTTP_METRIC_CASES ,
3233 CustomInstrumentor ,
3334 SentryTestTransport ,
34- capture_probe_warnings ,
3535 emulate_package_missing ,
3636 emulate_package_missing_with_module_reload ,
37- failed_probe_names ,
3837 in_memory_opentelemetry_metric_reader ,
39- opentelemetry_recorded_http_server_metrics ,
40- prometheus_recorded_path ,
38+ opentelemetry_http_request_count ,
4139 prometheus_request_count ,
4240 unregister_prometheus_request_metrics ,
4341 warning_source_files ,
@@ -614,8 +612,8 @@ async def items() -> list[int]:
614612 assert prometheus_request_count ("http_requests_total" , "handler" , normal_path ) == 1
615613
616614
617- def _http_metric_sets_one_request_moves (config : FastAPIConfig ) -> tuple [bool , bool ]:
618- """Return whether one request reached OpenTelemetry's HTTP metrics and Prometheus's, in that order ."""
615+ def _http_request_counts (config : FastAPIConfig ) -> tuple [int , float ]:
616+ """Return the OpenTelemetry and Prometheus request counts after one request ."""
619617 path : typing .Final = f"/items-{ uuid .uuid4 ().hex } "
620618 application : typing .Final = config .app
621619 unregister_prometheus_request_metrics ()
@@ -630,41 +628,31 @@ async def items() -> str:
630628 try :
631629 with TestClient (application ) as client :
632630 assert client .get (path ).status_code == status .HTTP_200_OK
633- return opentelemetry_recorded_http_server_metrics (reader ), prometheus_recorded_path (path )
631+ return opentelemetry_http_request_count (reader ), prometheus_request_count (
632+ "http_requests_total" , "handler" , path
633+ )
634634 finally :
635635 bootstrapper .teardown ()
636636
637637
638638@pytest .mark .parametrize (
639639 ("overrides" , "expected" ),
640640 [
641- pytest . param ({ "opentelemetry_metrics_endpoint" : "localhost:4317" }, ( True , False ), id = "both-keeps-otel" ) ,
641+ * HTTP_METRIC_CASES ,
642642 pytest .param (
643643 {
644644 "opentelemetry_metrics_endpoint" : "http://localhost:4318/v1/metrics" ,
645645 "opentelemetry_exporter_protocol" : "http" ,
646646 },
647- (True , False ),
647+ (1 , 0 ),
648648 id = "both-keeps-otel-over-http" ,
649649 ),
650- pytest .param (
651- {"opentelemetry_metrics_endpoint" : "localhost:4317" , "prometheus_http_metrics_enabled" : True },
652- (True , True ),
653- id = "both-restored" ,
654- ),
655- pytest .param ({}, (False , True ), id = "prometheus-alone" ),
656- pytest .param (
657- {"opentelemetry_metrics_endpoint" : "localhost:4317" , "prometheus_metrics_path" : "" },
658- (True , False ),
659- id = "otel-alone" ,
660- ),
661- pytest .param ({"prometheus_http_metrics_enabled" : False }, (False , False ), id = "prometheus-http-off" ),
662650 ],
663651)
664652def test_fastapi_http_metrics_are_recorded_once (
665- fastapi_config : FastAPIConfig , overrides : dict [str , typing .Any ], expected : tuple [bool , bool ]
653+ fastapi_config : FastAPIConfig , overrides : dict [str , typing .Any ], expected : tuple [int , int ]
666654) -> None :
667- assert _http_metric_sets_one_request_moves (dataclasses .replace (fastapi_config , ** overrides )) == expected
655+ assert _http_request_counts (dataclasses .replace (fastapi_config , ** overrides )) == expected
668656
669657
670658def test_fastapi_prometheus_keeps_http_metrics_when_the_otlp_metric_exporter_is_missing (
@@ -675,7 +663,7 @@ def test_fastapi_prometheus_keeps_http_metrics_when_the_otlp_metric_exporter_is_
675663 patch .object (import_checker , "is_otlp_grpc_exporter_installed" , new = False ),
676664 pytest .warns (InstrumentDependencyMissingWarning , match = "gRPC OTLP exporter is not installed" ),
677665 ):
678- assert _http_metric_sets_one_request_moves (config ) == (False , True )
666+ assert _http_request_counts (config ) == (0 , 1 )
679667
680668
681669def test_fastapi_prometheus_still_serves_metrics_while_opentelemetry_records_http_metrics (
@@ -771,18 +759,17 @@ def test_fastapi_otel_honors_excluded_urls_from_environment(
771759 assert span_names == ["GET /custom-healthy" ]
772760
773761
774- @pytest .mark .parametrize (("probes " , "expected_status" , "expected_health_status" , "expected_failed " ), HEALTH_PROBE_CASES )
762+ @pytest .mark .parametrize (("probe " , "expected_status" , "expected_health_status" ), HEALTH_PROBE_CASES )
775763def test_fastapi_health_check_runs_probes (
776764 fastapi_config : FastAPIConfig ,
777- probes : dict [ str , HealthProbe ] ,
765+ probe : HealthProbe ,
778766 expected_status : int ,
779767 expected_health_status : bool ,
780- expected_failed : list [str ],
781768) -> None :
782- config = dataclasses .replace (fastapi_config , health_checks_probes = probes )
769+ config = dataclasses .replace (fastapi_config , health_checks_probes = { "cache" : probe } )
783770 bootstrapper = FastAPIBootstrapper (bootstrap_config = config )
784771 application = bootstrapper .bootstrap ()
785- with capture_probe_warnings () as probe_warnings , TestClient (application ) as test_client :
772+ with TestClient (application ) as test_client :
786773 response = test_client .get (config .health_checks_path )
787774
788775 assert response .status_code == expected_status
@@ -791,7 +778,6 @@ def test_fastapi_health_check_runs_probes(
791778 "service_name" : "microservice" ,
792779 "service_version" : "2.0.0" ,
793780 }
794- assert failed_probe_names (probe_warnings ) == expected_failed
795781
796782
797783def test_built_app_title_and_version_fall_back_when_unset () -> None :
0 commit comments