From a36587c753eaab0737928f6ea20911f07dc56866 Mon Sep 17 00:00:00 2001 From: oliver zhang Date: Tue, 15 Oct 2024 15:20:36 +0800 Subject: [PATCH] sdk: Implement basic host resource detector (#4182) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add host resource detector * add host resource detector * update changelog * code format * Update opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py Co-authored-by: Pablo Collins * resolve conversation * check setup pre-commit * remove error import * remove error import * change get resources * add this resource detector to the list of entry points * add test_resource_detector_entry_points_host and code reformat * Add an underscore _ in front of the resource detector * Add an underscore _ in front of the resource detector * Add an underscore _ in front of the resource detector * Update opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py Co-authored-by: Emídio Neto <9735060+emdneto@users.noreply.github.com> * Remove unnecessary variable declarations --------- Co-authored-by: Pablo Collins Co-authored-by: Leighton Chen Co-authored-by: Pablo Collins Co-authored-by: Emídio Neto <9735060+emdneto@users.noreply.github.com> --- CHANGELOG.md | 2 ++ opentelemetry-sdk/pyproject.toml | 1 + .../opentelemetry/sdk/resources/__init__.py | 16 +++++++++++++ .../tests/resources/test_resources.py | 23 +++++++++++++++++++ 4 files changed, 42 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5340583fdfc..33438d21eda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#4154](https://github.com/open-telemetry/opentelemetry-python/pull/4154)) - sdk: Add support for log formatting ([#4137](https://github.com/open-telemetry/opentelemetry-python/pull/4166)) +- sdk: Add Host resource detector + ([#4182](https://github.com/open-telemetry/opentelemetry-python/pull/4182)) - sdk: Implementation of exemplars ([#4094](https://github.com/open-telemetry/opentelemetry-python/pull/4094)) - Implement events sdk diff --git a/opentelemetry-sdk/pyproject.toml b/opentelemetry-sdk/pyproject.toml index 69895bfd5b6..1a3b945d055 100644 --- a/opentelemetry-sdk/pyproject.toml +++ b/opentelemetry-sdk/pyproject.toml @@ -68,6 +68,7 @@ console = "opentelemetry.sdk.trace.export:ConsoleSpanExporter" otel = "opentelemetry.sdk.resources:OTELResourceDetector" process = "opentelemetry.sdk.resources:ProcessResourceDetector" os = "opentelemetry.sdk.resources:OsResourceDetector" +host = "opentelemetry.sdk.resources:_HostResourceDetector" [project.urls] Homepage = "https://github.com/open-telemetry/opentelemetry-python/tree/main/opentelemetry-sdk" diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py index 0ebd42349c4..bbec2850701 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -60,6 +60,7 @@ import logging import os import platform +import socket import sys import typing from json import dumps @@ -105,6 +106,7 @@ FAAS_VERSION = ResourceAttributes.FAAS_VERSION FAAS_INSTANCE = ResourceAttributes.FAAS_INSTANCE HOST_NAME = ResourceAttributes.HOST_NAME +HOST_ARCH = ResourceAttributes.HOST_ARCH HOST_TYPE = ResourceAttributes.HOST_TYPE HOST_IMAGE_NAME = ResourceAttributes.HOST_IMAGE_NAME HOST_IMAGE_ID = ResourceAttributes.HOST_IMAGE_ID @@ -471,6 +473,20 @@ def detect(self) -> "Resource": ) +class _HostResourceDetector(ResourceDetector): + """ + The HostResourceDetector detects the hostname and architecture attributes. + """ + + def detect(self) -> "Resource": + return Resource( + { + HOST_NAME: socket.gethostname(), + HOST_ARCH: platform.machine(), + } + ) + + def get_aggregated_resources( detectors: typing.List["ResourceDetector"], initial_resource: typing.Optional[Resource] = None, diff --git a/opentelemetry-sdk/tests/resources/test_resources.py b/opentelemetry-sdk/tests/resources/test_resources.py index 70e90864a53..1cceeb53a2b 100644 --- a/opentelemetry-sdk/tests/resources/test_resources.py +++ b/opentelemetry-sdk/tests/resources/test_resources.py @@ -28,6 +28,8 @@ _DEFAULT_RESOURCE, _EMPTY_RESOURCE, _OPENTELEMETRY_SDK_VERSION, + HOST_ARCH, + HOST_NAME, OS_TYPE, OS_VERSION, OTEL_RESOURCE_ATTRIBUTES, @@ -52,6 +54,7 @@ ProcessResourceDetector, Resource, ResourceDetector, + _HostResourceDetector, get_aggregated_resources, ) @@ -777,3 +780,23 @@ def test_os_detector_solaris(self): self.assertEqual(resource.attributes[OS_TYPE], "solaris") self.assertEqual(resource.attributes[OS_VERSION], "666.4.0.15.0") + + +class TestHostResourceDetector(unittest.TestCase): + @patch("socket.gethostname", lambda: "foo") + @patch("platform.machine", lambda: "AMD64") + def test_host_resource_detector(self): + resource = get_aggregated_resources( + [_HostResourceDetector()], + Resource({}), + ) + self.assertEqual(resource.attributes[HOST_NAME], "foo") + self.assertEqual(resource.attributes[HOST_ARCH], "AMD64") + + @patch.dict( + environ, {OTEL_EXPERIMENTAL_RESOURCE_DETECTORS: "host"}, clear=True + ) + def test_resource_detector_entry_points_host(self): + resource = Resource({}).create() + self.assertIn(HOST_NAME, resource.attributes) + self.assertIn(HOST_ARCH, resource.attributes)