Skip to content

Commit 830fd1b

Browse files
committed
Rework error handling after comments
1 parent dd7cef0 commit 830fd1b

2 files changed

Lines changed: 44 additions & 8 deletions

File tree

opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,25 @@ def _import_opamp() -> Callable[[...], None]:
528528
# the resource and instantiate an OpAMP agent.
529529
# Since configuration is not specified every implementors may have its own.
530530
# Refer to opentelemetry-opamp-client package on how to setup the OpAMP agent.
531-
_, opamp_init_func = _import_config_components(
532-
["init_function"], "_opentelemetry_opamp"
533-
)[0]
531+
try:
532+
entry_point = next(
533+
iter(
534+
entry_points(
535+
group="_opentelemetry_opamp", name="init_function"
536+
)
537+
)
538+
)
539+
return entry_point.load()
540+
except StopIteration:
541+
_logger.debug("No OpAMP init function found")
542+
except AttributeError as exc:
543+
_logger.warning(
544+
"Failed to load OpAMP init function from entry point, %s: %s",
545+
entry_point,
546+
exc,
547+
)
534548

535-
return opamp_init_func
549+
return None
536550

537551

538552
def _initialize_components(
@@ -598,11 +612,9 @@ def _initialize_components(
598612
# provided code as it's not strictly specified. We call OpAMP init before other code
599613
# because people may want to have it blocking to get an updated config before setting
600614
# up the rest of the SDK.
601-
try:
602-
_init_opamp = _import_opamp()
615+
_init_opamp = _import_opamp()
616+
if _init_opamp is not None:
603617
_init_opamp(resource=resource)
604-
except RuntimeError:
605-
_logger.debug("No OpAMP init function found")
606618

607619
_init_tracing(
608620
exporters=span_exporters,

opentelemetry-sdk/tests/test_configurator.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,30 @@ def test_init_function_found(self, mock_resource, mock_entry_points):
15281528
resource=mock_resource.create.return_value
15291529
)
15301530

1531+
@patch("opentelemetry.sdk._configuration.entry_points")
1532+
def test_init_function_load_failure(self, mock_entry_points):
1533+
entry_point_mock = mock.Mock()
1534+
entry_point_mock.load.side_effect = AttributeError(
1535+
"module 'foo' has no attribute 'OpampInit'"
1536+
)
1537+
mock_entry_points.configure_mock(
1538+
return_value=[entry_point_mock],
1539+
)
1540+
entry_point_mock.__str__ = lambda x: "<EntryPoint>"
1541+
1542+
with self.assertLogs(level="WARNING") as cm:
1543+
_initialize_components(id_generator=1)
1544+
1545+
mock_entry_points.assert_has_calls(
1546+
[mock.call(group="_opentelemetry_opamp", name="init_function")]
1547+
)
1548+
1549+
self.assertIn(
1550+
"WARNING:opentelemetry.sdk._configuration:Failed to load OpAMP init function from entry point,"
1551+
" <EntryPoint>: module 'foo' has no attribute 'OpampInit'",
1552+
cm.output,
1553+
)
1554+
15311555
@patch("opentelemetry.sdk._configuration.entry_points")
15321556
def test_init_function_not_found(self, mock_entry_points):
15331557
mock_entry_points.configure_mock(return_value=[])

0 commit comments

Comments
 (0)