From ecbd817f834e45f7338313cfc27ab0d15ca54a24 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Fri, 6 Dec 2024 14:54:16 +0100 Subject: [PATCH 1/2] opentelemetry-sdk-extension-aws: make eks detector less chatty Don't print warnings if we are not running inside an eks instance so we can load the resource detector more generally and avoid warnings in stderr. --- .../sdk/extension/aws/resource/eks.py | 19 +++++++++--- .../tests/resource/test_eks.py | 31 ++++++++++++++++++- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/resource/eks.py b/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/resource/eks.py index c712e9875f..07f78b2d29 100644 --- a/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/resource/eks.py +++ b/sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/resource/eks.py @@ -14,6 +14,7 @@ import json import logging +import os import ssl from urllib.request import Request, urlopen @@ -29,6 +30,9 @@ _CONTAINER_ID_LENGTH = 64 _GET_METHOD = "GET" +_TOKEN_PATH = "/var/run/secrets/kubernetes.io/serviceaccount/token" +_CERT_PATH = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" + def _aws_http_request(method, path, cred_value): with urlopen( @@ -39,7 +43,7 @@ def _aws_http_request(method, path, cred_value): ), timeout=5, context=ssl.create_default_context( - cafile="/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" + cafile=_CERT_PATH, ), ) as response: return response.read().decode("utf-8") @@ -47,10 +51,7 @@ def _aws_http_request(method, path, cred_value): def _get_k8s_cred_value(): try: - with open( - "/var/run/secrets/kubernetes.io/serviceaccount/token", - encoding="utf8", - ) as token_file: + with open(_TOKEN_PATH, encoding="utf8") as token_file: return "Bearer " + token_file.read() # pylint: disable=broad-except except Exception as exception: @@ -97,6 +98,10 @@ def _get_container_id(): return container_id +def _is_k8s() -> bool: + return os.path.exists(_TOKEN_PATH) and os.path.exists(_CERT_PATH) + + class AwsEksResourceDetector(ResourceDetector): """Detects attribute values only available when the app is running on AWS Elastic Kubernetes Service (EKS) and returns them in a Resource. @@ -106,6 +111,10 @@ class AwsEksResourceDetector(ResourceDetector): def detect(self) -> "Resource": try: + # if we are not running on eks exit early without warnings + if not _is_k8s(): + return Resource.get_empty() + cred_value = _get_k8s_cred_value() if not _is_eks(cred_value): diff --git a/sdk-extension/opentelemetry-sdk-extension-aws/tests/resource/test_eks.py b/sdk-extension/opentelemetry-sdk-extension-aws/tests/resource/test_eks.py index a3819e6383..a2adcb3969 100644 --- a/sdk-extension/opentelemetry-sdk-extension-aws/tests/resource/test_eks.py +++ b/sdk-extension/opentelemetry-sdk-extension-aws/tests/resource/test_eks.py @@ -42,6 +42,10 @@ class AwsEksResourceDetectorTest(unittest.TestCase): "opentelemetry.sdk.extension.aws.resource.eks._is_eks", return_value=True, ) + @patch( + "opentelemetry.sdk.extension.aws.resource.eks._is_k8s", + return_value=True, + ) @patch( "opentelemetry.sdk.extension.aws.resource.eks._get_cluster_info", return_value=f"""{{ @@ -88,6 +92,7 @@ def test_simple_create( self, mock_open_function, mock_get_cluster_info, + mock_is_k8s, mock_is_eks, mock_get_k8_cred_value, ): @@ -104,8 +109,32 @@ def test_simple_create( "opentelemetry.sdk.extension.aws.resource.eks._is_eks", return_value=False, ) + @patch( + "opentelemetry.sdk.extension.aws.resource.eks._is_k8s", + return_value=True, + ) def test_if_no_eks_env_var_and_should_raise( - self, mock_is_eks, mock_get_k8_cred_value + self, mock_is_k8s, mock_is_eks, mock_get_k8_cred_value ): with self.assertRaises(RuntimeError): AwsEksResourceDetector(raise_on_error=True).detect() + + @patch( + "opentelemetry.sdk.extension.aws.resource.eks._get_k8s_cred_value", + return_value="MOCK_TOKEN", + ) + @patch( + "opentelemetry.sdk.extension.aws.resource.eks._is_eks", + return_value=False, + ) + @patch( + "opentelemetry.sdk.extension.aws.resource.eks._is_k8s", + return_value=False, + ) + def test_if_no_eks_paths_should_not_raise( + self, mock_is_k8s, mock_is_eks, mock_get_k8_cred_value + ): + try: + AwsEksResourceDetector(raise_on_error=True).detect() + except RuntimeError: + self.fail("Should not raise") From 4b20aeb2915a8e32430a95de849965479e6a496b Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Fri, 6 Dec 2024 15:03:34 +0100 Subject: [PATCH 2/2] Update CHANGELOG --- sdk-extension/opentelemetry-sdk-extension-aws/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sdk-extension/opentelemetry-sdk-extension-aws/CHANGELOG.md b/sdk-extension/opentelemetry-sdk-extension-aws/CHANGELOG.md index 2d01260554..a85ad43256 100644 --- a/sdk-extension/opentelemetry-sdk-extension-aws/CHANGELOG.md +++ b/sdk-extension/opentelemetry-sdk-extension-aws/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +- Make EKS resource detector don't warn when not running in EKS + ([#3074](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3074)) + ## Version 2.0.2 (2024-08-05) See [common CHANGELOG](../../CHANGELOG.md) for the changes in this and prior versions.