Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from urllib import parse

from opentelemetry.configuration._common import load_entry_point
from opentelemetry.configuration._exceptions import ConfigurationError
from opentelemetry.configuration.models import (
AttributeNameValue,
AttributeType,
Expand Down Expand Up @@ -174,22 +175,31 @@ def _run_detectors(
unknown plugin detectors captured in additional_properties — are
loaded via the ``opentelemetry_resource_detector`` entry point group.

A detector that cannot be loaded, or that fails inside ``detect()``, is
logged and skipped. The remaining detectors still run, and so does the
rest of the SDK configuration that follows resource creation.

The detected_attrs dict is updated in-place; later detectors overwrite
earlier ones for the same key.
"""
for name in dataclasses.fields(detector_config):
value = getattr(detector_config, name.name, None)
if value is None:
continue
if name.name in _RESOURCE_DETECTOR_REGISTRY:
detected_attrs.update(_RESOURCE_DETECTOR_REGISTRY[name.name](value))
else:
cls = load_entry_point("opentelemetry_resource_detector", name.name)
detected_attrs.update(cls(**(value or {})).detect().attributes)

for name, plugin_config in detector_config.additional_properties.items():
cls = load_entry_point("opentelemetry_resource_detector", name)
detected_attrs.update(cls(**(plugin_config or {})).detect().attributes)
requested: list[tuple[str, Any]] = [
(field.name, value)
for field in dataclasses.fields(detector_config)
if (value := getattr(detector_config, field.name, None)) is not None
]
requested.extend(detector_config.additional_properties.items())

for name, value in requested:
try:
if name in _RESOURCE_DETECTOR_REGISTRY:
detected_attrs.update(_RESOURCE_DETECTOR_REGISTRY[name](value))
else:
cls = load_entry_point("opentelemetry_resource_detector", name)
detected_attrs.update(cls(**(value or {})).detect().attributes)
except ConfigurationError as exc:
_logger.warning("Skipping resource detector '%s' in declarative config: %s", name, exc)
except Exception: # pylint: disable=broad-except
_logger.exception("Failed to run resource detector '%s' from declarative config", name)


def _filter_attributes(attrs: dict[str, object], filter_config: IncludeExclude | None) -> dict[str, object]:
Expand Down
61 changes: 53 additions & 8 deletions opentelemetry-configuration/tests/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from unittest.mock import MagicMock, patch

from opentelemetry.configuration._conversion import _dict_to_dataclass
from opentelemetry.configuration._exceptions import ConfigurationError
from opentelemetry.configuration._resource import create_resource
from opentelemetry.configuration.models import (
AttributeNameValue,
Expand Down Expand Up @@ -444,16 +443,59 @@ def test_container_detector_not_run_when_detectors_list_empty(self):
resource = create_resource(config)
self.assertNotIn(CONTAINER_ID, resource.attributes)

def test_container_detector_raises_when_package_missing(self):
"""ConfigurationError is raised when the contrib entry point is not found."""
def test_container_detector_missing_package_is_skipped_instead_of_failing_the_resource(self):
"""A detector whose package is absent is warned about, and the resource is still built."""
with (
patch(
"opentelemetry.configuration._common.entry_points",
return_value=[],
),
self.assertRaises(ConfigurationError),
self.assertLogs("opentelemetry.configuration._resource", level="WARNING") as logs,
):
create_resource(self._config_with_container())
resource = create_resource(self._config_with_container())

self.assertNotIn(CONTAINER_ID, resource.attributes)
self.assertEqual(resource.attributes[TELEMETRY_SDK_LANGUAGE], "python")
self.assertIn("container", logs.output[0])

def test_container_detector_error_inside_detect_is_skipped_instead_of_failing_the_resource(self):
"""A detector that raises from detect() is warned about, and the resource is still built."""
mock_detector = MagicMock()
mock_detector.return_value.detect.side_effect = RuntimeError("no cgroup here")
mock_ep = MagicMock()
mock_ep.load.return_value = mock_detector

with (
patch(
"opentelemetry.configuration._common.entry_points",
return_value=[mock_ep],
),
self.assertLogs("opentelemetry.configuration._resource", level="ERROR") as logs,
):
resource = create_resource(self._config_with_container())

self.assertNotIn(CONTAINER_ID, resource.attributes)
self.assertEqual(resource.attributes[TELEMETRY_SDK_LANGUAGE], "python")
self.assertIn("container", logs.output[0])

def test_a_missing_container_detector_does_not_stop_the_host_detector(self):
"""Detectors listed after one that cannot be loaded still contribute attributes."""
config = ResourceConfig(
detection_development=ExperimentalResourceDetection(
detectors=[ExperimentalResourceDetector(container={}, host={})]
)
)
with (
patch(
"opentelemetry.configuration._common.entry_points",
return_value=[],
),
self.assertLogs("opentelemetry.configuration._resource", level="WARNING"),
):
resource = create_resource(config)

self.assertNotIn(CONTAINER_ID, resource.attributes)
self.assertEqual(resource.attributes[HOST_NAME], socket.gethostname())

def test_container_detector_uses_contrib_when_available(self):
"""When the contrib entry point is registered, container.id is detected."""
Expand Down Expand Up @@ -571,7 +613,7 @@ def test_plugin_detector_loaded_via_entry_point(self):

self.assertEqual(resource.attributes["custom.attr"], "value")

def test_unknown_detector_raises_configuration_error(self):
def test_unknown_detector_is_skipped_instead_of_failing_the_resource(self):
config = ResourceConfig(
detection_development=ExperimentalResourceDetection(
# pylint: disable=unexpected-keyword-arg
Expand All @@ -583,6 +625,9 @@ def test_unknown_detector_raises_configuration_error(self):
"opentelemetry.configuration._common.entry_points",
return_value=[],
),
self.assertRaises(ConfigurationError),
self.assertLogs("opentelemetry.configuration._resource", level="WARNING") as logs,
):
create_resource(config)
resource = create_resource(config)

self.assertEqual(resource.attributes[TELEMETRY_SDK_LANGUAGE], "python")
self.assertIn("no_such_detector", logs.output[0])
Loading