Skip to content

Commit

Permalink
sdk: Implement basic host resource detector (open-telemetry#4182)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>

* Remove unnecessary variable declarations

---------

Co-authored-by: Pablo Collins <[email protected]>
Co-authored-by: Leighton Chen <[email protected]>
Co-authored-by: Pablo Collins <[email protected]>
Co-authored-by: Emídio Neto <[email protected]>
  • Loading branch information
5 people authored Oct 15, 2024
1 parent d5c54e3 commit a36587c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions opentelemetry-sdk/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 16 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import logging
import os
import platform
import socket
import sys
import typing
from json import dumps
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions opentelemetry-sdk/tests/resources/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
_DEFAULT_RESOURCE,
_EMPTY_RESOURCE,
_OPENTELEMETRY_SDK_VERSION,
HOST_ARCH,
HOST_NAME,
OS_TYPE,
OS_VERSION,
OTEL_RESOURCE_ATTRIBUTES,
Expand All @@ -52,6 +54,7 @@
ProcessResourceDetector,
Resource,
ResourceDetector,
_HostResourceDetector,
get_aggregated_resources,
)

Expand Down Expand Up @@ -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)

0 comments on commit a36587c

Please sign in to comment.