Skip to content

Commit

Permalink
Merge branch 'main' into type-hints-to-redis
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex authored Dec 13, 2024
2 parents 5275716 + 38f0e33 commit 97db5cb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
3 changes: 3 additions & 0 deletions sdk-extension/opentelemetry-sdk-extension-aws/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
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 97db5cb

Please sign in to comment.