diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md index c09f1791c2ce..910702a5742d 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md @@ -25,6 +25,8 @@ - Updated FastAPI sample ([#34738](https://github.com/Azure/azure-sdk-for-python/pull/34738)) +- Set up branching logic for attach function + ([#35066](https://github.com/Azure/azure-sdk-for-python/pull/35066)) ## 1.0.0b23 (2024-02-28) 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 9ef51c6202f5..e6718737f350 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 @@ -50,7 +50,9 @@ def _is_on_functions(): return environ.get(_FUNCTIONS_WORKER_RUNTIME) is not None def _is_attach_enabled(): - return isdir("/agents/python/") + if _is_on_app_service(): + return isdir("/agents/python/") + return False # AKS 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 fc08963ffbec..040393c13655 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/test_utils.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/tests/test_utils.py @@ -104,110 +104,161 @@ def test_create_telemetry_item(self, mock_ns_to_iso_str): ) self.assertEqual(result, expected) - # Unknown + # Unknown SDK Version Prefix - @patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=False) + @patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=False) @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="") def test_get_sdk_version_prefix(self, mock_system, mock_getenv): result = _utils._get_sdk_version_prefix() self.assertEqual(result, "uum_") - @patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=False) + @patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=False) @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Linux") def test_get_sdk_version_prefix_linux(self, mock_system, mock_getenv): result = _utils._get_sdk_version_prefix() self.assertEqual(result, "ulm_") - @patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=False) + @patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=False) @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Windows") def test_get_sdk_version_prefix_windows(self, mock_system, mock_getenv): result = _utils._get_sdk_version_prefix() self.assertEqual(result, "uwm_") - # App Service + @patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=True) + @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="") + def test_get_sdk_version_prefix_attach(self, mock_system, mock_getenv): + result = _utils._get_sdk_version_prefix() + self.assertEqual(result, "uui_") + + @patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=True) + @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Linux") + def test_get_sdk_version_prefix_attach_linux(self, mock_system, mock_getenv): + result = _utils._get_sdk_version_prefix() + self.assertEqual(result, "uli_") + + @patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=True) + @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Windows") + def test_get_sdk_version_prefix_attach_windows(self, mock_system, mock_getenv): + result = _utils._get_sdk_version_prefix() + self.assertEqual(result, "uwi_") + + # App Service SDK Version Prefix - @patch("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}) + @patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}, clear=True) @patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=False) @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="") def test_get_sdk_version_prefix_app_service(self, mock_system, mock_getenv): result = _utils._get_sdk_version_prefix() self.assertEqual(result, "aum_") - @patch("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}) + @patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}, clear=True) @patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=False) @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Linux") def test_get_sdk_version_prefix_app_service_linux(self, mock_system, mock_getenv): result = _utils._get_sdk_version_prefix() self.assertEqual(result, "alm_") - @patch("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}) + @patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}, clear=True) @patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=False) @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Windows") def test_get_sdk_version_prefix_app_service_windows(self, mock_system, mock_getenv): result = _utils._get_sdk_version_prefix() self.assertEqual(result, "awm_") - @patch("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}) + @patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}, clear=True) @patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=True) @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="") def test_get_sdk_version_prefix_app_service_attach(self, mock_system, mock_getenv): result = _utils._get_sdk_version_prefix() self.assertEqual(result, "aui_") - @patch("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}) + @patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}, clear=True) @patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=True) @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Linux") def test_get_sdk_version_prefix_app_service_linux_attach(self, mock_system, mock_getenv): result = _utils._get_sdk_version_prefix() self.assertEqual(result, "ali_") - @patch("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}) + @patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}, clear=True) @patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=True) @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Windows") def test_get_sdk_version_prefix_app_service_windows_attach(self, mock_system, mock_getenv): result = _utils._get_sdk_version_prefix() self.assertEqual(result, "awi_") - # Function + # Function SDK Version Prefix - @patch("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}) - @patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=False) + @patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}, clear=True) + @patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=False) @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="") def test_get_sdk_version_prefix_function(self, mock_system, mock_getenv): result = _utils._get_sdk_version_prefix() self.assertEqual(result, "fum_") - @patch("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}) - @patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=False) + @patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}, clear=True) + @patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=False) @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Linux") def test_get_sdk_version_prefix_function_linux(self, mock_system, mock_getenv): result = _utils._get_sdk_version_prefix() self.assertEqual(result, "flm_") - @patch("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}) - @patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=False) + @patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}, clear=True) + @patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=False) @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Windows") def test_get_sdk_version_prefix_function_windows(self, mock_system, mock_getenv): result = _utils._get_sdk_version_prefix() self.assertEqual(result, "fwm_") - @patch("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}) - @patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=True) + @patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}, clear=True) + @patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=True) @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="") def test_get_sdk_version_prefix_function_attach(self, mock_system, mock_getenv): result = _utils._get_sdk_version_prefix() self.assertEqual(result, "fui_") - @patch("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}) - @patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=True) + @patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}, clear=True) + @patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=True) @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Linux") def test_get_sdk_version_prefix_function_linux_attach(self, mock_system, mock_getenv): result = _utils._get_sdk_version_prefix() self.assertEqual(result, "fli_") - @patch("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}) - @patch("azure.monitor.opentelemetry.exporter._utils.isdir", return_value=True) + @patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"FUNCTIONS_WORKER_RUNTIME": TEST_WEBSITE_SITE_NAME}, clear=True) + @patch("azure.monitor.opentelemetry.exporter._utils._is_attach_enabled", return_value=True) @patch("azure.monitor.opentelemetry.exporter._utils.platform.system", return_value="Windows") def test_get_sdk_version_prefix_function_windows_attach(self, mock_system, mock_getenv): result = _utils._get_sdk_version_prefix() self.assertEqual(result, "fwi_") + + # Attach + + @patch( + "azure.monitor.opentelemetry.exporter._utils.isdir", + return_value=True, + ) + @patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}) + def test_attach_enabled(self, mock_isdir): + self.assertEqual( + _utils._is_attach_enabled(), True + ) + + @patch( + "azure.monitor.opentelemetry.exporter._utils.isdir", + return_value=False, + ) + @patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {"WEBSITE_SITE_NAME": TEST_WEBSITE_SITE_NAME}) + def test_attach_app_service_disabled(self, mock_isdir): + self.assertEqual( + _utils._is_attach_enabled(), False + ) + + @patch( + "azure.monitor.opentelemetry.exporter._utils.isdir", + return_value=True, + ) + @patch.dict("azure.monitor.opentelemetry.exporter._utils.environ", {}, clear=True) + def test_attach_off_app_service_with_agent(self, mock_isdir): + # This is not an expected scenario and just tests the default + self.assertEqual( + _utils._is_attach_enabled(), False + ) diff --git a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md index d913fb578dc3..d3fd87489522 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md @@ -15,6 +15,8 @@ - Updated FastAPI sample ([#34738](https://github.com/Azure/azure-sdk-for-python/pull/34738)) +- Refactored constants and utils + ([#35066](https://github.com/Azure/azure-sdk-for-python/pull/35066)) ## 1.3.0 (2024-02-29) diff --git a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_autoinstrumentation/distro.py b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_autoinstrumentation/distro.py index 12e6970df5bb..364c80df0cf8 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_autoinstrumentation/distro.py +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_autoinstrumentation/distro.py @@ -35,7 +35,7 @@ from azure.monitor.opentelemetry._diagnostics.status_logger import ( AzureStatusLogger, ) -from azure.monitor.opentelemetry._util.configurations import ( +from azure.monitor.opentelemetry._utils.configurations import ( _get_otel_disabled_instrumentations, ) diff --git a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py index a2d19b153be5..9abe030aacf0 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py @@ -49,7 +49,7 @@ _DISTRO_DETECTS_ATTACH, AzureDiagnosticLogging, ) -from azure.monitor.opentelemetry._util.configurations import ( +from azure.monitor.opentelemetry._utils.configurations import ( _get_configurations, _is_instrumentation_enabled, ) diff --git a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py index 70ab4882b129..aa0a951cc5e2 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py @@ -4,14 +4,6 @@ # license information. # -------------------------------------------------------------------------- -import logging -import platform -from os import environ -from pathlib import Path - -from azure.monitor.opentelemetry.exporter._connection_string_parser import ( # pylint: disable=import-error,no-name-in-module - ConnectionStringParser, -) from azure.monitor.opentelemetry.exporter._constants import ( # pylint: disable=import-error,no-name-in-module _AZURE_MONITOR_DISTRO_VERSION_ARG, ) @@ -35,52 +27,10 @@ _LOG_PATH_LINUX = "/var/log/applicationinsights" _LOG_PATH_WINDOWS = "\\LogFiles\\ApplicationInsights" -_IS_ON_APP_SERVICE = "WEBSITE_SITE_NAME" in environ -# TODO: Add environment variable to enabled diagnostics off of App Service -_IS_DIAGNOSTICS_ENABLED = _IS_ON_APP_SERVICE -_CUSTOMER_IKEY_ENV_VAR = None _PREVIEW_ENTRY_POINT_WARNING = "Autoinstrumentation for the Azure Monitor OpenTelemetry Distro is in preview." -logger = logging.getLogger(__name__) - - -# pylint: disable=global-statement -def _get_customer_ikey_from_env_var(): - global _CUSTOMER_IKEY_ENV_VAR - if not _CUSTOMER_IKEY_ENV_VAR: - _CUSTOMER_IKEY_ENV_VAR = "unknown" - try: - _CUSTOMER_IKEY_ENV_VAR = ( - ConnectionStringParser().instrumentation_key - ) - except ValueError as e: - logger.error("Failed to parse Instrumentation Key: %s", e) - return _CUSTOMER_IKEY_ENV_VAR - -def _get_log_path(status_log_path=False): - system = platform.system() - if system == "Linux": - return _LOG_PATH_LINUX - if system == "Windows": - log_path = str(Path.home()) + _LOG_PATH_WINDOWS - if status_log_path: - return log_path + "\\status" - return log_path - return None - - -def _env_var_or_default(var_name, default_val=""): - try: - return environ[var_name] - except KeyError: - return default_val - - -_EXTENSION_VERSION = _env_var_or_default( - "ApplicationInsightsAgent_EXTENSION_VERSION", "disabled" -) -# Instrumentations +# --------------------Instrumentations------------------------------ # Opt-out _AZURE_SDK_INSTRUMENTATION_NAME = "azure_sdk" diff --git a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/diagnostic_logging.py b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/diagnostic_logging.py index 6ba4cd6ef89a..650dd67cfe0e 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/diagnostic_logging.py +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/diagnostic_logging.py @@ -9,7 +9,7 @@ from os import makedirs from os.path import exists, join -from azure.monitor.opentelemetry._constants import ( +from azure.monitor.opentelemetry._utils import ( _EXTENSION_VERSION, _IS_DIAGNOSTICS_ENABLED, _env_var_or_default, diff --git a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/status_logger.py b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/status_logger.py index ad91876fad12..80fefe3baf7a 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/status_logger.py +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/status_logger.py @@ -9,7 +9,7 @@ from os.path import exists, join from platform import node -from azure.monitor.opentelemetry._constants import ( +from azure.monitor.opentelemetry._utils import ( _EXTENSION_VERSION, _IS_DIAGNOSTICS_ENABLED, _get_customer_ikey_from_env_var, diff --git a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_util/__init__.py b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_util/__init__.py deleted file mode 100644 index 0bdee620366b..000000000000 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_util/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -# ------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License in the project root for -# license information. -# -------------------------------------------------------------------------- diff --git a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_utils/__init__.py b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_utils/__init__.py new file mode 100644 index 000000000000..c51940938b66 --- /dev/null +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_utils/__init__.py @@ -0,0 +1,67 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + +import logging +import platform +from os import environ +from pathlib import Path + +from azure.monitor.opentelemetry.exporter._connection_string_parser import ( # pylint: disable=import-error,no-name-in-module + ConnectionStringParser, +) +from azure.monitor.opentelemetry.exporter._utils import _is_on_app_service # pylint: disable=import-error,no-name-in-module +from azure.monitor.opentelemetry._constants import ( + _LOG_PATH_LINUX, + _LOG_PATH_WINDOWS, +) + + +logger = logging.getLogger(__name__) + + +# --------------------Diagnostic/status logging------------------------------ + +# TODO: Add environment variable to enabled diagnostics off of App Service +_IS_DIAGNOSTICS_ENABLED = _is_on_app_service() +_CUSTOMER_IKEY_ENV_VAR = None + + +# pylint: disable=global-statement +def _get_customer_ikey_from_env_var(): + global _CUSTOMER_IKEY_ENV_VAR + if not _CUSTOMER_IKEY_ENV_VAR: + _CUSTOMER_IKEY_ENV_VAR = "unknown" + try: + _CUSTOMER_IKEY_ENV_VAR = ( + ConnectionStringParser().instrumentation_key + ) + except ValueError as e: + logger.error("Failed to parse Instrumentation Key: %s", e) + return _CUSTOMER_IKEY_ENV_VAR + + +def _get_log_path(status_log_path=False): + system = platform.system() + if system == "Linux": + return _LOG_PATH_LINUX + if system == "Windows": + log_path = str(Path.home()) + _LOG_PATH_WINDOWS + if status_log_path: + return log_path + "\\status" + return log_path + return None + + +def _env_var_or_default(var_name, default_val=""): + try: + return environ[var_name] + except KeyError: + return default_val + + +_EXTENSION_VERSION = _env_var_or_default( + "ApplicationInsightsAgent_EXTENSION_VERSION", "disabled" +) diff --git a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_util/configurations.py b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_utils/configurations.py similarity index 100% rename from sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_util/configurations.py rename to sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_utils/configurations.py diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_configure.py b/sdk/monitor/azure-monitor-opentelemetry/tests/test_configure.py similarity index 100% rename from sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_configure.py rename to sdk/monitor/azure-monitor-opentelemetry/tests/test_configure.py diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_util.py b/sdk/monitor/azure-monitor-opentelemetry/tests/utils/test_configurations.py similarity index 95% rename from sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_util.py rename to sdk/monitor/azure-monitor-opentelemetry/tests/utils/test_configurations.py index 2eabc206e48c..8b10985f3880 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_util.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/utils/test_configurations.py @@ -19,7 +19,7 @@ from opentelemetry.instrumentation.environment_variables import ( OTEL_PYTHON_DISABLED_INSTRUMENTATIONS, ) -from azure.monitor.opentelemetry._util.configurations import ( +from azure.monitor.opentelemetry._utils.configurations import ( SAMPLING_RATIO_ENV_VAR, _get_configurations, ) @@ -38,9 +38,9 @@ }) -class TestUtil(TestCase): +class TestConfigurations(TestCase): @patch.dict("os.environ", {}, clear=True) - @patch("azure.monitor.opentelemetry._util.configurations._PREVIEW_INSTRUMENTED_LIBRARIES", ("previewlib1", "previewlib2")) + @patch("azure.monitor.opentelemetry._utils.configurations._PREVIEW_INSTRUMENTED_LIBRARIES", ("previewlib1", "previewlib2")) @patch("opentelemetry.sdk.resources.Resource.create", return_value=TEST_DEFAULT_RESOURCE) def test_get_configurations(self, resource_create_mock): configurations = _get_configurations( @@ -160,7 +160,7 @@ def test_get_configurations_env_vars_validation(self, resource_create_mock): clear=True, ) @patch("opentelemetry.sdk.resources.Resource.create", return_value=TEST_DEFAULT_RESOURCE) - @patch("azure.monitor.opentelemetry._util.configurations._PREVIEW_INSTRUMENTED_LIBRARIES", ("previewlib1", "previewlib2")) + @patch("azure.monitor.opentelemetry._utils.configurations._PREVIEW_INSTRUMENTED_LIBRARIES", ("previewlib1", "previewlib2")) def test_merge_instrumentation_options_conflict(self, resource_create_mock): configurations = _get_configurations( instrumentation_options = { @@ -186,7 +186,7 @@ def test_merge_instrumentation_options_conflict(self, resource_create_mock): }) @patch.dict("os.environ", {}, clear=True) - @patch("azure.monitor.opentelemetry._util.configurations._PREVIEW_INSTRUMENTED_LIBRARIES", ("previewlib1", "previewlib2")) + @patch("azure.monitor.opentelemetry._utils.configurations._PREVIEW_INSTRUMENTED_LIBRARIES", ("previewlib1", "previewlib2")) @patch("opentelemetry.sdk.resources.Resource.create", return_value=TEST_DEFAULT_RESOURCE) def test_merge_instrumentation_options_extra_args(self, resource_create_mock): configurations = _get_configurations( diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py b/sdk/monitor/azure-monitor-opentelemetry/tests/utils/test_utils.py similarity index 64% rename from sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py rename to sdk/monitor/azure-monitor-opentelemetry/tests/utils/test_utils.py index 884e5df08960..54cf367a0ada 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/utils/test_utils.py @@ -9,7 +9,7 @@ from unittest import TestCase from unittest.mock import patch -from azure.monitor.opentelemetry import _constants +from azure.monitor.opentelemetry import _utils TEST_VALUE = "TEST_VALUE" TEST_IKEY = "1234abcd-ab12-34cd-ab12-a23456abcdef" @@ -21,111 +21,111 @@ def clear_env_var(env_var): del environ[env_var] -class TestConstants(TestCase): +class TestUtils(TestCase): @patch.dict( "os.environ", {"ApplicationInsightsAgent_EXTENSION_VERSION": TEST_VALUE}, ) def test_extension_version(self): - reload(_constants) - self.assertEqual(_constants._EXTENSION_VERSION, TEST_VALUE) + reload(_utils) + self.assertEqual(_utils._EXTENSION_VERSION, TEST_VALUE) def test_extension_version_default(self): clear_env_var("ApplicationInsightsAgent_EXTENSION_VERSION") - reload(_constants) - self.assertEqual(_constants._EXTENSION_VERSION, "disabled") + reload(_utils) + self.assertEqual(_utils._EXTENSION_VERSION, "disabled") @patch.dict( "os.environ", {"APPLICATIONINSIGHTS_CONNECTION_STRING": TEST_CONN_STR} ) def test_ikey(self): - reload(_constants) + reload(_utils) self.assertEqual( - _constants._get_customer_ikey_from_env_var(), TEST_IKEY + _utils._get_customer_ikey_from_env_var(), TEST_IKEY ) def test_ikey_defaults(self): clear_env_var("APPLICATIONINSIGHTS_CONNECTION_STRING") - reload(_constants) + reload(_utils) self.assertEqual( - _constants._get_customer_ikey_from_env_var(), "unknown" + _utils._get_customer_ikey_from_env_var(), "unknown" ) @patch.dict("os.environ", {"WEBSITE_SITE_NAME": TEST_VALUE}) def test_diagnostics_enabled(self): - reload(_constants) - self.assertTrue(_constants._IS_DIAGNOSTICS_ENABLED) + reload(_utils) + self.assertTrue(_utils._IS_DIAGNOSTICS_ENABLED) def test_diagnostics_disabled(self): clear_env_var("WEBSITE_SITE_NAME") - reload(_constants) - self.assertFalse(_constants._IS_DIAGNOSTICS_ENABLED) + reload(_utils) + self.assertFalse(_utils._IS_DIAGNOSTICS_ENABLED) @patch( - "azure.monitor.opentelemetry._constants.platform.system", + "azure.monitor.opentelemetry._utils.platform.system", return_value="Linux", ) def test_log_path_linux(self, mock_system): self.assertEqual( - _constants._get_log_path(), "/var/log/applicationinsights" + _utils._get_log_path(), "/var/log/applicationinsights" ) @patch( - "azure.monitor.opentelemetry._constants.platform.system", + "azure.monitor.opentelemetry._utils.platform.system", return_value="Linux", ) def test_status_log_path_linux(self, mock_system): self.assertEqual( - _constants._get_log_path(status_log_path=True), + _utils._get_log_path(status_log_path=True), "/var/log/applicationinsights", ) @patch( - "azure.monitor.opentelemetry._constants.platform.system", + "azure.monitor.opentelemetry._utils.platform.system", return_value="Windows", ) @patch("pathlib.Path.home", return_value="\\HOME\\DIR") def test_log_path_windows(self, mock_system, mock_home): self.assertEqual( - _constants._get_log_path(), + _utils._get_log_path(), "\\HOME\\DIR\\LogFiles\\ApplicationInsights", ) @patch( - "azure.monitor.opentelemetry._constants.platform.system", + "azure.monitor.opentelemetry._utils.platform.system", return_value="Windows", ) @patch("pathlib.Path.home", return_value="\\HOME\\DIR") def test_status_log_path_windows(self, mock_system, mock_home): self.assertEqual( - _constants._get_log_path(status_log_path=True), + _utils._get_log_path(status_log_path=True), "\\HOME\\DIR\\LogFiles\\ApplicationInsights\\status", ) @patch( - "azure.monitor.opentelemetry._constants.platform.system", + "azure.monitor.opentelemetry._utils.platform.system", return_value="Window", ) def test_log_path_other(self, mock_platform): - self.assertIsNone(_constants._get_log_path()) + self.assertIsNone(_utils._get_log_path()) @patch( - "azure.monitor.opentelemetry._constants.platform.system", + "azure.monitor.opentelemetry._utils.platform.system", return_value="linux", ) def test_status_log_path_other(self, mock_platform): - self.assertIsNone(_constants._get_log_path(status_log_path=True)) + self.assertIsNone(_utils._get_log_path(status_log_path=True)) @patch.dict("os.environ", {"key": "value"}) def test_env_var_or_default(self): - self.assertEqual(_constants._env_var_or_default("key"), "value") + self.assertEqual(_utils._env_var_or_default("key"), "value") @patch.dict("os.environ", {}) def test_env_var_or_default_empty(self): - self.assertEqual(_constants._env_var_or_default("key"), "") + self.assertEqual(_utils._env_var_or_default("key"), "") @patch.dict("os.environ", {}) def test_env_var_or_default_empty_with_defaults(self): self.assertEqual( - _constants._env_var_or_default("key", default_val="value"), "value" + _utils._env_var_or_default("key", default_val="value"), "value" )