@@ -135,8 +135,14 @@ async def test_tracing_partial_match() -> None:
135135
136136async def test_metrics () -> None :
137137 # Arrange
138+ expected_content_type = "text/plain; version=0.0.4; charset=utf-8"
138139 app = Starlette ()
139- metrics_config = MetricsConfig (app_name = "test" , include_metrics_endpoint = True , include_trace_exemplar = False )
140+ metrics_config = MetricsConfig (
141+ app_name = "test" ,
142+ include_metrics_endpoint = True ,
143+ include_trace_exemplar = False ,
144+ openmetrics_format = False ,
145+ )
140146 setup_metrics (app = app , config = metrics_config )
141147
142148 # Act
@@ -145,6 +151,7 @@ async def test_metrics() -> None:
145151
146152 # Assert
147153 assert response .status_code == 200
154+ assert response .headers ["content-type" ] == expected_content_type
148155 assert_that (response .content .decode ()).contains (
149156 'starlette_app_info{app_name="test"} 1.0' ,
150157 'starlette_requests_total{app_name="test",method="GET",path="/metrics"} 1.0' ,
@@ -201,3 +208,34 @@ async def test_metrics_with_tracing() -> None:
201208 r'app_name="test",le="([\d.]+)",method="GET",path="\/"}\ 1.0 # \{TraceID="(\w+)"\} (\d+\.\d+) (\d+\.\d+)'
202209 )
203210 assert_that (metrics .payload .decode ()).matches (pattern )
211+
212+
213+ async def test_metrics_openmetrics_with_tracing () -> None :
214+ # Arrange
215+ expected_content_type = "application/openmetrics-text; version=1.0.0; charset=utf-8"
216+ trace_config , _ = build_starlette_tracing_config ()
217+ metrics_config = MetricsConfig (
218+ app_name = "test" ,
219+ include_metrics_endpoint = True ,
220+ include_trace_exemplar = True ,
221+ openmetrics_format = True ,
222+ )
223+ app = Starlette (routes = [Route ("/" , endpoint = index , methods = ["GET" ])])
224+
225+ setup_metrics (app = app , config = metrics_config )
226+ setup_tracing (app = app , config = trace_config )
227+
228+ # Act
229+ async with starlette_app (app ) as client :
230+ response = client .get ("/" )
231+ metrics = client .get ("/metrics" )
232+
233+ # Assert
234+ assert response .status_code == 200
235+ assert metrics .status_code == 200
236+ assert metrics .headers ["content-type" ] == expected_content_type
237+ pattern = (
238+ r"starlette_request_duration_seconds_bucket\{"
239+ r'app_name="test",le="([\d.]+)",method="GET",path="\/"}\ 1.0 # \{TraceID="(\w+)"\} (\d+\.\d+) (\d+\.\d+)'
240+ )
241+ assert_that (metrics .content .decode ()).matches (pattern )
0 commit comments