Skip to content

Commit

Permalink
otel
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen committed Dec 12, 2024
1 parent 1ef8c76 commit 2921117
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
16 changes: 15 additions & 1 deletion azure_functions_worker/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
73 changes: 73 additions & 0 deletions tests/unittests/test_opentelemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 2921117

Please sign in to comment.