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 os resource detector #3992

Merged
merged 18 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#3991](https://github.com/open-telemetry/opentelemetry-python/pull/3991))
- Add attributes field in `MeterProvider.get_meter` and `InstrumentationScope`
([#4015](https://github.com/open-telemetry/opentelemetry-python/pull/4015))
- sdk: Add an OS resource detector as a default detector
([#3992](https://github.com/open-telemetry/opentelemetry-python/pull/3992))

## Version 1.25.0/0.46b0 (2024-05-30)

Expand Down Expand Up @@ -1613,4 +1615,3 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#3778](https://github.com/open-telemetry/opentelemetry-python/pull/3778))
- Fix license field in pyproject.toml files
([#3803](https://github.com/open-telemetry/opentelemetry-python/pull/3803))

1 change: 1 addition & 0 deletions opentelemetry-sdk/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ console = "opentelemetry.sdk.trace.export:ConsoleSpanExporter"
[project.entry-points.opentelemetry_resource_detector]
otel = "opentelemetry.sdk.resources:OTELResourceDetector"
process = "opentelemetry.sdk.resources:ProcessResourceDetector"
os = "opentelemetry.sdk.resources:OsResourceDetector"

[project.urls]
Homepage = "https://github.com/open-telemetry/opentelemetry-python/tree/main/opentelemetry-sdk"
Expand Down
108 changes: 97 additions & 11 deletions opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import concurrent.futures
import logging
import os
import platform
import sys
import typing
from json import dumps
Expand Down Expand Up @@ -119,8 +120,9 @@
KUBERNETES_JOB_NAME = ResourceAttributes.K8S_JOB_NAME
KUBERNETES_CRON_JOB_UID = ResourceAttributes.K8S_CRONJOB_UID
KUBERNETES_CRON_JOB_NAME = ResourceAttributes.K8S_CRONJOB_NAME
OS_TYPE = ResourceAttributes.OS_TYPE
OS_DESCRIPTION = ResourceAttributes.OS_DESCRIPTION
OS_TYPE = ResourceAttributes.OS_TYPE
OS_VERSION = ResourceAttributes.OS_VERSION
PROCESS_PID = ResourceAttributes.PROCESS_PID
PROCESS_PARENT_PID = ResourceAttributes.PROCESS_PARENT_PID
PROCESS_EXECUTABLE_NAME = ResourceAttributes.PROCESS_EXECUTABLE_NAME
Expand Down Expand Up @@ -173,17 +175,17 @@ def create(
if not attributes:
attributes = {}

resource_detectors = []

resource = _DEFAULT_RESOURCE

otel_experimental_resource_detectors = environ.get(
OTEL_EXPERIMENTAL_RESOURCE_DETECTORS, "otel"
).split(",")

if "otel" not in otel_experimental_resource_detectors:
otel_experimental_resource_detectors.append("otel")
otel_experimental_resource_detectors = {"otel", "os"}.union(
{
otel_experimental_resource_detector.strip()
for otel_experimental_resource_detector in environ.get(
OTEL_EXPERIMENTAL_RESOURCE_DETECTORS, ""
).split(",")
if otel_experimental_resource_detector
}
)

resource_detectors = []
for resource_detector in otel_experimental_resource_detectors:
resource_detectors.append(
next(
Expand Down Expand Up @@ -371,6 +373,90 @@ def detect(self) -> "Resource":
return Resource(resource_info)


class OsResourceDetector(ResourceDetector):
"""Detect os resources based on `Operating System conventions <https://opentelemetry.io/docs/specs/semconv/resource/os/>`_."""

def detect(self) -> "Resource":
"""Returns a resource with with ``os.type`` and ``os.version``.

Python's platform library
~~~~~~~~~~~~~~~~~~~~~~~~~

To grab this information, Python's ``platform`` does not return what a
user might expect it to. Below is a breakdown of its return values in
different operating systems.

.. code-block:: python
:caption: Linux

>>> platform.system()
'Linux'
>>> platform.release()
'6.5.0-35-generic'
>>> platform.version()
'#35~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue May 7 09:00:52 UTC 2'

.. code-block:: python
:caption: MacOS

>>> platform.system()
'Darwin'
>>> platform.release()
'23.0.0'
>>> platform.version()
'Darwin Kernel Version 23.0.0: Fri Sep 15 14:42:57 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T8112'

.. code-block:: python
:caption: Windows

>>> platform.system()
'Windows'
>>> platform.release()
'2022Server'
>>> platform.version()
'10.0.20348'

.. code-block:: python
:caption: FreeBSD

>>> platform.system()
'FreeBSD'
>>> platform.release()
'14.1-RELEASE'
>>> platform.version()
'FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC'

.. code-block:: python
:caption: Solaris

>>> platform.system()
'SunOS'
>>> platform.release()
'5.11'
>>> platform.version()
'11.4.0.15.0'

"""

os_type = platform.system().lower()
os_version = platform.release()

# See docstring
if os_type == "windows":
Zirak marked this conversation as resolved.
Show resolved Hide resolved
os_version = platform.version()
# Align SunOS with conventions
elif os_type == "sunos":
os_type = "solaris"
os_version = platform.version()

return Resource(
{
OS_TYPE: os_type,
OS_VERSION: os_version,
}
)


def get_aggregated_resources(
detectors: typing.List["ResourceDetector"],
initial_resource: typing.Optional[Resource] = None,
Expand Down
103 changes: 92 additions & 11 deletions opentelemetry-sdk/tests/resources/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import platform
import sys
import unittest
import uuid
Expand All @@ -28,6 +29,8 @@
_DEFAULT_RESOURCE,
_EMPTY_RESOURCE,
_OPENTELEMETRY_SDK_VERSION,
OS_TYPE,
OS_VERSION,
OTEL_RESOURCE_ATTRIBUTES,
OTEL_SERVICE_NAME,
PROCESS_COMMAND,
Expand All @@ -45,6 +48,7 @@
TELEMETRY_SDK_LANGUAGE,
TELEMETRY_SDK_NAME,
TELEMETRY_SDK_VERSION,
OsResourceDetector,
OTELResourceDetector,
ProcessResourceDetector,
Resource,
Expand All @@ -58,10 +62,43 @@
psutil = None


def get_mock_uname():
"""Generate a mock uname. There are different signatures in different python
versions."""

kwargs = {
"system": "Linux",
"node": "node",
"release": "1.2.3",
"version": "4.5.6",
"machine": "x86_64",
}

if sys.version_info < (3, 9):
kwargs["processor"] = "x86_64"

return platform.uname_result(**kwargs)


@patch(
"platform.uname",
get_mock_uname,
)
class TestResources(unittest.TestCase):
def setUp(self) -> None:
environ[OTEL_RESOURCE_ATTRIBUTES] = ""

# OsResourceDetector calls into `platform` functions to grab its info,
# which is not part of the default resource. Provide the mock values
# and an extended default resource to be used throughout tests.
self.mock_platform = {
OS_TYPE: "linux",
OS_VERSION: "1.2.3",
}
self.default_resource = _DEFAULT_RESOURCE.merge(
Resource(self.mock_platform)
)

def tearDown(self) -> None:
environ.pop(OTEL_RESOURCE_ATTRIBUTES)

Expand All @@ -83,6 +120,7 @@ def test_create(self):
TELEMETRY_SDK_VERSION: _OPENTELEMETRY_SDK_VERSION,
SERVICE_NAME: "unknown_service",
}
expected_attributes.update(self.mock_platform)

resource = Resource.create(attributes)
self.assertIsInstance(resource, Resource)
Expand Down Expand Up @@ -110,7 +148,7 @@ def test_create(self):
resource = Resource.create(None)
self.assertEqual(
resource,
_DEFAULT_RESOURCE.merge(
self.default_resource.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
),
)
Expand All @@ -119,7 +157,7 @@ def test_create(self):
resource = Resource.create(None, None)
self.assertEqual(
resource,
_DEFAULT_RESOURCE.merge(
self.default_resource.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
),
)
Expand All @@ -128,7 +166,7 @@ def test_create(self):
resource = Resource.create({})
self.assertEqual(
resource,
_DEFAULT_RESOURCE.merge(
self.default_resource.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
),
)
Expand All @@ -137,7 +175,7 @@ def test_create(self):
resource = Resource.create({}, None)
self.assertEqual(
resource,
_DEFAULT_RESOURCE.merge(
self.default_resource.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
),
)
Expand Down Expand Up @@ -206,6 +244,7 @@ def test_immutability(self):
TELEMETRY_SDK_VERSION: _OPENTELEMETRY_SDK_VERSION,
SERVICE_NAME: "unknown_service",
}
default_attributes.update(self.mock_platform)

attributes_copy = attributes.copy()
attributes_copy.update(default_attributes)
Expand Down Expand Up @@ -258,7 +297,7 @@ def test_aggregated_resources_no_detectors(self):
aggregated_resources = get_aggregated_resources([])
self.assertEqual(
aggregated_resources,
_DEFAULT_RESOURCE.merge(
self.default_resource.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
),
)
Expand Down Expand Up @@ -309,7 +348,7 @@ def test_aggregated_resources_multiple_detectors(self):
get_aggregated_resources(
[resource_detector1, resource_detector2, resource_detector3]
),
_DEFAULT_RESOURCE.merge(
self.default_resource.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
).merge(
Resource(
Expand Down Expand Up @@ -352,7 +391,7 @@ def test_aggregated_resources_different_schema_urls(self):
)
self.assertEqual(
get_aggregated_resources([resource_detector1, resource_detector2]),
_DEFAULT_RESOURCE.merge(
self.default_resource.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
).merge(
Resource(
Expand All @@ -366,7 +405,7 @@ def test_aggregated_resources_different_schema_urls(self):
get_aggregated_resources(
[resource_detector2, resource_detector3]
),
_DEFAULT_RESOURCE.merge(
self.default_resource.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
).merge(
Resource({"key2": "value2", "key3": "value3"}, "url1")
Expand All @@ -384,7 +423,7 @@ def test_aggregated_resources_different_schema_urls(self):
resource_detector1,
]
),
_DEFAULT_RESOURCE.merge(
self.default_resource.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
).merge(
Resource(
Expand All @@ -408,7 +447,7 @@ def test_resource_detector_ignore_error(self):
with self.assertLogs(level=WARNING):
self.assertEqual(
get_aggregated_resources([resource_detector]),
_DEFAULT_RESOURCE.merge(
self.default_resource.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
),
)
Expand All @@ -428,7 +467,7 @@ def test_resource_detector_timeout(self, mock_logger):
resource_detector.raise_on_error = False
self.assertEqual(
get_aggregated_resources([resource_detector]),
_DEFAULT_RESOURCE.merge(
self.default_resource.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
),
)
Expand Down Expand Up @@ -673,6 +712,15 @@ def test_resource_detector_entry_points_non_default(self):
self.assertEqual(resource.attributes["a"], "b")
self.assertEqual(resource.schema_url, "")

@patch.dict(
environ, {OTEL_EXPERIMENTAL_RESOURCE_DETECTORS: ""}, clear=True
xrmx marked this conversation as resolved.
Show resolved Hide resolved
)
def test_resource_detector_entry_points_empty(self):
resource = Resource({}).create()
self.assertEqual(
resource.attributes["telemetry.sdk.language"], "python"
)

def test_resource_detector_entry_points_otel(self):
"""
Test that OTELResourceDetector-resource-generated attributes are
Expand Down Expand Up @@ -723,3 +771,36 @@ def test_resource_detector_entry_points_otel(self):
)
self.assertIn(PROCESS_RUNTIME_VERSION, resource.attributes.keys())
self.assertEqual(resource.schema_url, "")

@patch("platform.system", lambda: "Linux")
@patch("platform.release", lambda: "666.5.0-35-generic")
def test_os_detector_linux(self):
resource = get_aggregated_resources(
[OsResourceDetector()],
Resource({}),
)

self.assertEqual(resource.attributes[OS_TYPE], "linux")
self.assertEqual(resource.attributes[OS_VERSION], "666.5.0-35-generic")

@patch("platform.system", lambda: "Windows")
@patch("platform.version", lambda: "10.0.666")
def test_os_detector_windows(self):
resource = get_aggregated_resources(
[OsResourceDetector()],
Resource({}),
)

self.assertEqual(resource.attributes[OS_TYPE], "windows")
self.assertEqual(resource.attributes[OS_VERSION], "10.0.666")

@patch("platform.system", lambda: "SunOS")
@patch("platform.version", lambda: "666.4.0.15.0")
def test_os_detector_solaris(self):
resource = get_aggregated_resources(
[OsResourceDetector()],
Resource({}),
)

self.assertEqual(resource.attributes[OS_TYPE], "solaris")
self.assertEqual(resource.attributes[OS_VERSION], "666.4.0.15.0")
Loading