Skip to content

Commit

Permalink
opentelemetry-sdk-extension-aws: make eks detector less chatty
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
xrmx committed Dec 9, 2024
1 parent 6134d5a commit ecbd817
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import json
import logging
import os
import ssl
from urllib.request import Request, urlopen

Expand All @@ -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(
Expand All @@ -39,18 +43,15 @@ 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")


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:
Expand Down Expand Up @@ -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.
Expand All @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"""{{
Expand Down Expand Up @@ -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,
):
Expand All @@ -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")

0 comments on commit ecbd817

Please sign in to comment.