Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk: Implement basic host resource detector #4182

Merged
merged 32 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0db71e9
add host resource detector
oliver-zhang Sep 7, 2024
55d984d
add host resource detector
oliver-zhang Sep 7, 2024
3f2277e
update changelog
oliver-zhang Sep 7, 2024
e7f5bba
code format
oliver-zhang Sep 11, 2024
e30e79e
Update opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
oliver-zhang Sep 12, 2024
ac2a828
resolve conversation
oliver-zhang Sep 12, 2024
5ddc94a
Merge branch 'main' into resource-detector-host
oliver-zhang Sep 12, 2024
6093e34
check setup pre-commit
oliver-zhang Sep 13, 2024
c0e5856
Merge remote-tracking branch 'origin/resource-detector-host' into res…
oliver-zhang Sep 13, 2024
d162529
remove error import
oliver-zhang Sep 13, 2024
1e9e9c2
remove error import
oliver-zhang Sep 13, 2024
96f31d7
change get resources
oliver-zhang Sep 13, 2024
c1a87de
Merge branch 'main' into resource-detector-host
oliver-zhang Sep 18, 2024
13d6cbe
add this resource detector to the list of entry points
oliver-zhang Sep 18, 2024
0361ee5
Merge remote-tracking branch 'origin/resource-detector-host' into res…
oliver-zhang Sep 18, 2024
5d27915
add test_resource_detector_entry_points_host and code reformat
oliver-zhang Sep 19, 2024
ca63ce8
Merge branch 'main' into resource-detector-host
lzchen Sep 19, 2024
d4e4952
Add an underscore _ in front of the resource detector
oliver-zhang Sep 20, 2024
bb1740a
Merge remote-tracking branch 'origin/resource-detector-host' into res…
oliver-zhang Sep 20, 2024
0ea2c14
Merge branch 'main' into resource-detector-host
oliver-zhang Sep 20, 2024
bd6263e
Add an underscore _ in front of the resource detector
oliver-zhang Sep 21, 2024
0685f3b
Merge remote-tracking branch 'origin/resource-detector-host' into res…
oliver-zhang Sep 21, 2024
f31da4b
Add an underscore _ in front of the resource detector
oliver-zhang Sep 24, 2024
a058cb2
Merge branch 'main' into resource-detector-host
pmcollins Oct 1, 2024
a28026b
Merge branch 'main' into resource-detector-host
emdneto Oct 3, 2024
0d9a6d3
Update opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
oliver-zhang Oct 8, 2024
e57c902
Remove unnecessary variable declarations
oliver-zhang Oct 8, 2024
260792b
Merge branch 'main' into resource-detector-host
emdneto Oct 8, 2024
73b2ea4
Merge branch 'main' into resource-detector-host
emdneto Oct 10, 2024
484f25a
Merge branch 'main' into resource-detector-host
lzchen Oct 14, 2024
ebc3f2c
Merge branch 'main' into resource-detector-host
lzchen Oct 14, 2024
bae33a3
Merge branch 'main' into resource-detector-host
lzchen Oct 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
oliver-zhang marked this conversation as resolved.
Show resolved Hide resolved
@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)