Skip to content

Commit

Permalink
Add descriptive error
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Feb 1, 2023
1 parent af6d00f commit 5efa728
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
33 changes: 23 additions & 10 deletions opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 34 additions & 0 deletions opentelemetry-sdk/tests/test_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,6 +32,7 @@
_get_exporter_names,
_get_id_generator,
_get_sampler,
_import_config_components,
_import_exporters,
_import_id_generator,
_import_sampler,
Expand Down Expand Up @@ -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'",
)

0 comments on commit 5efa728

Please sign in to comment.