Skip to content

Commit 1ef8c76

Browse files
committed
change
1 parent 2467040 commit 1ef8c76

File tree

4 files changed

+48
-31
lines changed

4 files changed

+48
-31
lines changed

azure_functions_worker/constants.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,18 @@
8383

8484
# Appsetting to turn on OpenTelemetry support/features
8585
# A value of "true" enables the setting, defaults to "false"
86-
# Includes turning on Azure monitor distro to send telemetry to AppInsights
8786
PYTHON_ENABLE_OPENTELEMETRY = "PYTHON_ENABLE_OPENTELEMETRY"
8887
PYTHON_ENABLE_OPENTELEMETRY_DEFAULT = "false"
8988

89+
# Appsetting to turn on ApplicationInsights support/features
90+
# A value of "true" enables the setting, defaults to "false"
91+
PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY = "PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY"
92+
PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY_DEFAULT = "false"
93+
9094
# Appsetting to specify root logger name of logger to collect telemetry for
91-
# Used by Azure monitor distro
92-
PYTHON_AZURE_MONITOR_LOGGER_NAME = "PYTHON_AZURE_MONITOR_LOGGER_NAME"
93-
PYTHON_AZURE_MONITOR_LOGGER_NAME_DEFAULT = ""
95+
# Used by Azure monitor distro (Application Insights)
96+
PYTHON_APPLICATIONINSIGHTS_LOGGER_NAME = "PYTHON_APPLICATIONINSIGHTS_LOGGER_NAME"
97+
PYTHON_APPLICATIONINSIGHTS_LOGGER_NAME_DEFAULT = ""
9498

9599
# Appsetting to specify AppInsights connection string
96100
APPLICATIONINSIGHTS_CONNECTION_STRING = "APPLICATIONINSIGHTS_CONNECTION_STRING"

azure_functions_worker/dispatcher.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
APPLICATIONINSIGHTS_CONNECTION_STRING,
2727
HTTP_URI,
2828
METADATA_PROPERTIES_WORKER_INDEXED,
29-
PYTHON_AZURE_MONITOR_LOGGER_NAME,
30-
PYTHON_AZURE_MONITOR_LOGGER_NAME_DEFAULT,
29+
PYTHON_APPLICATIONINSIGHTS_LOGGER_NAME,
30+
PYTHON_APPLICATIONINSIGHTS_LOGGER_NAME_DEFAULT,
3131
PYTHON_ENABLE_DEBUG_LOGGING,
3232
PYTHON_ENABLE_INIT_INDEXING,
33-
PYTHON_ENABLE_OPENTELEMETRY,
34-
PYTHON_ENABLE_OPENTELEMETRY_DEFAULT,
33+
PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY,
34+
PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY_DEFAULT,
3535
PYTHON_LANGUAGE_RUNTIME,
3636
PYTHON_ROLLBACK_CWD_PATH,
3737
PYTHON_SCRIPT_FILE_NAME,
@@ -103,8 +103,10 @@ def __init__(self, loop: BaseEventLoop, host: str, port: int,
103103
self._function_metadata_result = None
104104
self._function_metadata_exception = None
105105

106-
# Used for checking if open telemetry is enabled
106+
# Used for checking if appinsights is enabled
107107
self._azure_monitor_available = False
108+
# Used for checking if open telemetry is enabled
109+
self._otel_libs_available = False
108110
self._context_api = None
109111
self._trace_context_propagator = None
110112

@@ -318,8 +320,8 @@ def initialize_azure_monitor(self):
318320
setting=APPLICATIONINSIGHTS_CONNECTION_STRING
319321
),
320322
logger_name=get_app_setting(
321-
setting=PYTHON_AZURE_MONITOR_LOGGER_NAME,
322-
default_value=PYTHON_AZURE_MONITOR_LOGGER_NAME_DEFAULT
323+
setting=PYTHON_APPLICATIONINSIGHTS_LOGGER_NAME,
324+
default_value=PYTHON_APPLICATIONINSIGHTS_LOGGER_NAME_DEFAULT
323325
),
324326
)
325327
self._azure_monitor_available = True
@@ -381,15 +383,16 @@ async def _handle__worker_init_request(self, request):
381383
constants.RPC_HTTP_TRIGGER_METADATA_REMOVED: _TRUE,
382384
constants.SHARED_MEMORY_DATA_TRANSFER: _TRUE,
383385
}
386+
384387
opentelemetry_app_setting = get_app_setting(
385-
setting=PYTHON_ENABLE_OPENTELEMETRY,
386-
default_value=PYTHON_ENABLE_OPENTELEMETRY_DEFAULT,
388+
setting=PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY,
389+
default_value=PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY_DEFAULT,
387390
)
388391
if opentelemetry_app_setting and opentelemetry_app_setting.lower() == "true":
389392
self.initialize_azure_monitor()
390393

391-
if self._azure_monitor_available:
392-
capabilities[constants.WORKER_OPEN_TELEMETRY_ENABLED] = _TRUE
394+
if self._azure_monitor_available or self._otel_libs_available:
395+
capabilities[constants.WORKER_OPEN_TELEMETRY_ENABLED] = _TRUE
393396

394397
if DependencyManager.should_load_cx_dependencies():
395398
DependencyManager.prioritize_customer_dependencies()
@@ -665,7 +668,7 @@ async def _handle__invocation_request(self, request):
665668
args[name] = bindings.Out()
666669

667670
if fi.is_async:
668-
if self._azure_monitor_available:
671+
if self._azure_monitor_available or self._otel_libs_available:
669672
self.configure_opentelemetry(fi_context)
670673

671674
call_result = \
@@ -783,13 +786,13 @@ async def _handle__function_environment_reload_request(self, request):
783786

784787
capabilities = {}
785788
if get_app_setting(
786-
setting=PYTHON_ENABLE_OPENTELEMETRY,
787-
default_value=PYTHON_ENABLE_OPENTELEMETRY_DEFAULT):
789+
setting=PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY,
790+
default_value=PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY_DEFAULT):
788791
self.initialize_azure_monitor()
789792

790-
if self._azure_monitor_available:
791-
capabilities[constants.WORKER_OPEN_TELEMETRY_ENABLED] = (
792-
_TRUE)
793+
if self._azure_monitor_available or self._otel_libs_available:
794+
capabilities[constants.WORKER_OPEN_TELEMETRY_ENABLED] = (
795+
_TRUE)
793796

794797
if is_envvar_true(PYTHON_ENABLE_INIT_INDEXING):
795798
try:
@@ -999,7 +1002,7 @@ def _run_sync_func(self, invocation_id, context, func, params):
9991002
# invocation_id from ThreadPoolExecutor's threads.
10001003
context.thread_local_storage.invocation_id = invocation_id
10011004
try:
1002-
if self._azure_monitor_available:
1005+
if self._azure_monitor_available or self._otel_libs_available:
10031006
self.configure_opentelemetry(context)
10041007
return ExtensionManager.get_sync_invocation_wrapper(context,
10051008
func)(params)

azure_functions_worker/utils/app_setting_manager.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
FUNCTIONS_WORKER_SHARED_MEMORY_DATA_TRANSFER_ENABLED,
88
PYTHON_ENABLE_DEBUG_LOGGING,
99
PYTHON_ENABLE_INIT_INDEXING,
10+
PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY,
1011
PYTHON_ENABLE_OPENTELEMETRY,
1112
PYTHON_ENABLE_WORKER_EXTENSIONS,
1213
PYTHON_ENABLE_WORKER_EXTENSIONS_DEFAULT,
@@ -29,7 +30,8 @@ def get_python_appsetting_state():
2930
FUNCTIONS_WORKER_SHARED_MEMORY_DATA_TRANSFER_ENABLED,
3031
PYTHON_SCRIPT_FILE_NAME,
3132
PYTHON_ENABLE_INIT_INDEXING,
32-
PYTHON_ENABLE_OPENTELEMETRY]
33+
PYTHON_ENABLE_OPENTELEMETRY,
34+
PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY]
3335

3436
app_setting_states = "".join(
3537
f"{app_setting}: {current_vars[app_setting]} | "

tests/unittests/test_opentelemetry.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ def test_update_opentelemetry_status_import_error(self):
2323
# Patch the built-in import mechanism
2424
with patch('builtins.__import__', side_effect=ImportError):
2525
self.dispatcher.update_opentelemetry_status()
26-
# Verify that otel_libs_available is set to False due to ImportError
27-
self.assertFalse(self.dispatcher._azure_monitor_available)
26+
# Verify that context variables are None due to ImportError
27+
self.assertIsNone(self.dispatcher._context_api)
28+
self.assertIsNone(self.dispatcher._trace_context_propagator)
2829

2930
@patch('builtins.__import__')
3031
def test_update_opentelemetry_status_success(
@@ -54,12 +55,12 @@ def test_initialize_azure_monitor_import_error(
5455
with patch('builtins.__import__', side_effect=ImportError):
5556
self.dispatcher.initialize_azure_monitor()
5657
mock_update_ot.assert_called_once()
57-
# Verify that otel_libs_available is set to False due to ImportError
58+
# Verify that azure_monitor_available is set to False due to ImportError
5859
self.assertFalse(self.dispatcher._azure_monitor_available)
5960

60-
@patch.dict(os.environ, {'PYTHON_ENABLE_OPENTELEMETRY': 'true'})
61+
@patch.dict(os.environ, {'PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY': 'true'})
6162
@patch('builtins.__import__')
62-
def test_init_request_otel_capability_enabled_app_setting(
63+
def test_init_request_initialize_azure_monitor_enabled_app_setting(
6364
self,
6465
mock_imports,
6566
):
@@ -78,13 +79,15 @@ def test_init_request_otel_capability_enabled_app_setting(
7879
self.assertEqual(init_response.worker_init_response.result.status,
7980
protos.StatusResult.Success)
8081

82+
# Verify azure_monitor_available is set to True
83+
self.assertTrue(self.dispatcher._azure_monitor_available)
8184
# Verify that WorkerOpenTelemetryEnabled capability is set to _TRUE
8285
capabilities = init_response.worker_init_response.capabilities
8386
self.assertIn("WorkerOpenTelemetryEnabled", capabilities)
8487
self.assertEqual(capabilities["WorkerOpenTelemetryEnabled"], "true")
8588

8689
@patch("azure_functions_worker.dispatcher.Dispatcher.initialize_azure_monitor")
87-
def test_init_request_otel_capability_default_app_setting(
90+
def test_init_request_initialize_azure_monitor_default_app_setting(
8891
self,
8992
mock_initialize_azmon,
9093
):
@@ -103,15 +106,18 @@ def test_init_request_otel_capability_default_app_setting(
103106
protos.StatusResult.Success)
104107

105108
# Azure monitor initialized not called
109+
# Since default behavior is not enabled
106110
mock_initialize_azmon.assert_not_called()
107111

112+
# Verify azure_monitor_available is set to False
113+
self.assertFalse(self.dispatcher._azure_monitor_available)
108114
# Verify that WorkerOpenTelemetryEnabled capability is not set
109115
capabilities = init_response.worker_init_response.capabilities
110116
self.assertNotIn("WorkerOpenTelemetryEnabled", capabilities)
111117

112-
@patch.dict(os.environ, {'PYTHON_ENABLE_OPENTELEMETRY': 'false'})
118+
@patch.dict(os.environ, {'PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY': 'false'})
113119
@patch("azure_functions_worker.dispatcher.Dispatcher.initialize_azure_monitor")
114-
def test_init_request_otel_capability_disabled_app_setting(
120+
def test_init_request_initialize_azure_monitor_disabled_app_setting(
115121
self,
116122
mock_initialize_azmon,
117123
):
@@ -132,6 +138,8 @@ def test_init_request_otel_capability_disabled_app_setting(
132138
# Azure monitor initialized not called
133139
mock_initialize_azmon.assert_not_called()
134140

141+
# Verify azure_monitor_available is set to False
142+
self.assertFalse(self.dispatcher._azure_monitor_available)
135143
# Verify that WorkerOpenTelemetryEnabled capability is not set
136144
capabilities = init_response.worker_init_response.capabilities
137145
self.assertNotIn("WorkerOpenTelemetryEnabled", capabilities)

0 commit comments

Comments
 (0)