Skip to content

feat: support for event loop debugging and slow tasks detection #1580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions azure_functions_worker/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,9 @@

# 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
19 changes: 19 additions & 0 deletions 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,16 @@ 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)
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 +394,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,6 +777,8 @@ 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()
Expand Down
Loading