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
This commit is contained in:
parent
38f0e33641
commit
770003dd17
|
|
@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
- Make ecs and beanstalk resource detector silent when loaded outside AWS
|
||||||
|
([#3076](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3076))
|
||||||
- Make EKS resource detector don't warn when not running in EKS
|
- Make EKS resource detector don't warn when not running in EKS
|
||||||
([#3074](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3074))
|
([#3074](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3074))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,9 @@ class AwsBeanstalkResourceDetector(ResourceDetector):
|
||||||
else:
|
else:
|
||||||
conf_file_path = "/var/elasticbeanstalk/xray/environment.conf"
|
conf_file_path = "/var/elasticbeanstalk/xray/environment.conf"
|
||||||
|
|
||||||
|
if not os.path.exists(conf_file_path):
|
||||||
|
return Resource.get_empty()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(conf_file_path, encoding="utf-8") as conf_file:
|
with open(conf_file_path, encoding="utf-8") as conf_file:
|
||||||
parsed_data = json.load(conf_file)
|
parsed_data = json.load(conf_file)
|
||||||
|
|
|
||||||
|
|
@ -41,9 +41,7 @@ class AwsEcsResourceDetector(ResourceDetector):
|
||||||
if not os.environ.get(
|
if not os.environ.get(
|
||||||
"ECS_CONTAINER_METADATA_URI"
|
"ECS_CONTAINER_METADATA_URI"
|
||||||
) and not os.environ.get("ECS_CONTAINER_METADATA_URI_V4"):
|
) and not os.environ.get("ECS_CONTAINER_METADATA_URI_V4"):
|
||||||
raise RuntimeError(
|
return Resource.get_empty()
|
||||||
"Missing ECS_CONTAINER_METADATA_URI therefore process is not on ECS."
|
|
||||||
)
|
|
||||||
|
|
||||||
container_id = ""
|
container_id = ""
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -41,9 +41,19 @@ class AwsBeanstalkResourceDetectorTest(unittest.TestCase):
|
||||||
new_callable=mock_open,
|
new_callable=mock_open,
|
||||||
read_data=f'{{"deployment_id":"{MockBeanstalkResourceAttributes[ResourceAttributes.SERVICE_INSTANCE_ID]}","environment_name":"{MockBeanstalkResourceAttributes[ResourceAttributes.SERVICE_NAMESPACE]}","version_label":"{MockBeanstalkResourceAttributes[ResourceAttributes.SERVICE_VERSION]}"}}',
|
read_data=f'{{"deployment_id":"{MockBeanstalkResourceAttributes[ResourceAttributes.SERVICE_INSTANCE_ID]}","environment_name":"{MockBeanstalkResourceAttributes[ResourceAttributes.SERVICE_NAMESPACE]}","version_label":"{MockBeanstalkResourceAttributes[ResourceAttributes.SERVICE_VERSION]}"}}',
|
||||||
)
|
)
|
||||||
def test_simple_create(self, mock_open_function):
|
@patch("os.path.exists", return_value=True)
|
||||||
|
def test_simple_create(self, mock_path_exists, mock_open_function):
|
||||||
actual = AwsBeanstalkResourceDetector().detect()
|
actual = AwsBeanstalkResourceDetector().detect()
|
||||||
self.assertDictEqual(
|
self.assertDictEqual(
|
||||||
actual.attributes.copy(),
|
actual.attributes.copy(),
|
||||||
OrderedDict(MockBeanstalkResourceAttributes),
|
OrderedDict(MockBeanstalkResourceAttributes),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@patch("os.name", "posix")
|
||||||
|
@patch("os.path.exists", return_value=False)
|
||||||
|
def test_not_on_beanstalk(self, mock_path_exists):
|
||||||
|
actual = AwsBeanstalkResourceDetector().detect()
|
||||||
|
self.assertDictEqual(actual.attributes.copy(), {})
|
||||||
|
mock_path_exists.assert_called_once_with(
|
||||||
|
"/var/elasticbeanstalk/xray/environment.conf"
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -79,6 +79,11 @@ def _http_get_function_fargate(url: str, *args, **kwargs) -> str:
|
||||||
|
|
||||||
|
|
||||||
class AwsEcsResourceDetectorTest(unittest.TestCase):
|
class AwsEcsResourceDetectorTest(unittest.TestCase):
|
||||||
|
@patch.dict("os.environ", {}, clear=True)
|
||||||
|
def test_not_on_ecs(self):
|
||||||
|
actual = AwsEcsResourceDetector().detect()
|
||||||
|
self.assertDictEqual(actual.attributes.copy(), {})
|
||||||
|
|
||||||
@patch.dict(
|
@patch.dict(
|
||||||
"os.environ",
|
"os.environ",
|
||||||
{"ECS_CONTAINER_METADATA_URI": "mock-uri"},
|
{"ECS_CONTAINER_METADATA_URI": "mock-uri"},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue