Skip to content

Commit

Permalink
str
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen committed Oct 3, 2024
1 parent 8f25e6d commit 2467040
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
3 changes: 2 additions & 1 deletion azure_functions_worker/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions azure_functions_worker/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 27 additions & 0 deletions tests/unittests/test_opentelemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2467040

Please sign in to comment.