diff --git a/azure_functions_worker/dispatcher.py b/azure_functions_worker/dispatcher.py index 84bf79f3..3692a206 100644 --- a/azure_functions_worker/dispatcher.py +++ b/azure_functions_worker/dispatcher.py @@ -32,6 +32,8 @@ PYTHON_ENABLE_INIT_INDEXING, PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY, PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY_DEFAULT, + PYTHON_ENABLE_OPENTELEMETRY, + PYTHON_ENABLE_OPENTELEMETRY_DEFAULT, PYTHON_LANGUAGE_RUNTIME, PYTHON_ROLLBACK_CWD_PATH, PYTHON_SCRIPT_FILE_NAME, @@ -385,11 +387,19 @@ async def _handle__worker_init_request(self, request): } opentelemetry_app_setting = get_app_setting( + setting=PYTHON_ENABLE_OPENTELEMETRY, + default_value=PYTHON_ENABLE_OPENTELEMETRY_DEFAULT, + ) + + appinsights_app_setting = get_app_setting( setting=PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY, default_value=PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY_DEFAULT, ) - if opentelemetry_app_setting and opentelemetry_app_setting.lower() == "true": + if appinsights_app_setting and appinsights_app_setting.lower() == "true": self.initialize_azure_monitor() + + if opentelemetry_app_setting and opentelemetry_app_setting.lower() == "true": + self._otel_libs_available = True if self._azure_monitor_available or self._otel_libs_available: capabilities[constants.WORKER_OPEN_TELEMETRY_ENABLED] = _TRUE @@ -785,6 +795,10 @@ async def _handle__function_environment_reload_request(self, request): bindings.load_binding_registry() capabilities = {} + if get_app_setting( + setting=PYTHON_ENABLE_OPENTELEMETRY, + default_value=PYTHON_ENABLE_OPENTELEMETRY_DEFAULT): + self._otel_libs_available = True if get_app_setting( setting=PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY, default_value=PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY_DEFAULT): diff --git a/tests/unittests/test_opentelemetry.py b/tests/unittests/test_opentelemetry.py index 883406ea..b7fd318a 100644 --- a/tests/unittests/test_opentelemetry.py +++ b/tests/unittests/test_opentelemetry.py @@ -143,3 +143,76 @@ def test_init_request_initialize_azure_monitor_disabled_app_setting( # 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': 'true'}) + def test_init_request_enable_opentelemetry_enabled_app_setting( + self, + ): + + 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) + + # Verify otel_libs_available is set to True + self.assertTrue(self.dispatcher._otel_libs_available) + # Verify that WorkerOpenTelemetryEnabled capability is set to _TRUE + capabilities = init_response.worker_init_response.capabilities + self.assertIn("WorkerOpenTelemetryEnabled", capabilities) + self.assertEqual(capabilities["WorkerOpenTelemetryEnabled"], "true") + + @patch.dict(os.environ, {}) + def test_init_request_enable_opentelemetry_default_app_setting( + self, + ): + + 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) + + # Verify otel_libs_available is set to False by default + self.assertFalse(self.dispatcher._otel_libs_available) + # Verify that WorkerOpenTelemetryEnabled capability is not set + capabilities = init_response.worker_init_response.capabilities + self.assertNotIn("WorkerOpenTelemetryEnabled", capabilities) + + @patch.dict(os.environ, {'PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY': 'false'}) + def test_init_request_initialize_azure_monitor_disabled_app_setting( + self, + ): + + 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) + + # Verify otel_libs_available is set to False by default + self.assertFalse(self.dispatcher._otel_libs_available) + # Verify that WorkerOpenTelemetryEnabled capability is not set + capabilities = init_response.worker_init_response.capabilities + self.assertNotIn("WorkerOpenTelemetryEnabled", capabilities)