@@ -530,12 +530,13 @@ def test_fastapi_metrics(self):
530530 histogram_data_point_seen = False
531531 self .assertTrue (len (metrics_list .resource_metrics ) == 1 )
532532 for resource_metric in metrics_list .resource_metrics :
533- self .assertTrue (len (resource_metric .scope_metrics ) == 1 )
534- for scope_metric in resource_metric .scope_metrics :
535- self .assertEqual (
536- scope_metric .scope .name ,
537- "opentelemetry.instrumentation.fastapi" ,
538- )
533+ scope_metrics = [
534+ sm
535+ for sm in resource_metric .scope_metrics
536+ if sm .scope .name == "opentelemetry.instrumentation.fastapi"
537+ ]
538+ self .assertTrue (len (scope_metrics ) == 1 )
539+ for scope_metric in scope_metrics :
539540 self .assertTrue (len (scope_metric .metrics ) == 3 )
540541 for metric in scope_metric .metrics :
541542 self .assertIn (metric .name , _expected_metric_names_old )
@@ -562,13 +563,13 @@ def test_fastapi_metrics_new_semconv(self):
562563 histogram_data_point_seen = False
563564 self .assertTrue (len (metrics_list .resource_metrics ) == 1 )
564565 for resource_metric in metrics_list .resource_metrics :
565- self . assertTrue ( len ( resource_metric . scope_metrics ) == 1 )
566- for scope_metric in resource_metric . scope_metrics :
567- self . assertEqual (
568- scope_metric .scope .name ,
569- "opentelemetry.instrumentation.fastapi" ,
570- )
571- self . assertTrue ( len ( scope_metric . metrics ) == 3 )
566+ scope_metrics = [
567+ sm
568+ for sm in resource_metric . scope_metrics
569+ if sm .scope .name == "opentelemetry.instrumentation.fastapi"
570+ ]
571+ self . assertTrue ( len ( scope_metrics ) == 1 )
572+ for scope_metric in scope_metrics :
572573 for metric in scope_metric .metrics :
573574 self .assertIn (metric .name , _expected_metric_names_new )
574575 data_points = list (metric .data .data_points )
@@ -594,13 +595,13 @@ def test_fastapi_metrics_both_semconv(self):
594595 histogram_data_point_seen = False
595596 self .assertTrue (len (metrics_list .resource_metrics ) == 1 )
596597 for resource_metric in metrics_list .resource_metrics :
597- self . assertTrue ( len ( resource_metric . scope_metrics ) == 1 )
598- for scope_metric in resource_metric . scope_metrics :
599- self . assertEqual (
600- scope_metric .scope .name ,
601- "opentelemetry.instrumentation.fastapi" ,
602- )
603- self . assertTrue ( len ( scope_metric . metrics ) == 5 )
598+ scope_metrics = [
599+ sm
600+ for sm in resource_metric . scope_metrics
601+ if sm .scope .name == "opentelemetry.instrumentation.fastapi"
602+ ]
603+ self . assertTrue ( len ( scope_metrics ) == 1 )
604+ for scope_metric in scope_metrics :
604605 for metric in scope_metric .metrics :
605606 self .assertIn (metric .name , _expected_metric_names_both )
606607 data_points = list (metric .data .data_points )
@@ -638,10 +639,8 @@ def test_basic_metric_success(self):
638639 HTTP_FLAVOR : "1.1" ,
639640 HTTP_SERVER_NAME : "testserver" ,
640641 }
641- metrics_list = self .memory_metrics_reader .get_metrics_data ()
642- for metric in (
643- metrics_list .resource_metrics [0 ].scope_metrics [0 ].metrics
644- ):
642+ metrics = self .get_sorted_metrics ()
643+ for metric in metrics :
645644 for point in list (metric .data .data_points ):
646645 if isinstance (point , HistogramDataPoint ):
647646 self .assertDictEqual (
@@ -672,10 +671,8 @@ def test_basic_metric_success_new_semconv(self):
672671 HTTP_REQUEST_METHOD : "GET" ,
673672 URL_SCHEME : "https" ,
674673 }
675- metrics_list = self .memory_metrics_reader .get_metrics_data ()
676- for metric in (
677- metrics_list .resource_metrics [0 ].scope_metrics [0 ].metrics
678- ):
674+ metrics = self .get_sorted_metrics ()
675+ for metric in metrics :
679676 for point in list (metric .data .data_points ):
680677 if isinstance (point , HistogramDataPoint ):
681678 self .assertDictEqual (
@@ -729,10 +726,8 @@ def test_basic_metric_success_both_semconv(self):
729726 HTTP_REQUEST_METHOD : "GET" ,
730727 URL_SCHEME : "https" ,
731728 }
732- metrics_list = self .memory_metrics_reader .get_metrics_data ()
733- for metric in (
734- metrics_list .resource_metrics [0 ].scope_metrics [0 ].metrics
735- ):
729+ metrics = self .get_sorted_metrics ()
730+ for metric in metrics :
736731 for point in list (metric .data .data_points ):
737732 if isinstance (point , HistogramDataPoint ):
738733 self .assertEqual (point .count , 1 )
@@ -803,10 +798,8 @@ def test_basic_metric_nonstandard_http_method_success(self):
803798 HTTP_FLAVOR : "1.1" ,
804799 HTTP_SERVER_NAME : "testserver" ,
805800 }
806- metrics_list = self .memory_metrics_reader .get_metrics_data ()
807- for metric in (
808- metrics_list .resource_metrics [0 ].scope_metrics [0 ].metrics
809- ):
801+ metrics = self .get_sorted_metrics ()
802+ for metric in metrics :
810803 for point in list (metric .data .data_points ):
811804 if isinstance (point , HistogramDataPoint ):
812805 self .assertDictEqual (
@@ -837,10 +830,8 @@ def test_basic_metric_nonstandard_http_method_success_new_semconv(self):
837830 HTTP_REQUEST_METHOD : "_OTHER" ,
838831 URL_SCHEME : "https" ,
839832 }
840- metrics_list = self .memory_metrics_reader .get_metrics_data ()
841- for metric in (
842- metrics_list .resource_metrics [0 ].scope_metrics [0 ].metrics
843- ):
833+ metrics = self .get_sorted_metrics ()
834+ for metric in metrics :
844835 for point in list (metric .data .data_points ):
845836 if isinstance (point , HistogramDataPoint ):
846837 self .assertDictEqual (
@@ -894,10 +885,8 @@ def test_basic_metric_nonstandard_http_method_success_both_semconv(self):
894885 HTTP_REQUEST_METHOD : "_OTHER" ,
895886 URL_SCHEME : "https" ,
896887 }
897- metrics_list = self .memory_metrics_reader .get_metrics_data ()
898- for metric in (
899- metrics_list .resource_metrics [0 ].scope_metrics [0 ].metrics
900- ):
888+ metrics = self .get_sorted_metrics ()
889+ for metric in metrics :
901890 for point in list (metric .data .data_points ):
902891 if isinstance (point , HistogramDataPoint ):
903892 self .assertEqual (point .count , 1 )
@@ -955,10 +944,8 @@ def test_basic_post_request_metric_success(self):
955944 duration = max (round ((default_timer () - start ) * 1000 ), 0 )
956945 response_size = int (response .headers .get ("content-length" ))
957946 request_size = int (response .request .headers .get ("content-length" ))
958- metrics_list = self .memory_metrics_reader .get_metrics_data ()
959- for metric in (
960- metrics_list .resource_metrics [0 ].scope_metrics [0 ].metrics
961- ):
947+ metrics = self .get_sorted_metrics ()
948+ for metric in metrics :
962949 for point in list (metric .data .data_points ):
963950 if isinstance (point , HistogramDataPoint ):
964951 self .assertEqual (point .count , 1 )
@@ -980,10 +967,8 @@ def test_basic_post_request_metric_success_new_semconv(self):
980967 duration_s = max (default_timer () - start , 0 )
981968 response_size = int (response .headers .get ("content-length" ))
982969 request_size = int (response .request .headers .get ("content-length" ))
983- metrics_list = self .memory_metrics_reader .get_metrics_data ()
984- for metric in (
985- metrics_list .resource_metrics [0 ].scope_metrics [0 ].metrics
986- ):
970+ metrics = self .get_sorted_metrics ()
971+ for metric in metrics :
987972 for point in list (metric .data .data_points ):
988973 if isinstance (point , HistogramDataPoint ):
989974 self .assertEqual (point .count , 1 )
@@ -1008,10 +993,8 @@ def test_basic_post_request_metric_success_both_semconv(self):
1008993 duration_s = max (default_timer () - start , 0 )
1009994 response_size = int (response .headers .get ("content-length" ))
1010995 request_size = int (response .request .headers .get ("content-length" ))
1011- metrics_list = self .memory_metrics_reader .get_metrics_data ()
1012- for metric in (
1013- metrics_list .resource_metrics [0 ].scope_metrics [0 ].metrics
1014- ):
996+ metrics = self .get_sorted_metrics ()
997+ for metric in metrics :
1015998 for point in list (metric .data .data_points ):
1016999 if isinstance (point , HistogramDataPoint ):
10171000 self .assertEqual (point .count , 1 )
@@ -1036,10 +1019,8 @@ def test_metric_uninstrument_app(self):
10361019 self ._client .get ("/foobar" )
10371020 self ._instrumentor .uninstrument_app (self ._app )
10381021 self ._client .get ("/foobar" )
1039- metrics_list = self .memory_metrics_reader .get_metrics_data ()
1040- for metric in (
1041- metrics_list .resource_metrics [0 ].scope_metrics [0 ].metrics
1042- ):
1022+ metrics = self .get_sorted_metrics ()
1023+ for metric in metrics :
10431024 for point in list (metric .data .data_points ):
10441025 if isinstance (point , HistogramDataPoint ):
10451026 self .assertEqual (point .count , 1 )
@@ -1053,10 +1034,8 @@ def test_metric_uninstrument(self):
10531034 self ._instrumentor .uninstrument ()
10541035 self ._client .get ("/foobar" )
10551036
1056- metrics_list = self .memory_metrics_reader .get_metrics_data ()
1057- for metric in (
1058- metrics_list .resource_metrics [0 ].scope_metrics [0 ].metrics
1059- ):
1037+ metrics = self .get_sorted_metrics ()
1038+ for metric in metrics :
10601039 for point in list (metric .data .data_points ):
10611040 if isinstance (point , HistogramDataPoint ):
10621041 self .assertEqual (point .count , 1 )
0 commit comments