You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by po...@apache.org on 2022/03/01 11:30:06 UTC

[airflow] branch main updated: add celery.task_timeout_error metric (#21602)

This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 9e67692  add celery.task_timeout_error metric (#21602)
9e67692 is described below

commit 9e6769206e124b65d31028a3b7b9047d51fd0be5
Author: Ping Zhang <pi...@umich.edu>
AuthorDate: Tue Mar 1 03:29:09 2022 -0800

    add celery.task_timeout_error metric (#21602)
    
    * log celery task id to correlate logs
    
    * add celery.task_timeout_error metric
---
 airflow/executors/celery_executor.py               | 24 +++++++++++++---------
 docs/apache-airflow/logging-monitoring/metrics.rst |  1 +
 2 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/airflow/executors/celery_executor.py b/airflow/executors/celery_executor.py
index 34488a8..04983c4 100644
--- a/airflow/executors/celery_executor.py
+++ b/airflow/executors/celery_executor.py
@@ -82,14 +82,17 @@ app = Celery(conf.get('celery', 'CELERY_APP_NAME'), config_source=celery_configu
 def execute_command(command_to_exec: CommandType) -> None:
     """Executes command."""
     BaseExecutor.validate_command(command_to_exec)
-    log.info("Executing command in Celery: %s", command_to_exec)
     celery_task_id = app.current_task.request.id
-    log.info(f"Celery task ID: {celery_task_id}")
+    log.info("[%s] Executing command in Celery: %s", celery_task_id, command_to_exec)
 
-    if settings.EXECUTE_TASKS_NEW_PYTHON_INTERPRETER:
-        _execute_in_subprocess(command_to_exec, celery_task_id)
-    else:
-        _execute_in_fork(command_to_exec, celery_task_id)
+    try:
+        if settings.EXECUTE_TASKS_NEW_PYTHON_INTERPRETER:
+            _execute_in_subprocess(command_to_exec, celery_task_id)
+        else:
+            _execute_in_fork(command_to_exec, celery_task_id)
+    except Exception:
+        Stats.incr("celery.execute_command.failure")
+        raise
 
 
 def _execute_in_fork(command_to_exec: CommandType, celery_task_id: Optional[str] = None) -> None:
@@ -100,7 +103,8 @@ def _execute_in_fork(command_to_exec: CommandType, celery_task_id: Optional[str]
         if ret == 0:
             return
 
-        raise AirflowException('Celery command failed on host: ' + get_hostname())
+        msg = f'Celery command failed on host: {get_hostname()} with celery_task_id {celery_task_id}'
+        raise AirflowException(msg)
 
     from airflow.sentry import Sentry
 
@@ -123,7 +127,7 @@ def _execute_in_fork(command_to_exec: CommandType, celery_task_id: Optional[str]
         args.func(args)
         ret = 0
     except Exception as e:
-        log.exception("Failed to execute task %s.", str(e))
+        log.exception("[%s] Failed to execute task %s.", celery_task_id, str(e))
         ret = 1
     finally:
         Sentry.flush()
@@ -138,9 +142,9 @@ def _execute_in_subprocess(command_to_exec: CommandType, celery_task_id: Optiona
     try:
         subprocess.check_output(command_to_exec, stderr=subprocess.STDOUT, close_fds=True, env=env)
     except subprocess.CalledProcessError as e:
-        log.exception('execute_command encountered a CalledProcessError')
+        log.exception('[%s] execute_command encountered a CalledProcessError', celery_task_id)
         log.error(e.output)
-        msg = 'Celery command failed on host: ' + get_hostname()
+        msg = f'Celery command failed on host: {get_hostname()} with celery_task_id {celery_task_id}'
         raise AirflowException(msg)
 
 
diff --git a/docs/apache-airflow/logging-monitoring/metrics.rst b/docs/apache-airflow/logging-monitoring/metrics.rst
index 011ac5a..2258ac7 100644
--- a/docs/apache-airflow/logging-monitoring/metrics.rst
+++ b/docs/apache-airflow/logging-monitoring/metrics.rst
@@ -107,6 +107,7 @@ Name                                        Description
 ``dag.callback_exceptions``                 Number of exceptions raised from DAG callbacks. When this happens, it
                                             means DAG callback is not working.
 ``celery.task_timeout_error``               Number of ``AirflowTaskTimeout`` errors raised when publishing Task to Celery Broker.
+``celery.execute_command.failure``          Number of non-zero exit code from Celery task.
 ``task_removed_from_dag.<dag_id>``          Number of tasks removed for a given dag (i.e. task no longer exists in DAG)
 ``task_restored_to_dag.<dag_id>``           Number of tasks restored for a given dag (i.e. task instance which was
                                             previously in REMOVED state in the DB is added to DAG file)