mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-07-28 20:52:57 +08:00
Ignore vm detector if already in other rps for azure resource detector (#2456)
This commit is contained in:
@ -7,9 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
- Ignore vm detector if already in other rps
|
||||||
|
([#2456](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2456))
|
||||||
|
|
||||||
|
## Version 0.1.4 (2024-04-05)
|
||||||
|
|
||||||
|
- Fix windows tests/suppress instrumentation for urllib call
|
||||||
|
([#2178](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2178))
|
||||||
|
|
||||||
|
## Version 0.1.3 (2024-01-25)
|
||||||
|
|
||||||
- Change meta data service timeout to 200ms
|
- Change meta data service timeout to 200ms
|
||||||
([#2387](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2387))
|
([#2387](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2387))
|
||||||
|
|
||||||
## Version 0.1.2 (2024-01-25)
|
## Version 0.1.1 (2024-01-10)
|
||||||
|
|
||||||
- Initial CHANGELOG.md entry
|
- Initial CHANGELOG.md entry
|
||||||
|
@ -0,0 +1,25 @@
|
|||||||
|
# Copyright The OpenTelemetry Authors
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
# pylint: disable=import-error
|
||||||
|
|
||||||
|
from .app_service import AzureAppServiceResourceDetector
|
||||||
|
from .version import __version__
|
||||||
|
from .vm import AzureVMResourceDetector
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"AzureAppServiceResourceDetector",
|
||||||
|
"AzureVMResourceDetector",
|
||||||
|
"__version__",
|
||||||
|
]
|
@ -0,0 +1,68 @@
|
|||||||
|
# Copyright The OpenTelemetry Authors
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
from opentelemetry.semconv.resource import ResourceAttributes
|
||||||
|
|
||||||
|
# cSpell:disable
|
||||||
|
|
||||||
|
# Azure Kubernetes
|
||||||
|
|
||||||
|
_AKS_ARM_NAMESPACE_ID = "AKS_ARM_NAMESPACE_ID"
|
||||||
|
|
||||||
|
# AppService
|
||||||
|
|
||||||
|
_AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE = "azure.app.service.stamp"
|
||||||
|
_REGION_NAME = "REGION_NAME"
|
||||||
|
_WEBSITE_HOME_STAMPNAME = "WEBSITE_HOME_STAMPNAME"
|
||||||
|
_WEBSITE_HOSTNAME = "WEBSITE_HOSTNAME"
|
||||||
|
_WEBSITE_INSTANCE_ID = "WEBSITE_INSTANCE_ID"
|
||||||
|
_WEBSITE_OWNER_NAME = "WEBSITE_OWNER_NAME"
|
||||||
|
_WEBSITE_RESOURCE_GROUP = "WEBSITE_RESOURCE_GROUP"
|
||||||
|
_WEBSITE_SITE_NAME = "WEBSITE_SITE_NAME"
|
||||||
|
_WEBSITE_SLOT_NAME = "WEBSITE_SLOT_NAME"
|
||||||
|
|
||||||
|
_APP_SERVICE_ATTRIBUTE_ENV_VARS = {
|
||||||
|
ResourceAttributes.CLOUD_REGION: _REGION_NAME,
|
||||||
|
ResourceAttributes.DEPLOYMENT_ENVIRONMENT: _WEBSITE_SLOT_NAME,
|
||||||
|
ResourceAttributes.HOST_ID: _WEBSITE_HOSTNAME,
|
||||||
|
ResourceAttributes.SERVICE_INSTANCE_ID: _WEBSITE_INSTANCE_ID,
|
||||||
|
_AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE: _WEBSITE_HOME_STAMPNAME,
|
||||||
|
}
|
||||||
|
|
||||||
|
# Functions
|
||||||
|
|
||||||
|
_FUNCTIONS_WORKER_RUNTIME = "FUNCTIONS_WORKER_RUNTIME"
|
||||||
|
|
||||||
|
# Vm
|
||||||
|
|
||||||
|
_AZURE_VM_METADATA_ENDPOINT = "http://169.254.169.254/metadata/instance/compute?api-version=2021-12-13&format=json"
|
||||||
|
_AZURE_VM_SCALE_SET_NAME_ATTRIBUTE = "azure.vm.scaleset.name"
|
||||||
|
_AZURE_VM_SKU_ATTRIBUTE = "azure.vm.sku"
|
||||||
|
|
||||||
|
_EXPECTED_AZURE_AMS_ATTRIBUTES = [
|
||||||
|
_AZURE_VM_SCALE_SET_NAME_ATTRIBUTE,
|
||||||
|
_AZURE_VM_SKU_ATTRIBUTE,
|
||||||
|
ResourceAttributes.CLOUD_PLATFORM,
|
||||||
|
ResourceAttributes.CLOUD_PROVIDER,
|
||||||
|
ResourceAttributes.CLOUD_REGION,
|
||||||
|
ResourceAttributes.CLOUD_RESOURCE_ID,
|
||||||
|
ResourceAttributes.HOST_ID,
|
||||||
|
ResourceAttributes.HOST_NAME,
|
||||||
|
ResourceAttributes.HOST_TYPE,
|
||||||
|
ResourceAttributes.OS_TYPE,
|
||||||
|
ResourceAttributes.OS_VERSION,
|
||||||
|
ResourceAttributes.SERVICE_INSTANCE_ID,
|
||||||
|
]
|
||||||
|
|
||||||
|
# cSpell:enable
|
@ -0,0 +1,37 @@
|
|||||||
|
# Copyright The OpenTelemetry Authors
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from ._constants import (
|
||||||
|
_AKS_ARM_NAMESPACE_ID,
|
||||||
|
_FUNCTIONS_WORKER_RUNTIME,
|
||||||
|
_WEBSITE_SITE_NAME,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _is_on_aks() -> bool:
|
||||||
|
return os.environ.get(_AKS_ARM_NAMESPACE_ID) is not None
|
||||||
|
|
||||||
|
|
||||||
|
def _is_on_app_service() -> bool:
|
||||||
|
return os.environ.get(_WEBSITE_SITE_NAME) is not None
|
||||||
|
|
||||||
|
|
||||||
|
def _is_on_functions() -> bool:
|
||||||
|
return os.environ.get(_FUNCTIONS_WORKER_RUNTIME) is not None
|
||||||
|
|
||||||
|
|
||||||
|
def _can_ignore_vm_detect() -> bool:
|
||||||
|
return _is_on_aks() or _is_on_app_service() or _is_on_functions()
|
@ -21,24 +21,12 @@ from opentelemetry.semconv.resource import (
|
|||||||
ResourceAttributes,
|
ResourceAttributes,
|
||||||
)
|
)
|
||||||
|
|
||||||
_AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE = "azure.app.service.stamp"
|
from ._constants import (
|
||||||
_REGION_NAME = "REGION_NAME"
|
_APP_SERVICE_ATTRIBUTE_ENV_VARS,
|
||||||
_WEBSITE_HOME_STAMPNAME = "WEBSITE_HOME_STAMPNAME"
|
_WEBSITE_OWNER_NAME,
|
||||||
_WEBSITE_HOSTNAME = "WEBSITE_HOSTNAME"
|
_WEBSITE_RESOURCE_GROUP,
|
||||||
_WEBSITE_INSTANCE_ID = "WEBSITE_INSTANCE_ID"
|
_WEBSITE_SITE_NAME,
|
||||||
_WEBSITE_OWNER_NAME = "WEBSITE_OWNER_NAME"
|
)
|
||||||
_WEBSITE_RESOURCE_GROUP = "WEBSITE_RESOURCE_GROUP"
|
|
||||||
_WEBSITE_SITE_NAME = "WEBSITE_SITE_NAME"
|
|
||||||
_WEBSITE_SLOT_NAME = "WEBSITE_SLOT_NAME"
|
|
||||||
|
|
||||||
|
|
||||||
_APP_SERVICE_ATTRIBUTE_ENV_VARS = {
|
|
||||||
ResourceAttributes.CLOUD_REGION: _REGION_NAME,
|
|
||||||
ResourceAttributes.DEPLOYMENT_ENVIRONMENT: _WEBSITE_SLOT_NAME,
|
|
||||||
ResourceAttributes.HOST_ID: _WEBSITE_HOSTNAME,
|
|
||||||
ResourceAttributes.SERVICE_INSTANCE_ID: _WEBSITE_INSTANCE_ID,
|
|
||||||
_AZURE_APP_SERVICE_STAMP_RESOURCE_ATTRIBUTE: _WEBSITE_HOME_STAMPNAME,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class AzureAppServiceResourceDetector(ResourceDetector):
|
class AzureAppServiceResourceDetector(ResourceDetector):
|
||||||
|
@ -30,40 +30,31 @@ from opentelemetry.semconv.resource import (
|
|||||||
ResourceAttributes,
|
ResourceAttributes,
|
||||||
)
|
)
|
||||||
|
|
||||||
_AZURE_VM_METADATA_ENDPOINT = "http://169.254.169.254/metadata/instance/compute?api-version=2021-12-13&format=json"
|
from ._constants import (
|
||||||
_AZURE_VM_SCALE_SET_NAME_ATTRIBUTE = "azure.vm.scaleset.name"
|
_AZURE_VM_METADATA_ENDPOINT,
|
||||||
_AZURE_VM_SKU_ATTRIBUTE = "azure.vm.sku"
|
|
||||||
_logger = getLogger(__name__)
|
|
||||||
|
|
||||||
EXPECTED_AZURE_AMS_ATTRIBUTES = [
|
|
||||||
_AZURE_VM_SCALE_SET_NAME_ATTRIBUTE,
|
_AZURE_VM_SCALE_SET_NAME_ATTRIBUTE,
|
||||||
_AZURE_VM_SKU_ATTRIBUTE,
|
_AZURE_VM_SKU_ATTRIBUTE,
|
||||||
ResourceAttributes.CLOUD_PLATFORM,
|
_EXPECTED_AZURE_AMS_ATTRIBUTES,
|
||||||
ResourceAttributes.CLOUD_PROVIDER,
|
)
|
||||||
ResourceAttributes.CLOUD_REGION,
|
from ._utils import _can_ignore_vm_detect
|
||||||
ResourceAttributes.CLOUD_RESOURCE_ID,
|
|
||||||
ResourceAttributes.HOST_ID,
|
_logger = getLogger(__name__)
|
||||||
ResourceAttributes.HOST_NAME,
|
|
||||||
ResourceAttributes.HOST_TYPE,
|
|
||||||
ResourceAttributes.OS_TYPE,
|
|
||||||
ResourceAttributes.OS_VERSION,
|
|
||||||
ResourceAttributes.SERVICE_INSTANCE_ID,
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
class AzureVMResourceDetector(ResourceDetector):
|
class AzureVMResourceDetector(ResourceDetector):
|
||||||
# pylint: disable=no-self-use
|
# pylint: disable=no-self-use
|
||||||
def detect(self) -> "Resource":
|
def detect(self) -> "Resource":
|
||||||
attributes = {}
|
attributes = {}
|
||||||
token = attach(set_value(_SUPPRESS_INSTRUMENTATION_KEY, True))
|
if not _can_ignore_vm_detect():
|
||||||
metadata_json = _get_azure_vm_metadata()
|
token = attach(set_value(_SUPPRESS_INSTRUMENTATION_KEY, True))
|
||||||
if not metadata_json:
|
metadata_json = _get_azure_vm_metadata()
|
||||||
return Resource(attributes)
|
if not metadata_json:
|
||||||
for attribute_key in EXPECTED_AZURE_AMS_ATTRIBUTES:
|
return Resource(attributes)
|
||||||
attributes[attribute_key] = _get_attribute_from_metadata(
|
for attribute_key in _EXPECTED_AZURE_AMS_ATTRIBUTES:
|
||||||
metadata_json, attribute_key
|
attributes[attribute_key] = _get_attribute_from_metadata(
|
||||||
)
|
metadata_json, attribute_key
|
||||||
detach(token)
|
)
|
||||||
|
detach(token)
|
||||||
return Resource(attributes)
|
return Resource(attributes)
|
||||||
|
|
||||||
|
|
||||||
|
@ -378,3 +378,13 @@ class TestAzureVMResourceDetector(unittest.TestCase):
|
|||||||
attributes = AzureVMResourceDetector().detect().attributes
|
attributes = AzureVMResourceDetector().detect().attributes
|
||||||
for attribute_key, attribute_value in WINDOWS_ATTRIBUTES.items():
|
for attribute_key, attribute_value in WINDOWS_ATTRIBUTES.items():
|
||||||
self.assertEqual(attributes[attribute_key], attribute_value)
|
self.assertEqual(attributes[attribute_key], attribute_value)
|
||||||
|
|
||||||
|
@patch("opentelemetry.resource.detector.azure.vm._can_ignore_vm_detect")
|
||||||
|
@patch("opentelemetry.resource.detector.azure.vm.urlopen")
|
||||||
|
def test_in_another_rp(self, mock_urlopen, detect_mock):
|
||||||
|
mock_urlopen.return_value.__enter__.return_value.read.return_value = (
|
||||||
|
LINUX_JSON
|
||||||
|
)
|
||||||
|
detect_mock.return_value = True
|
||||||
|
attributes = AzureVMResourceDetector().detect().attributes
|
||||||
|
self.assertEqual(attributes, {})
|
||||||
|
Reference in New Issue
Block a user