Skip to content

Commit 52137e0

Browse files
Allow loading all resource detectors by setting OTEL_EXPERIMENTAL_RESOURCE_DETECTORS to * (#4819)
* Allow loading all resource detectors by setting `OTEL_EXPERIMENTAL_RESOURCE_DETECTORS` to `all` * Add changelog * Use `*` instead of `all`
1 parent 1ac0158 commit 52137e0

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212

1313
## Unreleased
1414

15+
- Allow loading all resource detectors by setting `OTEL_EXPERIMENTAL_RESOURCE_DETECTORS` to `*`
16+
([#4819](https://github.com/open-telemetry/opentelemetry-python/pull/4819))
1517
- `opentelemetry-sdk`: Fix the type hint of the `_metrics_data` property to allow `None`
1618
([#4837](https://github.com/open-telemetry/opentelemetry-python/pull/4837)).
1719
- Regenerate opentelemetry-proto code with v1.9.0 release

opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def _original_entry_points_cached():
3636
return original_entry_points()
3737

3838

39-
def entry_points(**params):
39+
def entry_points(**params) -> EntryPoints:
4040
"""Replacement for importlib_metadata.entry_points that caches getting all the entry points.
4141
4242
That part can be very slow, and OTel uses this function many times."""

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
from json import dumps
7171
from os import environ
7272
from types import ModuleType
73-
from typing import List, Optional, cast
73+
from typing import List, Optional, Set, cast
7474
from urllib import parse
7575

7676
from opentelemetry.attributes import BoundedAttributes
@@ -195,7 +195,7 @@ def create(
195195
if not attributes:
196196
attributes = {}
197197

198-
otel_experimental_resource_detectors = {"otel"}.union(
198+
otel_experimental_resource_detectors: Set[str] = {"otel"}.union(
199199
{
200200
otel_experimental_resource_detector.strip()
201201
for otel_experimental_resource_detector in environ.get(
@@ -207,7 +207,11 @@ def create(
207207

208208
resource_detectors: List[ResourceDetector] = []
209209

210-
resource_detector: str
210+
if "*" in otel_experimental_resource_detectors:
211+
otel_experimental_resource_detectors = entry_points(
212+
group="opentelemetry_resource_detector"
213+
).names
214+
211215
for resource_detector in otel_experimental_resource_detectors:
212216
try:
213217
resource_detectors.append(

opentelemetry-sdk/tests/resources/test_resources.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ def test_service_name_env(self):
474474
self.assertEqual(resource.attributes["service.name"], "from-code")
475475

476476

477+
# pylint: disable=too-many-public-methods
477478
class TestOTELResourceDetector(unittest.TestCase):
478479
def setUp(self) -> None:
479480
environ[OTEL_RESOURCE_ATTRIBUTES] = ""
@@ -697,6 +698,31 @@ def test_resource_detector_entry_points_os(self):
697698
self.assertIn(OS_TYPE, resource.attributes)
698699
self.assertIn(OS_VERSION, resource.attributes)
699700

701+
@patch.dict(
702+
environ, {OTEL_EXPERIMENTAL_RESOURCE_DETECTORS: "*"}, clear=True
703+
)
704+
def test_resource_detector_entry_points_all(self):
705+
resource = Resource({}).create()
706+
707+
self.assertIn(
708+
TELEMETRY_SDK_NAME,
709+
resource.attributes,
710+
"'otel' resource detector not enabled",
711+
)
712+
self.assertIn(
713+
OS_TYPE, resource.attributes, "'os' resource detector not enabled"
714+
)
715+
self.assertIn(
716+
HOST_ARCH,
717+
resource.attributes,
718+
"'host' resource detector not enabled",
719+
)
720+
self.assertIn(
721+
PROCESS_RUNTIME_NAME,
722+
resource.attributes,
723+
"'process' resource detector not enabled",
724+
)
725+
700726
def test_resource_detector_entry_points_otel(self):
701727
"""
702728
Test that OTELResourceDetector-resource-generated attributes are

0 commit comments

Comments
 (0)