Skip to content

Commit

Permalink
Tolerate exceptions among experimental resource detectors (#4373)
Browse files Browse the repository at this point in the history
* feat: Tolerate exceptions among experimental resource detectors

OTEL resource creation will proceed even if one of the resource detector entry points indicated by the `OTEL_EXPERIMENTAL_RESOURCE_DETECTORS` env var fails to load. In fact, subsequent resource detector entry points will continue to be processed as well.

* test: 'Resource.create' tolerates missing resource detector

* test: Adds additional assertion to 'test_resource_detector_entry_points_tolerate_missing_detector'

* chore: Updates CHANGELOG

* feat: Logs exception when skipping a resource detector

Emulates auto-instrumentation log emitted when an instrumentor fails to load https://github.com/open-telemetry/opentelemetry-python-contrib/blob/v0.50b0/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/_load.py#L128

* Update CHANGELOG.md

* fix: Corrects var reference to `resource_detector`, removes unused `exc` var

* chore: Auto-formats code using 'tox -e ruff'

---------

Co-authored-by: Riccardo Magliocchetti <[email protected]>
Co-authored-by: Leighton Chen <[email protected]>
  • Loading branch information
3 people authored Feb 5, 2025
1 parent 87c9675 commit a7fe4f8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Tolerates exceptions when loading resource detectors via `OTEL_EXPERIMENTAL_RESOURCE_DETECTORS`
([#4373](https://github.com/open-telemetry/opentelemetry-python/pull/4373))

## Version 1.30.0/0.51b0 (2025-02-03)

- Always setup logs sdk, OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED only controls python `logging` module handler setup
Expand Down
27 changes: 17 additions & 10 deletions opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,23 @@ def create(

resource_detector: str
for resource_detector in otel_experimental_resource_detectors:
resource_detectors.append(
next(
iter(
entry_points(
group="opentelemetry_resource_detector",
name=resource_detector.strip(),
) # type: ignore
)
).load()()
)
try:
resource_detectors.append(
next(
iter(
entry_points(
group="opentelemetry_resource_detector",
name=resource_detector.strip(),
) # type: ignore
)
).load()()
)
except Exception: # pylint: disable=broad-exception-caught
logger.exception(
"Failed to load resource detector '%s', skipping",
resource_detector,
)
continue
resource = get_aggregated_resources(
resource_detectors, _DEFAULT_RESOURCE
).merge(Resource(attributes, schema_url))
Expand Down
12 changes: 12 additions & 0 deletions opentelemetry-sdk/tests/resources/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,3 +800,15 @@ def test_resource_detector_entry_points_host(self):
resource = Resource({}).create()
self.assertIn(HOST_NAME, resource.attributes)
self.assertIn(HOST_ARCH, resource.attributes)

@patch.dict(
environ,
{OTEL_EXPERIMENTAL_RESOURCE_DETECTORS: "doesnotexist,host"},
clear=True,
)
def test_resource_detector_entry_points_tolerate_missing_detector(self):
resource = Resource({}).create()
self.assertEqual(
resource.attributes["telemetry.sdk.language"], "python"
)
self.assertIn(HOST_NAME, resource.attributes)

0 comments on commit a7fe4f8

Please sign in to comment.