From 24670406e68a73c6fadcd00251208fcef89d8a66 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Thu, 3 Oct 2024 15:42:39 -0700 Subject: [PATCH] str --- azure_functions_worker/constants.py | 3 ++- azure_functions_worker/dispatcher.py | 7 +++++-- tests/unittests/test_opentelemetry.py | 27 +++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/azure_functions_worker/constants.py b/azure_functions_worker/constants.py index b916252c..362b4ab7 100644 --- a/azure_functions_worker/constants.py +++ b/azure_functions_worker/constants.py @@ -82,9 +82,10 @@ BASE_EXT_SUPPORTED_PY_MINOR_VERSION = 8 # Appsetting to turn on OpenTelemetry support/features +# A value of "true" enables the setting, defaults to "false" # Includes turning on Azure monitor distro to send telemetry to AppInsights PYTHON_ENABLE_OPENTELEMETRY = "PYTHON_ENABLE_OPENTELEMETRY" -PYTHON_ENABLE_OPENTELEMETRY_DEFAULT = False +PYTHON_ENABLE_OPENTELEMETRY_DEFAULT = "false" # Appsetting to specify root logger name of logger to collect telemetry for # Used by Azure monitor distro diff --git a/azure_functions_worker/dispatcher.py b/azure_functions_worker/dispatcher.py index 897a3499..5b566904 100644 --- a/azure_functions_worker/dispatcher.py +++ b/azure_functions_worker/dispatcher.py @@ -381,8 +381,11 @@ async def _handle__worker_init_request(self, request): constants.RPC_HTTP_TRIGGER_METADATA_REMOVED: _TRUE, constants.SHARED_MEMORY_DATA_TRANSFER: _TRUE, } - if get_app_setting(setting=PYTHON_ENABLE_OPENTELEMETRY, - default_value=PYTHON_ENABLE_OPENTELEMETRY_DEFAULT): + opentelemetry_app_setting = get_app_setting( + setting=PYTHON_ENABLE_OPENTELEMETRY, + default_value=PYTHON_ENABLE_OPENTELEMETRY_DEFAULT, + ) + if opentelemetry_app_setting and opentelemetry_app_setting.lower() == "true": self.initialize_azure_monitor() if self._azure_monitor_available: diff --git a/tests/unittests/test_opentelemetry.py b/tests/unittests/test_opentelemetry.py index b26334bd..e32c426d 100644 --- a/tests/unittests/test_opentelemetry.py +++ b/tests/unittests/test_opentelemetry.py @@ -83,6 +83,33 @@ def test_init_request_otel_capability_enabled_app_setting( self.assertIn("WorkerOpenTelemetryEnabled", capabilities) self.assertEqual(capabilities["WorkerOpenTelemetryEnabled"], "true") + @patch("azure_functions_worker.dispatcher.Dispatcher.initialize_azure_monitor") + def test_init_request_otel_capability_default_app_setting( + self, + mock_initialize_azmon, + ): + + init_request = protos.StreamingMessage( + worker_init_request=protos.WorkerInitRequest( + host_version="2.3.4", + function_app_directory=str(FUNCTION_APP_DIRECTORY) + ) + ) + + init_response = self.loop.run_until_complete( + self.dispatcher._handle__worker_init_request(init_request)) + + self.assertEqual(init_response.worker_init_response.result.status, + protos.StatusResult.Success) + + # Azure monitor initialized not called + mock_initialize_azmon.assert_not_called() + + # Verify that WorkerOpenTelemetryEnabled capability is not set + capabilities = init_response.worker_init_response.capabilities + self.assertNotIn("WorkerOpenTelemetryEnabled", capabilities) + + @patch.dict(os.environ, {'PYTHON_ENABLE_OPENTELEMETRY': 'false'}) @patch("azure_functions_worker.dispatcher.Dispatcher.initialize_azure_monitor") def test_init_request_otel_capability_disabled_app_setting( self,