|
class ResourceDetector(abc.ABC): |
|
def __init__(self, raise_on_error=False): |
|
self.raise_on_error = raise_on_error |
|
|
|
@abc.abstractmethod |
|
def detect(self) -> "Resource": |
|
raise NotImplementedError() |
|
|
|
|
|
class OTELResourceDetector(ResourceDetector): |
|
# pylint: disable=no-self-use |
|
def detect(self) -> "Resource": |
|
env_resources_items = os.environ.get("OTEL_RESOURCE_ATTRIBUTES") |
|
env_resource_map = {} |
|
if env_resources_items: |
|
env_resource_map = { |
|
key.strip(): value.strip() |
|
for key, value in ( |
|
item.split("=") for item in env_resources_items.split(",") |
|
) |
|
} |
|
return Resource(env_resource_map) |
|
|
|
|
|
def get_aggregated_resources( |
|
detectors: typing.List["ResourceDetector"], |
|
initial_resource: typing.Optional[Resource] = None, |
|
timeout=5, |
|
) -> "Resource": |
|
""" Retrieves resources from detectors in the order that they were passed |
|
|
|
:param detectors: List of resources in order of priority |
|
:param initial_resource: Static resource. This has highest priority |
|
:param timeout: Number of seconds to wait for each detector to return |
|
:return: |
|
""" |
|
final_resource = initial_resource or _EMPTY_RESOURCE |
|
detectors = [OTELResourceDetector()] + detectors |
|
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor: |
|
futures = [executor.submit(detector.detect) for detector in detectors] |
|
for detector_ind, future in enumerate(futures): |
|
detector = detectors[detector_ind] |
|
try: |
|
detected_resources = future.result(timeout=timeout) |
|
# pylint: disable=broad-except |
|
except Exception as ex: |
|
if detector.raise_on_error: |
|
raise ex |
|
logger.warning( |
|
"Exception %s in detector %s, ignoring", ex, detector |
|
) |
|
detected_resources = _EMPTY_RESOURCE |
|
finally: |
|
final_resource = final_resource.merge(detected_resources) |
|
return final_resource |
Resource detection has been merged into the spec. The SDK should be aligned with the spec. This may already be the case.
Is your feature request related to a problem?
No
Describe the solution you'd like
The resource detection spec is flexible and doesn't prescribe anything. Currently, we have this code:
opentelemetry-python/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
Lines 65 to 120 in c435600
We could:
ResourceDetectorinterface orget_aggregated_resources()nor does it say it shouldn't be there.ResourceDetectorinterface andget_aggregated_resources()ResourceDetectorinterface with aCallable[[bool], Resource]function param, since the spec only saysThoughts?