You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Airflow UI中DAG运行面板备注的使用及程序化填充方法咨询

Airflow DAG运行备注功能使用与程序化填充方法

一、UI手动操作备注

  • 进入Airflow UI的DAG运行面板(DAG Runs),找到目标DAG运行记录
  • 点击记录右侧的铅笔图标(Edit Note),在弹出框输入备注内容后保存即可

二、程序化填充/修改备注

以下是几种常用的代码实现方式,可根据传入的DAG运行参数生成或更新备注:

1. 在任务中通过PythonOperator更新备注

利用Airflow的DagRun模型和上下文参数,在任务内动态生成并更新备注:

from airflow.models import DagRun
from airflow.operators.python import PythonOperator
from airflow.utils.session import create_session

def update_dag_run_note(**kwargs):
    dag_run = kwargs["dag_run"]
    # 从DAG运行参数中提取自定义字段
    custom_param = dag_run.conf.get("custom_param", "未传入参数")
    # 拼接生成备注内容
    note_content = f"本次运行参数:custom_param={custom_param} | 执行时间:{dag_run.execution_date}"
    
    with create_session() as session:
        dag_run.note = note_content
        session.merge(dag_run)
        session.commit()

# 将该任务加入DAG
update_note_task = PythonOperator(
    task_id="update_dag_run_note",
    python_callable=update_dag_run_note,
    provide_context=True,
    dag=your_dag_object
)

2. 触发DAG时直接设置初始备注

使用TriggerDagRunOperator触发目标DAG时,可直接指定初始备注内容:

from airflow.operators.trigger_dagrun import TriggerDagRunOperator

trigger_target_dag = TriggerDagRunOperator(
    task_id="trigger_target_dag",
    trigger_dag_id="your_target_dag_id",
    conf={"custom_param": "test_value_001"},
    # 直接传入初始备注
    note=f"由当前DAG触发,运行参数custom_param=test_value_001",
    dag=your_dag_object
)

3. 通过Airflow REST API更新备注

若需要外部系统调用更新,可使用Airflow的REST API(需确保API权限已开启):

# 示例PUT请求:更新指定DAG Run的备注
curl -X PUT \
  http://your-airflow-domain/api/v1/dags/<target_dag_id>/dagRuns/<target_dag_run_id> \
  -H "Authorization: Bearer <your_auth_token>" \
  -H "Content-Type: application/json" \
  -d '{"note": "外部系统更新:参数xxx=yyy,执行状态已确认"}'

注意事项

  • 操作DagRun的任务需拥有Airflow的编辑权限,避免权限不足报错
  • Airflow 2.x版本中,PythonOperator的provide_context参数可替换为通过kwargs直接获取上下文
  • 从dag_run.conf提取参数时,建议添加默认值,避免因参数缺失引发KeyError

内容的提问来源于stack exchange,提问作者Justin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 02:01:13