From 5efa728466dc68f88b06d880a53396b25c5c1c3c Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Fri, 20 Jan 2023 19:01:17 -0600 Subject: [PATCH] Add descriptive error --- .../sdk/_configuration/__init__.py | 33 ++++++++++++------ opentelemetry-sdk/tests/test_configurator.py | 34 +++++++++++++++++++ 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py index 1c2272a90cd..a65ed85d84d 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -94,18 +94,31 @@ def _import_config_components( component_implementations = [] for selected_component in selected_components: - component_implementations.append( - ( - selected_component, - next( - iter( - entry_points( - group=entry_point_name, name=selected_component + try: + component_implementations.append( + ( + selected_component, + next( + iter( + entry_points( + group=entry_point_name, name=selected_component + ) ) - ) - ).load(), + ).load(), + ) + ) + except KeyError: + + raise RuntimeError( + f"Requested entry point '{entry_point_name}' not found" + ) + + except StopIteration: + + raise RuntimeError( + f"Requested component '{selected_component}' not found in " + f"entry point '{entry_point_name}'" ) - ) return component_implementations diff --git a/opentelemetry-sdk/tests/test_configurator.py b/opentelemetry-sdk/tests/test_configurator.py index c17bd62c72c..e07c5e3c787 100644 --- a/opentelemetry-sdk/tests/test_configurator.py +++ b/opentelemetry-sdk/tests/test_configurator.py @@ -20,6 +20,8 @@ from unittest import TestCase from unittest.mock import patch +from pytest import raises + from opentelemetry import trace from opentelemetry.context import Context from opentelemetry.environment_variables import OTEL_PYTHON_ID_GENERATOR @@ -30,6 +32,7 @@ _get_exporter_names, _get_id_generator, _get_sampler, + _import_config_components, _import_exporters, _import_id_generator, _import_sampler, @@ -780,3 +783,34 @@ def test_console_exporters(self): metric_exporterts["console"].__class__, ConsoleMetricExporter.__class__, ) + + +class TestImportConfigComponents(TestCase): + @patch( + "opentelemetry.sdk._configuration.entry_points", + **{"side_effect": KeyError}, + ) + def test__import_config_components_missing_entry_point( + self, mock_entry_points + ): + + with raises(RuntimeError) as error: + _import_config_components(["a", "b", "c"], "name") + self.assertEqual( + str(error.value), "Requested entry point 'name' not found" + ) + + @patch( + "opentelemetry.sdk._configuration.entry_points", + **{"side_effect": StopIteration}, + ) + def test__import_config_components_missing_component( + self, mock_entry_points + ): + + with raises(RuntimeError) as error: + _import_config_components(["a", "b", "c"], "name") + self.assertEqual( + str(error.value), + "Requested component 'a' not found in entry point 'name'", + )