From ea550afd8dc882576e283796108e05b9c37a10d7 Mon Sep 17 00:00:00 2001 From: Jeremy Voss Date: Wed, 28 May 2025 10:12:42 -0700 Subject: [PATCH 1/3] Make sure functions aren't marked as windows --- .../monitor/opentelemetry/exporter/_utils.py | 4 ++-- .../tests/test_utils.py | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_utils.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_utils.py index 3e8f3ee83f0e..3e666537723d 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_utils.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_utils.py @@ -70,10 +70,10 @@ def _is_on_aks(): def _is_attach_enabled(): - if _is_on_app_service(): - return isdir("/agents/python/") if _is_on_functions(): return environ.get(_PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY) == "true" + if _is_on_app_service(): + return isdir("/agents/python/") if _is_on_aks(): return _AKS_ARM_NAMESPACE_ID in environ return False diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/test_utils.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/test_utils.py index 685f8516e12e..a4358d4a46d6 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/test_utils.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/test_utils.py @@ -377,7 +377,10 @@ def test_get_sdk_version_prefix_app_service_windows_attach(self, mock_system, mo @patch.dict( "azure.monitor.opentelemetry.exporter._utils.environ", - {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}, + { + "FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME, + "WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME, + }, clear=True, ) @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="") @@ -387,7 +390,10 @@ def test_get_sdk_version_prefix_function(self, mock_system): @patch.dict( "azure.monitor.opentelemetry.exporter._utils.environ", - {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}, + { + "FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME, + "WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME, + }, clear=True, ) @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Linux") @@ -397,7 +403,10 @@ def test_get_sdk_version_prefix_function_linux(self, mock_system): @patch.dict( "azure.monitor.opentelemetry.exporter._utils.environ", - {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}, + { + "FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME, + "WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME, + }, clear=True, ) @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Windows") @@ -409,6 +418,7 @@ def test_get_sdk_version_prefix_function_windows(self, mock_system): "azure.monitor.opentelemetry.exporter._utils.environ", { "FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME, + "WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME, "PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY": "true", }, clear=True, @@ -422,6 +432,7 @@ def test_get_sdk_version_prefix_function_attach(self, mock_system): "azure.monitor.opentelemetry.exporter._utils.environ", { "FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME, + "WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME, "PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY": "true", }, clear=True, @@ -435,6 +446,7 @@ def test_get_sdk_version_prefix_function_linux_attach(self, mock_system): "azure.monitor.opentelemetry.exporter._utils.environ", { "FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME, + "WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME, "PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY": "true", }, clear=True, From 0bce3923eba685e776340fbc05021848e4472d50 Mon Sep 17 00:00:00 2001 From: Jeremy Voss Date: Wed, 28 May 2025 10:45:08 -0700 Subject: [PATCH 2/3] changelog --- sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md index c48c9a60f45f..1ca7bb837bd2 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md @@ -13,6 +13,9 @@ ### Bugs Fixed +- Do not count Functions as App Service + ([#41327](https://github.com/Azure/azure-sdk-for-python/pull/41327)) + ### Other Changes - Extend version range for `psutil` to include 7.x From adfd29e898106b31f2bbb8d43e2ad7b3df5b8735 Mon Sep 17 00:00:00 2001 From: Jeremy Voss Date: Wed, 28 May 2025 14:02:57 -0700 Subject: [PATCH 3/3] Fix statsbeat functions vs appsvc conflict --- .../exporter/statsbeat/_statsbeat_metrics.py | 10 +++++----- .../tests/statsbeat/test_statsbeat.py | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/statsbeat/_statsbeat_metrics.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/statsbeat/_statsbeat_metrics.py index 47647565c22e..f7e3e94e7815 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/statsbeat/_statsbeat_metrics.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/statsbeat/_statsbeat_metrics.py @@ -177,14 +177,14 @@ def _get_attach_metric(self, options: CallbackOptions) -> Iterable[Observation]: rpId = "" os_type = platform.system() # rp, rpId - if _utils._is_on_app_service(): - # Web apps - rp = _RP_Names.APP_SERVICE.value - rpId = "{}/{}".format(os.environ.get(_WEBSITE_SITE_NAME), os.environ.get(_WEBSITE_HOME_STAMPNAME, "")) - elif _utils._is_on_functions(): + if _utils._is_on_functions(): # Function apps rp = _RP_Names.FUNCTIONS.value rpId = os.environ.get(_WEBSITE_HOSTNAME, "") + elif _utils._is_on_app_service(): + # Web apps + rp = _RP_Names.APP_SERVICE.value + rpId = "{}/{}".format(os.environ.get(_WEBSITE_SITE_NAME), os.environ.get(_WEBSITE_HOME_STAMPNAME, "")) elif _utils._is_on_aks(): # AKS rp = _RP_Names.AKS.value diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/statsbeat/test_statsbeat.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/statsbeat/test_statsbeat.py index eff9d23365ce..e777e06ed0c0 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/statsbeat/test_statsbeat.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/statsbeat/test_statsbeat.py @@ -424,6 +424,8 @@ def test_get_attach_metric_appsvc(self, metadata_mock): os.environ, { "FUNCTIONS_WORKER_RUNTIME": "runtime", + # Functions can have WEBSITE_SITE_NAME + "WEBSITE_SITE_NAME": "site_name", "WEBSITE_HOSTNAME": "host_name", }, )