@@ -151,6 +151,9 @@ def matches(self, scope: Scope) -> tuple[Match, Scope]:
151151 return super ().matches (scope )
152152
153153
154+ SCOPE = "opentelemetry.instrumentation.fastapi"
155+
156+
154157class TestBaseFastAPI (TestBase ):
155158 def _create_app (self ):
156159 app = self ._create_fastapi_app ()
@@ -454,7 +457,7 @@ def test_instrument_app_with_instrument(self):
454457 self .assertIn ("GET /foobar" , span .name )
455458 self .assertEqual (
456459 span .instrumentation_scope .name ,
457- "opentelemetry.instrumentation.fastapi" ,
460+ SCOPE ,
458461 )
459462
460463 def test_uninstrument_app (self ):
@@ -525,97 +528,66 @@ def test_fastapi_metrics(self):
525528 self ._client .get ("/foobar" )
526529 self ._client .get ("/foobar" )
527530 self ._client .get ("/foobar" )
528- metrics_list = self .memory_metrics_reader .get_metrics_data ()
529531 number_data_point_seen = False
530532 histogram_data_point_seen = False
531- self .assertTrue (len (metrics_list .resource_metrics ) == 1 )
532- for resource_metric in metrics_list .resource_metrics :
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 :
540- self .assertTrue (len (scope_metric .metrics ) == 3 )
541- for metric in scope_metric .metrics :
542- self .assertIn (metric .name , _expected_metric_names_old )
543- data_points = list (metric .data .data_points )
544- self .assertEqual (len (data_points ), 1 )
545- for point in data_points :
546- if isinstance (point , HistogramDataPoint ):
547- self .assertEqual (point .count , 3 )
548- histogram_data_point_seen = True
549- if isinstance (point , NumberDataPoint ):
550- number_data_point_seen = True
551- for attr in point .attributes :
552- self .assertIn (
553- attr , _recommended_attrs_old [metric .name ]
554- )
533+ metrics = self .get_sorted_metrics (SCOPE )
534+ self .assertTrue (len (metrics ) == 3 )
535+ for metric in metrics :
536+ self .assertIn (metric .name , _expected_metric_names_old )
537+ data_points = list (metric .data .data_points )
538+ self .assertEqual (len (data_points ), 1 )
539+ for point in data_points :
540+ if isinstance (point , HistogramDataPoint ):
541+ self .assertEqual (point .count , 3 )
542+ histogram_data_point_seen = True
543+ if isinstance (point , NumberDataPoint ):
544+ number_data_point_seen = True
545+ for attr in point .attributes :
546+ self .assertIn (attr , _recommended_attrs_old [metric .name ])
555547 self .assertTrue (number_data_point_seen and histogram_data_point_seen )
556548
557549 def test_fastapi_metrics_new_semconv (self ):
558550 self ._client .get ("/foobar" )
559551 self ._client .get ("/foobar" )
560552 self ._client .get ("/foobar" )
561- metrics_list = self .memory_metrics_reader .get_metrics_data ()
562553 number_data_point_seen = False
563554 histogram_data_point_seen = False
564- self .assertTrue (len (metrics_list .resource_metrics ) == 1 )
565- for resource_metric in metrics_list .resource_metrics :
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 :
573- for metric in scope_metric .metrics :
574- self .assertIn (metric .name , _expected_metric_names_new )
575- data_points = list (metric .data .data_points )
576- self .assertEqual (len (data_points ), 1 )
577- for point in data_points :
578- if isinstance (point , HistogramDataPoint ):
579- self .assertEqual (point .count , 3 )
580- histogram_data_point_seen = True
581- if isinstance (point , NumberDataPoint ):
582- number_data_point_seen = True
583- for attr in point .attributes :
584- self .assertIn (
585- attr , _recommended_attrs_new [metric .name ]
586- )
555+ metrics = self .get_sorted_metrics (SCOPE )
556+ self .assertTrue (len (metrics ) == 3 )
557+ for metric in metrics :
558+ self .assertIn (metric .name , _expected_metric_names_new )
559+ data_points = list (metric .data .data_points )
560+ self .assertEqual (len (data_points ), 1 )
561+ for point in data_points :
562+ if isinstance (point , HistogramDataPoint ):
563+ self .assertEqual (point .count , 3 )
564+ histogram_data_point_seen = True
565+ if isinstance (point , NumberDataPoint ):
566+ number_data_point_seen = True
567+ for attr in point .attributes :
568+ self .assertIn (attr , _recommended_attrs_new [metric .name ])
587569 self .assertTrue (number_data_point_seen and histogram_data_point_seen )
588570
589571 def test_fastapi_metrics_both_semconv (self ):
590572 self ._client .get ("/foobar" )
591573 self ._client .get ("/foobar" )
592574 self ._client .get ("/foobar" )
593- metrics_list = self .memory_metrics_reader .get_metrics_data ()
594575 number_data_point_seen = False
595576 histogram_data_point_seen = False
596- self .assertTrue (len (metrics_list .resource_metrics ) == 1 )
597- for resource_metric in metrics_list .resource_metrics :
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 :
605- for metric in scope_metric .metrics :
606- self .assertIn (metric .name , _expected_metric_names_both )
607- data_points = list (metric .data .data_points )
608- self .assertEqual (len (data_points ), 1 )
609- for point in data_points :
610- if isinstance (point , HistogramDataPoint ):
611- self .assertEqual (point .count , 3 )
612- histogram_data_point_seen = True
613- if isinstance (point , NumberDataPoint ):
614- number_data_point_seen = True
615- for attr in point .attributes :
616- self .assertIn (
617- attr , _recommended_attrs_both [metric .name ]
618- )
577+ metrics = self .get_sorted_metrics (SCOPE )
578+ self .assertTrue (len (metrics ) == 5 )
579+ for metric in metrics :
580+ self .assertIn (metric .name , _expected_metric_names_both )
581+ data_points = list (metric .data .data_points )
582+ self .assertEqual (len (data_points ), 1 )
583+ for point in data_points :
584+ if isinstance (point , HistogramDataPoint ):
585+ self .assertEqual (point .count , 3 )
586+ histogram_data_point_seen = True
587+ if isinstance (point , NumberDataPoint ):
588+ number_data_point_seen = True
589+ for attr in point .attributes :
590+ self .assertIn (attr , _recommended_attrs_both [metric .name ])
619591 self .assertTrue (number_data_point_seen and histogram_data_point_seen )
620592
621593 def test_basic_metric_success (self ):
@@ -639,7 +611,7 @@ def test_basic_metric_success(self):
639611 HTTP_FLAVOR : "1.1" ,
640612 HTTP_SERVER_NAME : "testserver" ,
641613 }
642- metrics = self .get_sorted_metrics ()
614+ metrics = self .get_sorted_metrics (SCOPE )
643615 for metric in metrics :
644616 for point in list (metric .data .data_points ):
645617 if isinstance (point , HistogramDataPoint ):
@@ -671,7 +643,7 @@ def test_basic_metric_success_new_semconv(self):
671643 HTTP_REQUEST_METHOD : "GET" ,
672644 URL_SCHEME : "https" ,
673645 }
674- metrics = self .get_sorted_metrics ()
646+ metrics = self .get_sorted_metrics (SCOPE )
675647 for metric in metrics :
676648 for point in list (metric .data .data_points ):
677649 if isinstance (point , HistogramDataPoint ):
@@ -726,7 +698,7 @@ def test_basic_metric_success_both_semconv(self):
726698 HTTP_REQUEST_METHOD : "GET" ,
727699 URL_SCHEME : "https" ,
728700 }
729- metrics = self .get_sorted_metrics ()
701+ metrics = self .get_sorted_metrics (SCOPE )
730702 for metric in metrics :
731703 for point in list (metric .data .data_points ):
732704 if isinstance (point , HistogramDataPoint ):
@@ -798,7 +770,7 @@ def test_basic_metric_nonstandard_http_method_success(self):
798770 HTTP_FLAVOR : "1.1" ,
799771 HTTP_SERVER_NAME : "testserver" ,
800772 }
801- metrics = self .get_sorted_metrics ()
773+ metrics = self .get_sorted_metrics (SCOPE )
802774 for metric in metrics :
803775 for point in list (metric .data .data_points ):
804776 if isinstance (point , HistogramDataPoint ):
@@ -830,7 +802,7 @@ def test_basic_metric_nonstandard_http_method_success_new_semconv(self):
830802 HTTP_REQUEST_METHOD : "_OTHER" ,
831803 URL_SCHEME : "https" ,
832804 }
833- metrics = self .get_sorted_metrics ()
805+ metrics = self .get_sorted_metrics (SCOPE )
834806 for metric in metrics :
835807 for point in list (metric .data .data_points ):
836808 if isinstance (point , HistogramDataPoint ):
@@ -885,7 +857,7 @@ def test_basic_metric_nonstandard_http_method_success_both_semconv(self):
885857 HTTP_REQUEST_METHOD : "_OTHER" ,
886858 URL_SCHEME : "https" ,
887859 }
888- metrics = self .get_sorted_metrics ()
860+ metrics = self .get_sorted_metrics (SCOPE )
889861 for metric in metrics :
890862 for point in list (metric .data .data_points ):
891863 if isinstance (point , HistogramDataPoint ):
@@ -944,7 +916,7 @@ def test_basic_post_request_metric_success(self):
944916 duration = max (round ((default_timer () - start ) * 1000 ), 0 )
945917 response_size = int (response .headers .get ("content-length" ))
946918 request_size = int (response .request .headers .get ("content-length" ))
947- metrics = self .get_sorted_metrics ()
919+ metrics = self .get_sorted_metrics (SCOPE )
948920 for metric in metrics :
949921 for point in list (metric .data .data_points ):
950922 if isinstance (point , HistogramDataPoint ):
@@ -967,7 +939,7 @@ def test_basic_post_request_metric_success_new_semconv(self):
967939 duration_s = max (default_timer () - start , 0 )
968940 response_size = int (response .headers .get ("content-length" ))
969941 request_size = int (response .request .headers .get ("content-length" ))
970- metrics = self .get_sorted_metrics ()
942+ metrics = self .get_sorted_metrics (SCOPE )
971943 for metric in metrics :
972944 for point in list (metric .data .data_points ):
973945 if isinstance (point , HistogramDataPoint ):
@@ -993,7 +965,7 @@ def test_basic_post_request_metric_success_both_semconv(self):
993965 duration_s = max (default_timer () - start , 0 )
994966 response_size = int (response .headers .get ("content-length" ))
995967 request_size = int (response .request .headers .get ("content-length" ))
996- metrics = self .get_sorted_metrics ()
968+ metrics = self .get_sorted_metrics (SCOPE )
997969 for metric in metrics :
998970 for point in list (metric .data .data_points ):
999971 if isinstance (point , HistogramDataPoint ):
@@ -1019,7 +991,7 @@ def test_metric_uninstrument_app(self):
1019991 self ._client .get ("/foobar" )
1020992 self ._instrumentor .uninstrument_app (self ._app )
1021993 self ._client .get ("/foobar" )
1022- metrics = self .get_sorted_metrics ()
994+ metrics = self .get_sorted_metrics (SCOPE )
1023995 for metric in metrics :
1024996 for point in list (metric .data .data_points ):
1025997 if isinstance (point , HistogramDataPoint ):
@@ -1034,7 +1006,7 @@ def test_metric_uninstrument(self):
10341006 self ._instrumentor .uninstrument ()
10351007 self ._client .get ("/foobar" )
10361008
1037- metrics = self .get_sorted_metrics ()
1009+ metrics = self .get_sorted_metrics (SCOPE )
10381010 for metric in metrics :
10391011 for point in list (metric .data .data_points ):
10401012 if isinstance (point , HistogramDataPoint ):
0 commit comments