Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydvoss committed Sep 10, 2024
1 parent 19a60af commit 6ad9850
Showing 1 changed file with 4 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1038,32 +1038,13 @@ def client_response_hook(send_span, scope, message):


def get_distribution_with_fastapi(*args, **kwargs):
print("JEREVOSS get_distribution_with_fastapi")
print("args: %s" % args)
print("kwargs: %s" % kwargs)
dist = args[0]
print("JEREVOSS get_distribution_with_fastapi: dist: %s" % dist)
if dist == "fastapi~=0.58":
return None #Value does not matter. Only whether an exception is thrown
elif "fastapi-slim" in dist:
raise DistributionNotFound()
else:
return None #TODO: may want to change to not found
raise DistributionNotFound()

def get_distribution_without_fastapi(*args, **kwargs):
print("JEREVOSS get_distribution_without_fastapi")
print("args: %s" % args)
print("kwargs: %s" % kwargs)
dist = args[0]
print("JEREVOSS get_distribution_without_fastapi: dist: %s" % dist)
if dist == "fastapi~=0.58":
print("JEREVOSS get_distribution_without_fastapi IF: dist: %s" % dist)
raise DistributionNotFound()
elif "fastapi-slim" in dist:
raise DistributionNotFound()
else:
print("JEREVOSS get_distribution_without_fastapi ELSE: dist: %s" % dist)
return None #TODO: may want to change to not found
raise DistributionNotFound()


class TestAutoInstrumentation(TestBaseAutoFastAPI):
Expand All @@ -1084,64 +1065,37 @@ def test_entry_point_exists(self):
self.assertEqual(ep.name, 'fastapi')
self.assertIsNone(next(eps, None))

# @patch("pkg_resources.get_distribution", side_effect=get_distribution_with_fastapi)
@patch("opentelemetry.instrumentation.auto_instrumentation._load.get_dist_dependency_conflicts")
@patch("opentelemetry.instrumentation.dependencies.get_dependency_conflicts")
@patch("opentelemetry.instrumentation.dependencies.get_distribution")
def test_instruments_with_fastapi_installed(self, mock_get_distribution, mock_get_dependency_conflicts, mock_get_dist_dependency_conflicts):
# Need to use matchers
# mock_get_distribution.side_effect = TestAutoInstrumentationget_distribution_with_fastapi
# mock_get_distribution("foobar").side_effect = DistributionNotFound()
# mock_get_distribution("fastapi-slim ~= 0.111").side_effect = DistributionNotFound()
# mock_get_distribution("fastapi ~= 0.58").return_value = None #Value does not matter. Only whether an exception is thrown
mock_get_dist_dependency_conflicts.side_effect = get_dist_dependency_conflicts
mock_get_dependency_conflicts.side_effect = get_dependency_conflicts
mock_get_distribution.side_effect = get_distribution_with_fastapi
mock_distro = Mock()
_load_instrumentors(mock_distro)
print("JEREVOSS mock_get_dist_dependency_conflicts.call_args_list: %s" % mock_get_dist_dependency_conflicts.call_args_list)
print("JEREVOSS mock_get_dependency_conflicts.call_args_list: %s" % mock_get_dependency_conflicts.call_args_list)
print("JEREVOSS mock_get_distribution.call_args_list: %s" % mock_get_distribution.call_args_list)
mock_get_distribution.assert_called_once_with("fastapi~=0.58")
# print("JEREVOSS mock_get_distribution().call_args_list: %s" % mock_get_distribution().call_args_list)
self.assertEqual(len(mock_distro.load_instrumentor.call_args_list), 1)
call_args = mock_distro.load_instrumentor.call_args
call_args_args = call_args.args
print("JEREVOSS call_args: %s" % str(call_args))
print(call_args)
print("JEREVOSS call_args_args: %s" % call_args_args)
ep = call_args_args[0]
args = mock_distro.load_instrumentor.call_args.args
ep = args[0]
self.assertEqual(ep.dist.key, 'opentelemetry-instrumentation-fastapi')
self.assertEqual(ep.module_name, 'opentelemetry.instrumentation.fastapi')
self.assertEqual(ep.attrs, ('FastAPIInstrumentor',))
self.assertEqual(ep.name, 'fastapi')
# mock_distro.load_instrumentor.assert_called_once_with(self.entry_point, skip_dep_check=True) Doesn't work

# @patch("pkg_resources.get_distribution", side_effect=get_distribution_without_fastapi)
@patch("opentelemetry.instrumentation.auto_instrumentation._load.get_dist_dependency_conflicts")
@patch("opentelemetry.instrumentation.dependencies.get_dependency_conflicts")
@patch("opentelemetry.instrumentation.dependencies.get_distribution")
def test_instruments_without_fastapi_installed(self, mock_get_distribution, mock_get_dependency_conflicts, mock_get_dist_dependency_conflicts):
# mock_get_distribution.side_effect = get_distribution_without_fastapi
# mock_get_distribution("foobar").side_effect = DistributionNotFound()
# mock_get_distribution("fastapi-slim ~= 0.111").side_effect = DistributionNotFound()
# mock_get_distribution("fastapi ~= 0.58").side_effect = DistributionNotFound()
mock_get_dist_dependency_conflicts.side_effect = get_dist_dependency_conflicts
mock_get_dependency_conflicts.side_effect = get_dependency_conflicts
mock_get_distribution.side_effect = get_distribution_without_fastapi
mock_distro = Mock()
_load_instrumentors(mock_distro)
print("JEREVOSS mock_get_dist_dependency_conflicts.call_args_list: %s" % mock_get_dist_dependency_conflicts.call_args_list)
print("JEREVOSS mock_get_dependency_conflicts.call_args_list: %s" % mock_get_dependency_conflicts.call_args_list)
print("JEREVOSS mock_get_distribution.call_args_list: %s" % mock_get_distribution.call_args_list)
print("JEREVOSS mock_get_distribution.side_effect: %s" % mock_get_distribution.side_effect)
mock_get_distribution.assert_called_once_with("fastapi~=0.58")
print("-----\nJEREVOSS about to test error")
with self.assertRaises(DistributionNotFound):
mock_get_distribution("fastapi~=0.58")
self.assertEqual(len(mock_distro.load_instrumentor.call_args_list), 0)
# print("JEREVOSS mock_get_distribution().call_args_list: %s" % mock_get_distribution().call_args_list)
# mock_get_distribution.assert_called_once_with("fastapi ~= 0.58")
mock_distro.load_instrumentor.assert_not_called()

def _create_app(self):
Expand Down

0 comments on commit 6ad9850

Please sign in to comment.