|
17 | 17 | from urllib.error import URLError |
18 | 18 | from urllib.request import Request, urlopen |
19 | 19 |
|
| 20 | +from opentelemetry.context import ( |
| 21 | + _SUPPRESS_INSTRUMENTATION_KEY, |
| 22 | + attach, |
| 23 | + detach, |
| 24 | + set_value, |
| 25 | +) |
20 | 26 | from opentelemetry.sdk.resources import Resource, ResourceDetector |
21 | 27 | from opentelemetry.semconv.resource import ( |
22 | 28 | CloudPlatformValues, |
@@ -49,64 +55,60 @@ class AzureVMResourceDetector(ResourceDetector): |
49 | 55 | # pylint: disable=no-self-use |
50 | 56 | def detect(self) -> "Resource": |
51 | 57 | attributes = {} |
52 | | - metadata_json = ( |
53 | | - _AzureVMMetadataServiceRequestor().get_azure_vm_metadata() |
54 | | - ) |
| 58 | + token = attach(set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)) |
| 59 | + metadata_json = _get_azure_vm_metadata() |
55 | 60 | if not metadata_json: |
56 | 61 | return Resource(attributes) |
57 | 62 | for attribute_key in EXPECTED_AZURE_AMS_ATTRIBUTES: |
58 | | - attributes[ |
59 | | - attribute_key |
60 | | - ] = _AzureVMMetadataServiceRequestor().get_attribute_from_metadata( |
| 63 | + attributes[attribute_key] = _get_attribute_from_metadata( |
61 | 64 | metadata_json, attribute_key |
62 | 65 | ) |
| 66 | + detach(token) |
63 | 67 | return Resource(attributes) |
64 | 68 |
|
65 | 69 |
|
66 | | -class _AzureVMMetadataServiceRequestor: |
67 | | - def get_azure_vm_metadata(self): # pylint: disable=no-self-use |
68 | | - request = Request(_AZURE_VM_METADATA_ENDPOINT) |
69 | | - request.add_header("Metadata", "True") |
70 | | - try: |
71 | | - # TODO: Changed to 4s to fit into OTel SDK's 5 second timeout. |
72 | | - # Lengthen or allow user input if issue is resolved. |
73 | | - # See https://github.com/open-telemetry/opentelemetry-python/issues/3644 |
74 | | - with urlopen(request, timeout=4) as response: |
75 | | - return loads(response.read()) |
76 | | - except URLError: |
77 | | - # Not on Azure VM |
78 | | - return None |
79 | | - except Exception as e: # pylint: disable=broad-except,invalid-name |
80 | | - _logger.exception("Failed to receive Azure VM metadata: %s", e) |
81 | | - return None |
| 70 | +def _get_azure_vm_metadata(): |
| 71 | + request = Request(_AZURE_VM_METADATA_ENDPOINT) |
| 72 | + request.add_header("Metadata", "True") |
| 73 | + try: |
| 74 | + # TODO: Changed to 4s to fit into OTel SDK's 5 second timeout. |
| 75 | + # Lengthen or allow user input if issue is resolved. |
| 76 | + # See https://github.com/open-telemetry/opentelemetry-python/issues/3644 |
| 77 | + with urlopen(request, timeout=4) as response: |
| 78 | + return loads(response.read()) |
| 79 | + except URLError: |
| 80 | + # Not on Azure VM |
| 81 | + return None |
| 82 | + except Exception as e: # pylint: disable=broad-except,invalid-name |
| 83 | + _logger.exception("Failed to receive Azure VM metadata: %s", e) |
| 84 | + return None |
| 85 | + |
82 | 86 |
|
83 | | - def get_attribute_from_metadata( |
84 | | - self, metadata_json, attribute_key |
85 | | - ): # pylint: disable=no-self-use |
86 | | - ams_value = "" |
87 | | - if attribute_key == _AZURE_VM_SCALE_SET_NAME_ATTRIBUTE: |
88 | | - ams_value = metadata_json["vmScaleSetName"] |
89 | | - elif attribute_key == _AZURE_VM_SKU_ATTRIBUTE: |
90 | | - ams_value = metadata_json["sku"] |
91 | | - elif attribute_key == ResourceAttributes.CLOUD_PLATFORM: |
92 | | - ams_value = CloudPlatformValues.AZURE_VM.value |
93 | | - elif attribute_key == ResourceAttributes.CLOUD_PROVIDER: |
94 | | - ams_value = CloudProviderValues.AZURE.value |
95 | | - elif attribute_key == ResourceAttributes.CLOUD_REGION: |
96 | | - ams_value = metadata_json["location"] |
97 | | - elif attribute_key == ResourceAttributes.CLOUD_RESOURCE_ID: |
98 | | - ams_value = metadata_json["resourceId"] |
99 | | - elif attribute_key in ( |
100 | | - ResourceAttributes.HOST_ID, |
101 | | - ResourceAttributes.SERVICE_INSTANCE_ID, |
102 | | - ): |
103 | | - ams_value = metadata_json["vmId"] |
104 | | - elif attribute_key == ResourceAttributes.HOST_NAME: |
105 | | - ams_value = metadata_json["name"] |
106 | | - elif attribute_key == ResourceAttributes.HOST_TYPE: |
107 | | - ams_value = metadata_json["vmSize"] |
108 | | - elif attribute_key == ResourceAttributes.OS_TYPE: |
109 | | - ams_value = metadata_json["osType"] |
110 | | - elif attribute_key == ResourceAttributes.OS_VERSION: |
111 | | - ams_value = metadata_json["version"] |
112 | | - return ams_value |
| 87 | +def _get_attribute_from_metadata(metadata_json, attribute_key): |
| 88 | + ams_value = "" |
| 89 | + if attribute_key == _AZURE_VM_SCALE_SET_NAME_ATTRIBUTE: |
| 90 | + ams_value = metadata_json["vmScaleSetName"] |
| 91 | + elif attribute_key == _AZURE_VM_SKU_ATTRIBUTE: |
| 92 | + ams_value = metadata_json["sku"] |
| 93 | + elif attribute_key == ResourceAttributes.CLOUD_PLATFORM: |
| 94 | + ams_value = CloudPlatformValues.AZURE_VM.value |
| 95 | + elif attribute_key == ResourceAttributes.CLOUD_PROVIDER: |
| 96 | + ams_value = CloudProviderValues.AZURE.value |
| 97 | + elif attribute_key == ResourceAttributes.CLOUD_REGION: |
| 98 | + ams_value = metadata_json["location"] |
| 99 | + elif attribute_key == ResourceAttributes.CLOUD_RESOURCE_ID: |
| 100 | + ams_value = metadata_json["resourceId"] |
| 101 | + elif attribute_key in ( |
| 102 | + ResourceAttributes.HOST_ID, |
| 103 | + ResourceAttributes.SERVICE_INSTANCE_ID, |
| 104 | + ): |
| 105 | + ams_value = metadata_json["vmId"] |
| 106 | + elif attribute_key == ResourceAttributes.HOST_NAME: |
| 107 | + ams_value = metadata_json["name"] |
| 108 | + elif attribute_key == ResourceAttributes.HOST_TYPE: |
| 109 | + ams_value = metadata_json["vmSize"] |
| 110 | + elif attribute_key == ResourceAttributes.OS_TYPE: |
| 111 | + ams_value = metadata_json["osType"] |
| 112 | + elif attribute_key == ResourceAttributes.OS_VERSION: |
| 113 | + ams_value = metadata_json["version"] |
| 114 | + return ams_value |
0 commit comments