open-telemetry / opentelemetry-python

OpenTelemetry Python API and SDK
https://opentelemetry.io
Apache License 2.0
1.66k stars 568 forks source link

sdk: Implement basic os resource detector #3992

Open Zirak opened 6 days ago

Zirak commented 6 days ago

Description

Implement basic os resource detector.

Based on OS resource semantics: https://opentelemetry.io/docs/specs/semconv/resource/os/

Currently implements os.type and os.version, attempting to be in line with what's reported by other runtimes (like java and node).

I have not yet tested on some more exotic OSs such as hp-ux, aix, or z/os.

Type of change

Please delete options that are not relevant.

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

Does This PR Require a Contrib Repo Change?

Answer the following question based on these examples of changes that would require a Contrib Repo Change:

Checklist:

I'm unsure what the practice to do the two items above (changelog & documentation) would actually require. It seems like opening a PR is a prerequisite to generating a changelog. I haven't seen any special documentation around resource detectors. Is my understanding correct?

Zirak commented 4 days ago

Great points, thank you! I've completely missed the entrypoint.

Regarding making it a default, I've done the following simple diff:

@@ -180,11 +180,13 @@ class Resource:
         resource = _DEFAULT_RESOURCE

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

         if "otel" not in otel_experimental_resource_detectors:
             otel_experimental_resource_detectors.append("otel")
+        if "os" not in otel_experimental_resource_detectors:
+            otel_experimental_resource_detectors.append("os")

         for resource_detector in otel_experimental_resource_detectors:
             resource_detectors.append(

Running locally it looks good (yay!), but I'm having trouble with testing. A lot of things expect _DEFAULT_RESOURCE to be the baseline for all future resources. I'm currently tinkering with decorating the entire TestResources with a platform.uname patch, alongside with extending _DEFAULT_RESOURCE as part of __init__ and rewriting existing test cases to use it (Edit: This has since been pushed), e.g.

@@ -61,12 +62,26 @@ except ImportError:
     psutil = None

+@patch("platform.uname", lambda: platform.uname_result(
+            system="Linux",
+            node="node",
+            release="1.2.3",
+            version="4.5.6",
+            machine="x86_64",
+            processor="x86_64"
+        ))
 class TestResources(unittest.TestCase):
     def setUp(self) -> None:
         environ[OTEL_RESOURCE_ATTRIBUTES] = ""
+        self.mock_platform = {
+            OS_TYPE: "linux",
+            OS_VERSION: "1.2.3",
+        }
+        self.default_resource = _DEFAULT_RESOURCE.merge(Resource(self.mock_platform))
@@ -86,6 +101,7 @@ class TestResources(unittest.TestCase):
             TELEMETRY_SDK_VERSION: _OPENTELEMETRY_SDK_VERSION,
             SERVICE_NAME: "unknown_service",
         }
+        expected_attributes.update(self.mock_platform)
@@ -431,7 +447,7 @@ class TestResources(unittest.TestCase):
         resource_detector.raise_on_error = False
         self.assertEqual(
             get_aggregated_resources([resource_detector]),
-            _DEFAULT_RESOURCE.merge(
+            self.default_resource.merge(

It feels a bit icky. Am I missing a better, simpler way?