Skip to content

Commit 770003d

Browse files
authored
opentelemetry-sdk-extension-aws: make ecs and beanstalk resource detector silent when loaded outside AWS (#3076)
* opentelemetry-sdk-extension-aws: make ecs detector less chatty Don't print warnings if we are not running inside an ecs instance so we can load the resource detector more generally and avoid warnings in stderr. * opentelemetry-sdk-extension-aws: make beanstalk detector less chatty Don't print warnings if we are not running inside beanstalk so we can load the resource detector more generally and avoid warnings in stderr. * Add changelog
1 parent 38f0e33 commit 770003d

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

sdk-extension/opentelemetry-sdk-extension-aws/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Make ecs and beanstalk resource detector silent when loaded outside AWS
11+
([#3076](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3076))
1012
- Make EKS resource detector don't warn when not running in EKS
1113
([#3074](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3074))
1214

sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/resource/beanstalk.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def detect(self) -> "Resource":
4141
else:
4242
conf_file_path = "/var/elasticbeanstalk/xray/environment.conf"
4343

44+
if not os.path.exists(conf_file_path):
45+
return Resource.get_empty()
46+
4447
try:
4548
with open(conf_file_path, encoding="utf-8") as conf_file:
4649
parsed_data = json.load(conf_file)

sdk-extension/opentelemetry-sdk-extension-aws/src/opentelemetry/sdk/extension/aws/resource/ecs.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ def detect(self) -> "Resource":
4141
if not os.environ.get(
4242
"ECS_CONTAINER_METADATA_URI"
4343
) and not os.environ.get("ECS_CONTAINER_METADATA_URI_V4"):
44-
raise RuntimeError(
45-
"Missing ECS_CONTAINER_METADATA_URI therefore process is not on ECS."
46-
)
44+
return Resource.get_empty()
4745

4846
container_id = ""
4947
try:

sdk-extension/opentelemetry-sdk-extension-aws/tests/resource/test_beanstalk.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,19 @@ class AwsBeanstalkResourceDetectorTest(unittest.TestCase):
4141
new_callable=mock_open,
4242
read_data=f'{{"deployment_id":"{MockBeanstalkResourceAttributes[ResourceAttributes.SERVICE_INSTANCE_ID]}","environment_name":"{MockBeanstalkResourceAttributes[ResourceAttributes.SERVICE_NAMESPACE]}","version_label":"{MockBeanstalkResourceAttributes[ResourceAttributes.SERVICE_VERSION]}"}}',
4343
)
44-
def test_simple_create(self, mock_open_function):
44+
@patch("os.path.exists", return_value=True)
45+
def test_simple_create(self, mock_path_exists, mock_open_function):
4546
actual = AwsBeanstalkResourceDetector().detect()
4647
self.assertDictEqual(
4748
actual.attributes.copy(),
4849
OrderedDict(MockBeanstalkResourceAttributes),
4950
)
51+
52+
@patch("os.name", "posix")
53+
@patch("os.path.exists", return_value=False)
54+
def test_not_on_beanstalk(self, mock_path_exists):
55+
actual = AwsBeanstalkResourceDetector().detect()
56+
self.assertDictEqual(actual.attributes.copy(), {})
57+
mock_path_exists.assert_called_once_with(
58+
"/var/elasticbeanstalk/xray/environment.conf"
59+
)

sdk-extension/opentelemetry-sdk-extension-aws/tests/resource/test_ecs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ def _http_get_function_fargate(url: str, *args, **kwargs) -> str:
7979

8080

8181
class AwsEcsResourceDetectorTest(unittest.TestCase):
82+
@patch.dict("os.environ", {}, clear=True)
83+
def test_not_on_ecs(self):
84+
actual = AwsEcsResourceDetector().detect()
85+
self.assertDictEqual(actual.attributes.copy(), {})
86+
8287
@patch.dict(
8388
"os.environ",
8489
{"ECS_CONTAINER_METADATA_URI": "mock-uri"},

0 commit comments

Comments
 (0)