Skip to content

Commit 2493258

Browse files
authored
Ignore vm detector if already in other rps for azure resource detector (#2456)
1 parent 58f3d87 commit 2493258

File tree

7 files changed

+174
-45
lines changed

7 files changed

+174
-45
lines changed

Diff for: resource/opentelemetry-resource-detector-azure/CHANGELOG.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Ignore vm detector if already in other rps
11+
([#2456](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2456))
12+
13+
## Version 0.1.4 (2024-04-05)
14+
15+
- Fix windows tests/suppress instrumentation for urllib call
16+
([#2178](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2178))
17+
18+
## Version 0.1.3 (2024-01-25)
19+
1020
- Change meta data service timeout to 200ms
1121
([#2387](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2387))
1222

13-
## Version 0.1.2 (2024-01-25)
23+
## Version 0.1.1 (2024-01-10)
1424

1525
- Initial CHANGELOG.md entry
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# pylint: disable=import-error
16+
17+
from .app_service import AzureAppServiceResourceDetector
18+
from .version import __version__
19+
from .vm import AzureVMResourceDetector
20+
21+
__all__ = [
22+
"AzureAppServiceResourceDetector",
23+
"AzureVMResourceDetector",
24+
"__version__",
25+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from opentelemetry.semconv.resource import ResourceAttributes
16+
17+
# cSpell:disable
18+
19+
# Azure Kubernetes
20+
21+
_AKS_ARM_NAMESPACE_ID = "AKS_ARM_NAMESPACE_ID"
22+
23+
# AppService
24+
25+
_AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE = "azure.app.service.stamp"
26+
_REGION_NAME = "REGION_NAME"
27+
_WEBSITE_HOME_STAMPNAME = "WEBSITE_HOME_STAMPNAME"
28+
_WEBSITE_HOSTNAME = "WEBSITE_HOSTNAME"
29+
_WEBSITE_INSTANCE_ID = "WEBSITE_INSTANCE_ID"
30+
_WEBSITE_OWNER_NAME = "WEBSITE_OWNER_NAME"
31+
_WEBSITE_RESOURCE_GROUP = "WEBSITE_RESOURCE_GROUP"
32+
_WEBSITE_SITE_NAME = "WEBSITE_SITE_NAME"
33+
_WEBSITE_SLOT_NAME = "WEBSITE_SLOT_NAME"
34+
35+
_APP_SERVICE_ATTRIBUTE_ENV_VARS = {
36+
ResourceAttributes.CLOUD_REGION: _REGION_NAME,
37+
ResourceAttributes.DEPLOYMENT_ENVIRONMENT: _WEBSITE_SLOT_NAME,
38+
ResourceAttributes.HOST_ID: _WEBSITE_HOSTNAME,
39+
ResourceAttributes.SERVICE_INSTANCE_ID: _WEBSITE_INSTANCE_ID,
40+
_AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE: _WEBSITE_HOME_STAMPNAME,
41+
}
42+
43+
# Functions
44+
45+
_FUNCTIONS_WORKER_RUNTIME = "FUNCTIONS_WORKER_RUNTIME"
46+
47+
# Vm
48+
49+
_AZURE_VM_METADATA_ENDPOINT = "http://169.254.169.254/metadata/instance/compute?api-version=2021-12-13&format=json"
50+
_AZURE_VM_SCALE_SET_NAME_ATTRIBUTE = "azure.vm.scaleset.name"
51+
_AZURE_VM_SKU_ATTRIBUTE = "azure.vm.sku"
52+
53+
_EXPECTED_AZURE_AMS_ATTRIBUTES = [
54+
_AZURE_VM_SCALE_SET_NAME_ATTRIBUTE,
55+
_AZURE_VM_SKU_ATTRIBUTE,
56+
ResourceAttributes.CLOUD_PLATFORM,
57+
ResourceAttributes.CLOUD_PROVIDER,
58+
ResourceAttributes.CLOUD_REGION,
59+
ResourceAttributes.CLOUD_RESOURCE_ID,
60+
ResourceAttributes.HOST_ID,
61+
ResourceAttributes.HOST_NAME,
62+
ResourceAttributes.HOST_TYPE,
63+
ResourceAttributes.OS_TYPE,
64+
ResourceAttributes.OS_VERSION,
65+
ResourceAttributes.SERVICE_INSTANCE_ID,
66+
]
67+
68+
# cSpell:enable
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
from ._constants import (
18+
_AKS_ARM_NAMESPACE_ID,
19+
_FUNCTIONS_WORKER_RUNTIME,
20+
_WEBSITE_SITE_NAME,
21+
)
22+
23+
24+
def _is_on_aks() -> bool:
25+
return os.environ.get(_AKS_ARM_NAMESPACE_ID) is not None
26+
27+
28+
def _is_on_app_service() -> bool:
29+
return os.environ.get(_WEBSITE_SITE_NAME) is not None
30+
31+
32+
def _is_on_functions() -> bool:
33+
return os.environ.get(_FUNCTIONS_WORKER_RUNTIME) is not None
34+
35+
36+
def _can_ignore_vm_detect() -> bool:
37+
return _is_on_aks() or _is_on_app_service() or _is_on_functions()

Diff for: resource/opentelemetry-resource-detector-azure/src/opentelemetry/resource/detector/azure/app_service.py

+6-18
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,12 @@
2121
ResourceAttributes,
2222
)
2323

24-
_AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE = "azure.app.service.stamp"
25-
_REGION_NAME = "REGION_NAME"
26-
_WEBSITE_HOME_STAMPNAME = "WEBSITE_HOME_STAMPNAME"
27-
_WEBSITE_HOSTNAME = "WEBSITE_HOSTNAME"
28-
_WEBSITE_INSTANCE_ID = "WEBSITE_INSTANCE_ID"
29-
_WEBSITE_OWNER_NAME = "WEBSITE_OWNER_NAME"
30-
_WEBSITE_RESOURCE_GROUP = "WEBSITE_RESOURCE_GROUP"
31-
_WEBSITE_SITE_NAME = "WEBSITE_SITE_NAME"
32-
_WEBSITE_SLOT_NAME = "WEBSITE_SLOT_NAME"
33-
34-
35-
_APP_SERVICE_ATTRIBUTE_ENV_VARS = {
36-
ResourceAttributes.CLOUD_REGION: _REGION_NAME,
37-
ResourceAttributes.DEPLOYMENT_ENVIRONMENT: _WEBSITE_SLOT_NAME,
38-
ResourceAttributes.HOST_ID: _WEBSITE_HOSTNAME,
39-
ResourceAttributes.SERVICE_INSTANCE_ID: _WEBSITE_INSTANCE_ID,
40-
_AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE: _WEBSITE_HOME_STAMPNAME,
41-
}
24+
from ._constants import (
25+
_APP_SERVICE_ATTRIBUTE_ENV_VARS,
26+
_WEBSITE_OWNER_NAME,
27+
_WEBSITE_RESOURCE_GROUP,
28+
_WEBSITE_SITE_NAME,
29+
)
4230

4331

4432
class AzureAppServiceResourceDetector(ResourceDetector):

Diff for: resource/opentelemetry-resource-detector-azure/src/opentelemetry/resource/detector/azure/vm.py

+17-26
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,31 @@
3030
ResourceAttributes,
3131
)
3232

33-
_AZURE_VM_METADATA_ENDPOINT = "http://169.254.169.254/metadata/instance/compute?api-version=2021-12-13&format=json"
34-
_AZURE_VM_SCALE_SET_NAME_ATTRIBUTE = "azure.vm.scaleset.name"
35-
_AZURE_VM_SKU_ATTRIBUTE = "azure.vm.sku"
36-
_logger = getLogger(__name__)
37-
38-
EXPECTED_AZURE_AMS_ATTRIBUTES = [
33+
from ._constants import (
34+
_AZURE_VM_METADATA_ENDPOINT,
3935
_AZURE_VM_SCALE_SET_NAME_ATTRIBUTE,
4036
_AZURE_VM_SKU_ATTRIBUTE,
41-
ResourceAttributes.CLOUD_PLATFORM,
42-
ResourceAttributes.CLOUD_PROVIDER,
43-
ResourceAttributes.CLOUD_REGION,
44-
ResourceAttributes.CLOUD_RESOURCE_ID,
45-
ResourceAttributes.HOST_ID,
46-
ResourceAttributes.HOST_NAME,
47-
ResourceAttributes.HOST_TYPE,
48-
ResourceAttributes.OS_TYPE,
49-
ResourceAttributes.OS_VERSION,
50-
ResourceAttributes.SERVICE_INSTANCE_ID,
51-
]
37+
_EXPECTED_AZURE_AMS_ATTRIBUTES,
38+
)
39+
from ._utils import _can_ignore_vm_detect
40+
41+
_logger = getLogger(__name__)
5242

5343

5444
class AzureVMResourceDetector(ResourceDetector):
5545
# pylint: disable=no-self-use
5646
def detect(self) -> "Resource":
5747
attributes = {}
58-
token = attach(set_value(_SUPPRESS_INSTRUMENTATION_KEY, True))
59-
metadata_json = _get_azure_vm_metadata()
60-
if not metadata_json:
61-
return Resource(attributes)
62-
for attribute_key in EXPECTED_AZURE_AMS_ATTRIBUTES:
63-
attributes[attribute_key] = _get_attribute_from_metadata(
64-
metadata_json, attribute_key
65-
)
66-
detach(token)
48+
if not _can_ignore_vm_detect():
49+
token = attach(set_value(_SUPPRESS_INSTRUMENTATION_KEY, True))
50+
metadata_json = _get_azure_vm_metadata()
51+
if not metadata_json:
52+
return Resource(attributes)
53+
for attribute_key in _EXPECTED_AZURE_AMS_ATTRIBUTES:
54+
attributes[attribute_key] = _get_attribute_from_metadata(
55+
metadata_json, attribute_key
56+
)
57+
detach(token)
6758
return Resource(attributes)
6859

6960

Diff for: resource/opentelemetry-resource-detector-azure/tests/test_vm.py

+10
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,13 @@ def test_windows(self, mock_urlopen):
378378
attributes = AzureVMResourceDetector().detect().attributes
379379
for attribute_key, attribute_value in WINDOWS_ATTRIBUTES.items():
380380
self.assertEqual(attributes[attribute_key], attribute_value)
381+
382+
@patch("opentelemetry.resource.detector.azure.vm._can_ignore_vm_detect")
383+
@patch("opentelemetry.resource.detector.azure.vm.urlopen")
384+
def test_in_another_rp(self, mock_urlopen, detect_mock):
385+
mock_urlopen.return_value.__enter__.return_value.read.return_value = (
386+
LINUX_JSON
387+
)
388+
detect_mock.return_value = True
389+
attributes = AzureVMResourceDetector().detect().attributes
390+
self.assertEqual(attributes, {})

0 commit comments

Comments
 (0)