From d79d84401be6b3278f9118fc2adad0d1fbd4265e Mon Sep 17 00:00:00 2001 From: jeremydvoss Date: Mon, 27 Nov 2023 14:36:38 -0800 Subject: [PATCH] Feedback: remove try catch and only fix dependencies --- .../pyproject.toml | 3 +- .../resource/detector/azure/app_service.py | 39 +++++++++---------- .../resource/detector/azure/vm.py | 25 ++++++------ .../tests/test_app_service.py | 8 ---- .../tests/test_vm.py | 10 ----- 5 files changed, 30 insertions(+), 55 deletions(-) diff --git a/resource/opentelemetry-resource-detector-azure/pyproject.toml b/resource/opentelemetry-resource-detector-azure/pyproject.toml index 2e3838c57f..403553be3d 100644 --- a/resource/opentelemetry-resource-detector-azure/pyproject.toml +++ b/resource/opentelemetry-resource-detector-azure/pyproject.toml @@ -25,8 +25,7 @@ classifiers = [ "Programming Language :: Python :: 3.11", ] dependencies = [ - "opentelemetry-sdk ~= 1.19", - "opentelemetry-semantic-conventions ~= 0.42b0", + "opentelemetry-sdk ~= 1.21", ] [project.optional-dependencies] diff --git a/resource/opentelemetry-resource-detector-azure/src/opentelemetry/resource/detector/azure/app_service.py b/resource/opentelemetry-resource-detector-azure/src/opentelemetry/resource/detector/azure/app_service.py index 527293917e..f2e7e3e585 100644 --- a/resource/opentelemetry-resource-detector-azure/src/opentelemetry/resource/detector/azure/app_service.py +++ b/resource/opentelemetry-resource-detector-azure/src/opentelemetry/resource/detector/azure/app_service.py @@ -46,28 +46,25 @@ class AzureAppServiceResourceDetector(ResourceDetector): def detect(self) -> Resource: attributes = {} - try: - website_site_name = environ.get(_WEBSITE_SITE_NAME) - if website_site_name: - attributes[ResourceAttributes.SERVICE_NAME] = website_site_name - attributes[ - ResourceAttributes.CLOUD_PROVIDER - ] = CloudProviderValues.AZURE.value - attributes[ - ResourceAttributes.CLOUD_PLATFORM - ] = CloudPlatformValues.AZURE_APP_SERVICE.value + website_site_name = environ.get(_WEBSITE_SITE_NAME) + if website_site_name: + attributes[ResourceAttributes.SERVICE_NAME] = website_site_name + attributes[ + ResourceAttributes.CLOUD_PROVIDER + ] = CloudProviderValues.AZURE.value + attributes[ + ResourceAttributes.CLOUD_PLATFORM + ] = CloudPlatformValues.AZURE_APP_SERVICE.value - azure_resource_uri = _get_azure_resource_uri(website_site_name) - if azure_resource_uri: - attributes[ - ResourceAttributes.CLOUD_RESOURCE_ID - ] = azure_resource_uri - for (key, env_var) in _APP_SERVICE_ATTRIBUTE_ENV_VARS.items(): - value = environ.get(env_var) - if value: - attributes[key] = value - except Exception as e: - _logger.info("Could not detect Azure App Service metadata: %s", e) + azure_resource_uri = _get_azure_resource_uri(website_site_name) + if azure_resource_uri: + attributes[ + ResourceAttributes.CLOUD_RESOURCE_ID + ] = azure_resource_uri + for (key, env_var) in _APP_SERVICE_ATTRIBUTE_ENV_VARS.items(): + value = environ.get(env_var) + if value: + attributes[key] = value return Resource(attributes) diff --git a/resource/opentelemetry-resource-detector-azure/src/opentelemetry/resource/detector/azure/vm.py b/resource/opentelemetry-resource-detector-azure/src/opentelemetry/resource/detector/azure/vm.py index 3aac204052..8dd741b901 100644 --- a/resource/opentelemetry-resource-detector-azure/src/opentelemetry/resource/detector/azure/vm.py +++ b/resource/opentelemetry-resource-detector-azure/src/opentelemetry/resource/detector/azure/vm.py @@ -14,8 +14,8 @@ from json import loads from logging import getLogger -from urllib.request import Request, urlopen from urllib.error import URLError +from urllib.request import Request, urlopen from opentelemetry.sdk.resources import Resource, ResourceDetector from opentelemetry.semconv.resource import ( @@ -49,20 +49,17 @@ class AzureVMResourceDetector(ResourceDetector): # pylint: disable=no-self-use def detect(self) -> "Resource": attributes = {} - try: - metadata_json = ( - _AzureVMMetadataServiceRequestor().get_azure_vm_metadata() + metadata_json = ( + _AzureVMMetadataServiceRequestor().get_azure_vm_metadata() + ) + if not metadata_json: + return Resource(attributes) + for attribute_key in EXPECTED_AZURE_AMS_ATTRIBUTES: + attributes[ + attribute_key + ] = _AzureVMMetadataServiceRequestor().get_attribute_from_metadata( + metadata_json, attribute_key ) - if not metadata_json: - return Resource(attributes) - for attribute_key in EXPECTED_AZURE_AMS_ATTRIBUTES: - attributes[ - attribute_key - ] = _AzureVMMetadataServiceRequestor().get_attribute_from_metadata( - metadata_json, attribute_key - ) - except Exception as e: - _logger.info("Could not detect Azure VM metadata: %s", e) return Resource(attributes) diff --git a/resource/opentelemetry-resource-detector-azure/tests/test_app_service.py b/resource/opentelemetry-resource-detector-azure/tests/test_app_service.py index 96d73c1cff..c5d2396dab 100644 --- a/resource/opentelemetry-resource-detector-azure/tests/test_app_service.py +++ b/resource/opentelemetry-resource-detector-azure/tests/test_app_service.py @@ -152,11 +152,3 @@ def test_on_app_service_no_owner(self): def test_off_app_service(self): resource = AzureAppServiceResourceDetector().detect() self.assertEqual(resource.attributes, {}) - - @patch("opentelemetry.resource.detector.azure.app_service._logger") - @patch("opentelemetry.resource.detector.azure.app_service.environ") - def test_off_app_service(self, mock_environ, mock_logger): - mock_environ.get.side_effect = Exception("Test Exception") - resource = AzureAppServiceResourceDetector().detect() - mock_logger.info.assert_called_once() - self.assertEqual(resource.attributes, {}) diff --git a/resource/opentelemetry-resource-detector-azure/tests/test_vm.py b/resource/opentelemetry-resource-detector-azure/tests/test_vm.py index 06c1586b83..60523b89b3 100644 --- a/resource/opentelemetry-resource-detector-azure/tests/test_vm.py +++ b/resource/opentelemetry-resource-detector-azure/tests/test_vm.py @@ -383,13 +383,3 @@ def test_windows(self, mock_urlopen): attributes = AzureVMResourceDetector().detect().attributes for attribute_key, attribute_value in LINUX_ATTRIBUTES.items(): self.assertEqual(attributes[attribute_key], attribute_value) - - @patch("opentelemetry.resource.detector.azure.vm._logger") - @patch("opentelemetry.resource.detector.azure.vm.urlopen") - def test_incomplete_exception(self, mock_urlopen, mock_logger): - mock_open = Mock() - mock_urlopen.return_value = mock_open - mock_open.read.return_value = INCOMPLETE_JSON - attributes = AzureVMResourceDetector().detect().attributes - mock_logger.info.assert_called_once() - self.assertEqual(attributes, {})