Skip to content

Commit ed40983

Browse files
committed
Added try catch, logging, and dependency for azure resource detector
1 parent 9afaf26 commit ed40983

File tree

6 files changed

+56
-25
lines changed

6 files changed

+56
-25
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616

1717
- `opentelemetry-instrumentation-urllib`/`opentelemetry-instrumentation-urllib3` Fix metric descriptions to match semantic conventions
1818
([#1959]((https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1959))
19+
- `opentelemetry-resource-detector-azure` Added dependency for Cloud Resource ID attribute and clearer logging for Azure Resource Detectors
20+
([#XXX](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/XXXX))
1921

2022
## Version 1.21.0/0.42b0 (2023-11-01)
2123

resource/opentelemetry-resource-detector-azure/pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ classifiers = [
2626
]
2727
dependencies = [
2828
"opentelemetry-sdk ~= 1.19",
29+
"opentelemetry-semantic-conventions ~= 0.42b0",
2930
]
3031

3132
[project.optional-dependencies]

resource/opentelemetry-resource-detector-azure/src/opentelemetry/resource/detector/azure/app_service.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from logging import getLogger
1516
from os import environ
1617

1718
from opentelemetry.sdk.resources import ResourceDetector, Resource
@@ -26,6 +27,7 @@
2627
_WEBSITE_RESOURCE_GROUP = "WEBSITE_RESOURCE_GROUP"
2728
_WEBSITE_SITE_NAME = "WEBSITE_SITE_NAME"
2829
_WEBSITE_SLOT_NAME = "WEBSITE_SLOT_NAME"
30+
_logger = getLogger(__name__)
2931

3032

3133
_APP_SERVICE_ATTRIBUTE_ENV_VARS = {
@@ -39,20 +41,22 @@
3941
class AzureAppServiceResourceDetector(ResourceDetector):
4042
def detect(self) -> Resource:
4143
attributes = {}
42-
website_site_name = environ.get(_WEBSITE_SITE_NAME)
43-
if website_site_name:
44-
attributes[ResourceAttributes.SERVICE_NAME] = website_site_name
45-
attributes[ResourceAttributes.CLOUD_PROVIDER] = CloudProviderValues.AZURE.value
46-
attributes[ResourceAttributes.CLOUD_PLATFORM] = CloudPlatformValues.AZURE_APP_SERVICE.value
47-
48-
azure_resource_uri = _get_azure_resource_uri(website_site_name)
49-
if azure_resource_uri:
50-
attributes[ResourceAttributes.CLOUD_RESOURCE_ID] = azure_resource_uri
51-
for (key, env_var) in _APP_SERVICE_ATTRIBUTE_ENV_VARS.items():
52-
value = environ.get(env_var)
53-
if value:
54-
attributes[key] = value
44+
try:
45+
website_site_name = environ.get(_WEBSITE_SITE_NAME)
46+
if website_site_name:
47+
attributes[ResourceAttributes.SERVICE_NAME] = website_site_name
48+
attributes[ResourceAttributes.CLOUD_PROVIDER] = CloudProviderValues.AZURE.value
49+
attributes[ResourceAttributes.CLOUD_PLATFORM] = CloudPlatformValues.AZURE_APP_SERVICE.value
5550

51+
azure_resource_uri = _get_azure_resource_uri(website_site_name)
52+
if azure_resource_uri:
53+
attributes[ResourceAttributes.CLOUD_RESOURCE_ID] = azure_resource_uri
54+
for (key, env_var) in _APP_SERVICE_ATTRIBUTE_ENV_VARS.items():
55+
value = environ.get(env_var)
56+
if value:
57+
attributes[key] = value
58+
except Exception as e:
59+
_logger.info("Could not detect Azure App Service metadata: %s", e)
5660
return Resource(attributes)
5761

5862
def _get_azure_resource_uri(website_site_name):

resource/opentelemetry-resource-detector-azure/src/opentelemetry/resource/detector/azure/vm.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from json import loads
1616
from logging import getLogger
17-
from os import environ
1817
from urllib.request import Request, urlopen
1918
from urllib.error import URLError
2019

@@ -26,7 +25,6 @@
2625
)
2726

2827

29-
# TODO: Remove when cloud resource id is no longer missing in Resource Attributes
3028
_AZURE_VM_METADATA_ENDPOINT = "http://169.254.169.254/metadata/instance/compute?api-version=2021-12-13&format=json"
3129
_AZURE_VM_SCALE_SET_NAME_ATTRIBUTE = "azure.vm.scaleset.name"
3230
_AZURE_VM_SKU_ATTRIBUTE = "azure.vm.sku"
@@ -52,17 +50,20 @@ class AzureVMResourceDetector(ResourceDetector):
5250
# pylint: disable=no-self-use
5351
def detect(self) -> "Resource":
5452
attributes = {}
55-
metadata_json = (
56-
_AzureVMMetadataServiceRequestor().get_azure_vm_metadata()
57-
)
58-
if not metadata_json:
59-
return Resource(attributes)
60-
for attribute_key in EXPECTED_AZURE_AMS_ATTRIBUTES:
61-
attributes[
62-
attribute_key
63-
] = _AzureVMMetadataServiceRequestor().get_attribute_from_metadata(
64-
metadata_json, attribute_key
53+
try:
54+
metadata_json = (
55+
_AzureVMMetadataServiceRequestor().get_azure_vm_metadata()
6556
)
57+
if not metadata_json:
58+
return Resource(attributes)
59+
for attribute_key in EXPECTED_AZURE_AMS_ATTRIBUTES:
60+
attributes[
61+
attribute_key
62+
] = _AzureVMMetadataServiceRequestor().get_attribute_from_metadata(
63+
metadata_json, attribute_key
64+
)
65+
except Exception as e:
66+
_logger.info("Could not detect Azure VM metadata: %s", e)
6667
return Resource(attributes)
6768

6869

resource/opentelemetry-resource-detector-azure/tests/test_app_service.py

+8
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,11 @@ def test_on_app_service_no_owner(self):
151151
def test_off_app_service(self):
152152
resource = AzureAppServiceResourceDetector().detect()
153153
self.assertEqual(resource.attributes, {})
154+
155+
@patch("opentelemetry.resource.detector.azure.app_service._logger")
156+
@patch("opentelemetry.resource.detector.azure.app_service.environ")
157+
def test_off_app_service(self, mock_environ, mock_logger):
158+
mock_environ.get.side_effect = Exception("Test Exception")
159+
resource = AzureAppServiceResourceDetector().detect()
160+
mock_logger.info.assert_called_once()
161+
self.assertEqual(resource.attributes, {})

resource/opentelemetry-resource-detector-azure/tests/test_vm.py

+15
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@
332332
"zone": "1"
333333
}
334334
"""
335+
INCOMPLETE_JSON = """
336+
{
337+
"vmId": "02aab8a4-74ef-476e-8182-f6d2ba4166a6"
338+
}
339+
"""
335340
LINUX_ATTRIBUTES = {
336341
"azure.vm.scaleset.name": "crpteste9vflji9",
337342
"azure.vm.sku": "20_04-lts-gen2",
@@ -384,3 +389,13 @@ def test_windows(self, mock_urlopen):
384389
self.assertEqual(
385390
attributes[attribute_key], WINDOWS_ATTRIBUTES[attribute_key]
386391
)
392+
393+
@patch("opentelemetry.resource.detector.azure.vm._logger")
394+
@patch("opentelemetry.resource.detector.azure.vm.urlopen")
395+
def test_incomplete_exception(self, mock_urlopen, mock_logger):
396+
mock_open = Mock()
397+
mock_urlopen.return_value = mock_open
398+
mock_open.read.return_value = INCOMPLETE_JSON
399+
attributes = AzureVMResourceDetector().detect().attributes
400+
mock_logger.info.assert_called_once()
401+
self.assertEqual(attributes, {})

0 commit comments

Comments
 (0)