Skip to content

Commit

Permalink
sdk: Take OS out of the default resource detectors list
Browse files Browse the repository at this point in the history
  • Loading branch information
Zirak committed Jul 21, 2024
1 parent 500b11d commit 1f3becf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def create(
if not attributes:
attributes = {}

otel_experimental_resource_detectors = {"otel", "os"}.union(
otel_experimental_resource_detectors = {"otel"}.union(
{
otel_experimental_resource_detector.strip()
for otel_experimental_resource_detector in environ.get(
Expand Down
58 changes: 11 additions & 47 deletions opentelemetry-sdk/tests/resources/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# 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 Down Expand Up @@ -62,43 +61,10 @@
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 @@ -120,7 +86,6 @@ 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 @@ -148,7 +113,7 @@ def test_create(self):
resource = Resource.create(None)
self.assertEqual(
resource,
self.default_resource.merge(
_DEFAULT_RESOURCE.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
),
)
Expand All @@ -157,7 +122,7 @@ def test_create(self):
resource = Resource.create(None, None)
self.assertEqual(
resource,
self.default_resource.merge(
_DEFAULT_RESOURCE.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
),
)
Expand All @@ -166,7 +131,7 @@ def test_create(self):
resource = Resource.create({})
self.assertEqual(
resource,
self.default_resource.merge(
_DEFAULT_RESOURCE.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
),
)
Expand All @@ -175,7 +140,7 @@ def test_create(self):
resource = Resource.create({}, None)
self.assertEqual(
resource,
self.default_resource.merge(
_DEFAULT_RESOURCE.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
),
)
Expand Down Expand Up @@ -244,7 +209,6 @@ 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 @@ -297,7 +261,7 @@ def test_aggregated_resources_no_detectors(self):
aggregated_resources = get_aggregated_resources([])
self.assertEqual(
aggregated_resources,
self.default_resource.merge(
_DEFAULT_RESOURCE.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
),
)
Expand Down Expand Up @@ -348,7 +312,7 @@ def test_aggregated_resources_multiple_detectors(self):
get_aggregated_resources(
[resource_detector1, resource_detector2, resource_detector3]
),
self.default_resource.merge(
_DEFAULT_RESOURCE.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
).merge(
Resource(
Expand Down Expand Up @@ -391,7 +355,7 @@ def test_aggregated_resources_different_schema_urls(self):
)
self.assertEqual(
get_aggregated_resources([resource_detector1, resource_detector2]),
self.default_resource.merge(
_DEFAULT_RESOURCE.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
).merge(
Resource(
Expand All @@ -405,7 +369,7 @@ def test_aggregated_resources_different_schema_urls(self):
get_aggregated_resources(
[resource_detector2, resource_detector3]
),
self.default_resource.merge(
_DEFAULT_RESOURCE.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
).merge(
Resource({"key2": "value2", "key3": "value3"}, "url1")
Expand All @@ -423,7 +387,7 @@ def test_aggregated_resources_different_schema_urls(self):
resource_detector1,
]
),
self.default_resource.merge(
_DEFAULT_RESOURCE.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
).merge(
Resource(
Expand All @@ -447,7 +411,7 @@ def test_resource_detector_ignore_error(self):
with self.assertLogs(level=WARNING):
self.assertEqual(
get_aggregated_resources([resource_detector]),
self.default_resource.merge(
_DEFAULT_RESOURCE.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
),
)
Expand All @@ -467,7 +431,7 @@ def test_resource_detector_timeout(self, mock_logger):
resource_detector.raise_on_error = False
self.assertEqual(
get_aggregated_resources([resource_detector]),
self.default_resource.merge(
_DEFAULT_RESOURCE.merge(
Resource({SERVICE_NAME: "unknown_service"}, "")
),
)
Expand Down

0 comments on commit 1f3becf

Please sign in to comment.