1616
1717import unittest
1818from timeit import default_timer
19- from unittest .mock import patch
19+ from unittest .mock import Mock , patch
2020
2121import fastapi
2222from fastapi .middleware .httpsredirect import HTTPSRedirectMiddleware
3434 _server_duration_attrs_old ,
3535)
3636from opentelemetry .instrumentation .asgi import OpenTelemetryMiddleware
37+ from opentelemetry .instrumentation .auto_instrumentation ._load import (
38+ _load_instrumentors ,
39+ )
3740from opentelemetry .sdk .metrics .export import (
3841 HistogramDataPoint ,
3942 NumberDataPoint ,
@@ -1028,15 +1031,23 @@ def client_response_hook(send_span, scope, message):
10281031 )
10291032
10301033
1031- def get_distribution_with_fastapi (* args , ** kwargs ):
1032- dist = args [0 ]
1033- if dist == "fastapi~=0.58 " :
1034- # Value does not matter. Only whether an exception is thrown
1035- return None
1034+ def mock_version_with_fastapi (* args , ** kwargs ):
1035+ req_name = args [0 ]
1036+ if req_name == "fastapi" :
1037+ # TODO: Value now matters
1038+ return "0.58"
10361039 raise PackageNotFoundError ()
10371040
10381041
1039- def get_distribution_without_fastapi (* args , ** kwargs ):
1042+ def mock_version_with_old_fastapi (* args , ** kwargs ):
1043+ req_name = args [0 ]
1044+ if req_name == "fastapi" :
1045+ # TODO: Value now matters
1046+ return "0.57"
1047+ raise PackageNotFoundError ()
1048+
1049+
1050+ def mock_version_without_fastapi (* args , ** kwargs ):
10401051 raise PackageNotFoundError ()
10411052
10421053
@@ -1051,30 +1062,31 @@ def test_entry_point_exists(self):
10511062 (ep ,) = entry_points (group = "opentelemetry_instrumentor" )
10521063 self .assertEqual (ep .name , "fastapi" )
10531064
1054- """ FIXME: get_distribution is gone
1055- @patch("opentelemetry.instrumentation.dependencies.get_distribution")
1056- def test_instruments_with_fastapi_installed(self, mock_get_distribution):
1057- mock_get_distribution.side_effect = get_distribution_with_fastapi
1065+ @patch ("opentelemetry.instrumentation.dependencies.version" )
1066+ def test_instruments_with_fastapi_installed (self , mock_version ):
1067+ mock_version .side_effect = mock_version_with_fastapi
10581068 mock_distro = Mock ()
10591069 _load_instrumentors (mock_distro )
1060- mock_get_distribution .assert_called_once_with("fastapi~=0.58 ")
1070+ mock_version .assert_called_once_with ("fastapi" )
10611071 self .assertEqual (len (mock_distro .load_instrumentor .call_args_list ), 1 )
10621072 (ep ,) = mock_distro .load_instrumentor .call_args .args
10631073 self .assertEqual (ep .name , "fastapi" )
10641074
1065- @patch("opentelemetry.instrumentation.dependencies.get_distribution")
1066- def test_instruments_without_fastapi_installed(
1067- self, mock_get_distribution
1068- ):
1069- mock_get_distribution.side_effect = get_distribution_without_fastapi
1075+ @patch ("opentelemetry.instrumentation.dependencies.version" )
1076+ def test_instruments_with_old_fastapi_installed (self , mock_version ):
1077+ mock_version .side_effect = mock_version_with_old_fastapi
10701078 mock_distro = Mock ()
10711079 _load_instrumentors (mock_distro )
1072- mock_get_distribution.assert_called_once_with("fastapi~=0.58")
1073- with self.assertRaises(PackageNotFoundError):
1074- mock_get_distribution("fastapi~=0.58")
1075- self.assertEqual(len(mock_distro.load_instrumentor.call_args_list), 0)
1080+ mock_version .assert_called_once_with ("fastapi" )
1081+ mock_distro .load_instrumentor .assert_not_called ()
1082+
1083+ @patch ("opentelemetry.instrumentation.dependencies.version" )
1084+ def test_instruments_without_fastapi_installed (self , mock_version ):
1085+ mock_version .side_effect = mock_version_without_fastapi
1086+ mock_distro = Mock ()
1087+ _load_instrumentors (mock_distro )
1088+ mock_version .assert_called_once_with ("fastapi" )
10761089 mock_distro .load_instrumentor .assert_not_called ()
1077- """
10781090
10791091 def _create_app (self ):
10801092 # instrumentation is handled by the instrument call
0 commit comments