Skip to content

Commit

Permalink
feat: support for event loop debugging and slow tasks detection
Browse files Browse the repository at this point in the history
  • Loading branch information
YunchuWang committed Sep 24, 2024
1 parent 0189c65 commit b7673d5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions azure_functions_worker/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,8 @@

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

# New constant for event loop profiling
PYTHON_ENABLE_EVENT_LOOP_DEBUGGING = 'PYTHON_ENABLE_EVENT_LOOP_DEBUGGING'
PYTHON_DEBUGGING_EVENT_LOOP_SLOW_CALLBACK_THRESHOLD = 'PYTHON_DEBUGGING_EVENT_LOOP_SLOW_CALLBAK_THRESHOLD'
PYTHON_DEBUGGING_EVENT_LOOP_SLOW_CALLBACK_THRESHOLD_DEFAULT = 600 # 10 minutes
20 changes: 19 additions & 1 deletion azure_functions_worker/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
METADATA_PROPERTIES_WORKER_INDEXED,
PYTHON_AZURE_MONITOR_LOGGER_NAME,
PYTHON_AZURE_MONITOR_LOGGER_NAME_DEFAULT,
PYTHON_DEBUGGING_EVENT_LOOP_SLOW_CALLBACK_THRESHOLD,
PYTHON_DEBUGGING_EVENT_LOOP_SLOW_CALLBACK_THRESHOLD_DEFAULT,
PYTHON_ENABLE_DEBUG_LOGGING,
PYTHON_ENABLE_EVENT_LOOP_DEBUGGING,
PYTHON_ENABLE_INIT_INDEXING,
PYTHON_ENABLE_OPENTELEMETRY,
PYTHON_ENABLE_OPENTELEMETRY_DEFAULT,
Expand Down Expand Up @@ -353,6 +356,15 @@ def update_opentelemetry_status(self):
"Cannot import OpenTelemetry libraries."
)


def _setup_event_loop_profiling(self):
if is_envvar_true(PYTHON_ENABLE_EVENT_LOOP_DEBUGGING):
self._loop.set_debug(True)
self._loop.slow_callback_duration = get_app_setting(PYTHON_DEBUGGING_EVENT_LOOP_SLOW_CALLBACK_THRESHOLD,
PYTHON_DEBUGGING_EVENT_LOOP_SLOW_CALLBACK_THRESHOLD_DEFAULT) # 10 minutes in seconds
logger.info("Event loop debug mode enabled with 10-minute slow callback detection")


async def _handle__worker_init_request(self, request):
logger.info('Received WorkerInitRequest, '
'python version %s, '
Expand Down Expand Up @@ -381,6 +393,10 @@ async def _handle__worker_init_request(self, request):
constants.RPC_HTTP_TRIGGER_METADATA_REMOVED: _TRUE,
constants.SHARED_MEMORY_DATA_TRANSFER: _TRUE,
}

# Set up event loop profiling
self._setup_event_loop_profiling()

if get_app_setting(setting=PYTHON_ENABLE_OPENTELEMETRY,
default_value=PYTHON_ENABLE_OPENTELEMETRY_DEFAULT):
self.initialize_azure_monitor()
Expand Down Expand Up @@ -760,7 +776,9 @@ async def _handle__function_environment_reload_request(self, request):
env_vars = func_env_reload_request.environment_variables
for var in env_vars:
os.environ[var] = env_vars[var]

# Enable event loop debug mode
self._setup_event_loop_profiling()

# Apply PYTHON_THREADPOOL_THREAD_COUNT
self._stop_sync_call_tp()
self._sync_call_tp = (
Expand Down

0 comments on commit b7673d5

Please sign in to comment.