Skip to content

Commit 2921117

Browse files
committed
otel
1 parent 1ef8c76 commit 2921117

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

azure_functions_worker/dispatcher.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
PYTHON_ENABLE_INIT_INDEXING,
3333
PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY,
3434
PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY_DEFAULT,
35+
PYTHON_ENABLE_OPENTELEMETRY,
36+
PYTHON_ENABLE_OPENTELEMETRY_DEFAULT,
3537
PYTHON_LANGUAGE_RUNTIME,
3638
PYTHON_ROLLBACK_CWD_PATH,
3739
PYTHON_SCRIPT_FILE_NAME,
@@ -385,11 +387,19 @@ async def _handle__worker_init_request(self, request):
385387
}
386388

387389
opentelemetry_app_setting = get_app_setting(
390+
setting=PYTHON_ENABLE_OPENTELEMETRY,
391+
default_value=PYTHON_ENABLE_OPENTELEMETRY_DEFAULT,
392+
)
393+
394+
appinsights_app_setting = get_app_setting(
388395
setting=PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY,
389396
default_value=PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY_DEFAULT,
390397
)
391-
if opentelemetry_app_setting and opentelemetry_app_setting.lower() == "true":
398+
if appinsights_app_setting and appinsights_app_setting.lower() == "true":
392399
self.initialize_azure_monitor()
400+
401+
if opentelemetry_app_setting and opentelemetry_app_setting.lower() == "true":
402+
self._otel_libs_available = True
393403

394404
if self._azure_monitor_available or self._otel_libs_available:
395405
capabilities[constants.WORKER_OPEN_TELEMETRY_ENABLED] = _TRUE
@@ -785,6 +795,10 @@ async def _handle__function_environment_reload_request(self, request):
785795
bindings.load_binding_registry()
786796

787797
capabilities = {}
798+
if get_app_setting(
799+
setting=PYTHON_ENABLE_OPENTELEMETRY,
800+
default_value=PYTHON_ENABLE_OPENTELEMETRY_DEFAULT):
801+
self._otel_libs_available = True
788802
if get_app_setting(
789803
setting=PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY,
790804
default_value=PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY_DEFAULT):

tests/unittests/test_opentelemetry.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,76 @@ def test_init_request_initialize_azure_monitor_disabled_app_setting(
143143
# Verify that WorkerOpenTelemetryEnabled capability is not set
144144
capabilities = init_response.worker_init_response.capabilities
145145
self.assertNotIn("WorkerOpenTelemetryEnabled", capabilities)
146+
147+
@patch.dict(os.environ, {'PYTHON_ENABLE_OPENTELEMETRY': 'true'})
148+
def test_init_request_enable_opentelemetry_enabled_app_setting(
149+
self,
150+
):
151+
152+
init_request = protos.StreamingMessage(
153+
worker_init_request=protos.WorkerInitRequest(
154+
host_version="2.3.4",
155+
function_app_directory=str(FUNCTION_APP_DIRECTORY)
156+
)
157+
)
158+
159+
init_response = self.loop.run_until_complete(
160+
self.dispatcher._handle__worker_init_request(init_request))
161+
162+
self.assertEqual(init_response.worker_init_response.result.status,
163+
protos.StatusResult.Success)
164+
165+
# Verify otel_libs_available is set to True
166+
self.assertTrue(self.dispatcher._otel_libs_available)
167+
# Verify that WorkerOpenTelemetryEnabled capability is set to _TRUE
168+
capabilities = init_response.worker_init_response.capabilities
169+
self.assertIn("WorkerOpenTelemetryEnabled", capabilities)
170+
self.assertEqual(capabilities["WorkerOpenTelemetryEnabled"], "true")
171+
172+
@patch.dict(os.environ, {})
173+
def test_init_request_enable_opentelemetry_default_app_setting(
174+
self,
175+
):
176+
177+
init_request = protos.StreamingMessage(
178+
worker_init_request=protos.WorkerInitRequest(
179+
host_version="2.3.4",
180+
function_app_directory=str(FUNCTION_APP_DIRECTORY)
181+
)
182+
)
183+
184+
init_response = self.loop.run_until_complete(
185+
self.dispatcher._handle__worker_init_request(init_request))
186+
187+
self.assertEqual(init_response.worker_init_response.result.status,
188+
protos.StatusResult.Success)
189+
190+
# Verify otel_libs_available is set to False by default
191+
self.assertFalse(self.dispatcher._otel_libs_available)
192+
# Verify that WorkerOpenTelemetryEnabled capability is not set
193+
capabilities = init_response.worker_init_response.capabilities
194+
self.assertNotIn("WorkerOpenTelemetryEnabled", capabilities)
195+
196+
@patch.dict(os.environ, {'PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY': 'false'})
197+
def test_init_request_initialize_azure_monitor_disabled_app_setting(
198+
self,
199+
):
200+
201+
init_request = protos.StreamingMessage(
202+
worker_init_request=protos.WorkerInitRequest(
203+
host_version="2.3.4",
204+
function_app_directory=str(FUNCTION_APP_DIRECTORY)
205+
)
206+
)
207+
208+
init_response = self.loop.run_until_complete(
209+
self.dispatcher._handle__worker_init_request(init_request))
210+
211+
self.assertEqual(init_response.worker_init_response.result.status,
212+
protos.StatusResult.Success)
213+
214+
# Verify otel_libs_available is set to False by default
215+
self.assertFalse(self.dispatcher._otel_libs_available)
216+
# Verify that WorkerOpenTelemetryEnabled capability is not set
217+
capabilities = init_response.worker_init_response.capabilities
218+
self.assertNotIn("WorkerOpenTelemetryEnabled", capabilities)

0 commit comments

Comments
 (0)