Skip to content

Commit a7fe4f8

Browse files
cruisehallxrmxlzchen
authored
Tolerate exceptions among experimental resource detectors (#4373)
* 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]>
1 parent 87c9675 commit a7fe4f8

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Tolerates exceptions when loading resource detectors via `OTEL_EXPERIMENTAL_RESOURCE_DETECTORS`
11+
([#4373](https://github.com/open-telemetry/opentelemetry-python/pull/4373))
12+
1013
## Version 1.30.0/0.51b0 (2025-02-03)
1114

1215
- Always setup logs sdk, OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED only controls python `logging` module handler setup

Diff for: opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,23 @@ def create(
203203

204204
resource_detector: str
205205
for resource_detector in otel_experimental_resource_detectors:
206-
resource_detectors.append(
207-
next(
208-
iter(
209-
entry_points(
210-
group="opentelemetry_resource_detector",
211-
name=resource_detector.strip(),
212-
) # type: ignore
213-
)
214-
).load()()
215-
)
206+
try:
207+
resource_detectors.append(
208+
next(
209+
iter(
210+
entry_points(
211+
group="opentelemetry_resource_detector",
212+
name=resource_detector.strip(),
213+
) # type: ignore
214+
)
215+
).load()()
216+
)
217+
except Exception: # pylint: disable=broad-exception-caught
218+
logger.exception(
219+
"Failed to load resource detector '%s', skipping",
220+
resource_detector,
221+
)
222+
continue
216223
resource = get_aggregated_resources(
217224
resource_detectors, _DEFAULT_RESOURCE
218225
).merge(Resource(attributes, schema_url))

Diff for: opentelemetry-sdk/tests/resources/test_resources.py

+12
Original file line numberDiff line numberDiff line change
@@ -800,3 +800,15 @@ def test_resource_detector_entry_points_host(self):
800800
resource = Resource({}).create()
801801
self.assertIn(HOST_NAME, resource.attributes)
802802
self.assertIn(HOST_ARCH, resource.attributes)
803+
804+
@patch.dict(
805+
environ,
806+
{OTEL_EXPERIMENTAL_RESOURCE_DETECTORS: "doesnotexist,host"},
807+
clear=True,
808+
)
809+
def test_resource_detector_entry_points_tolerate_missing_detector(self):
810+
resource = Resource({}).create()
811+
self.assertEqual(
812+
resource.attributes["telemetry.sdk.language"], "python"
813+
)
814+
self.assertIn(HOST_NAME, resource.attributes)

0 commit comments

Comments
 (0)