Skip to content

Commit

Permalink
Feedback: remove try catch and only fix dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydvoss committed Nov 27, 2023
1 parent 835ce52 commit d79d844
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, {})
10 changes: 0 additions & 10 deletions resource/opentelemetry-resource-detector-azure/tests/test_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, {})

0 comments on commit d79d844

Please sign in to comment.