Airflow2 日志使用vector写入Elastic Search

文章目录

组件

  1. ElasticSearch - 7.15.2
  2. vector - 0.17.3
  3. Airflow - 2.2.1

步骤

  1. 启动elasticsearch跟kibana(可选,但是启动起来能看es里面的数据,方便调试)

  2. 先写好vector解析toml,新版本的vector可以使用Vector Remap Language来添加字段、删除字段,还有一些内置函数,变量等,挺好用的。

[sources.airflow_log]
    type = "file"
    ignore_older_secs = 86400
    include = [ "/home/greetlist/airflow/logs/*/*/*/*.log" ]
    read_from = "beginning"
    data_dir = "."

[transforms.transform_get_unique_id]
    type = "remap"
    inputs = [ "airflow_log" ]
    source = """
        . |= parse_regex!(.file, r'/home/greetlist/airflow/logs/(?P<dag_id>.*)/(?P<task_id>.*)/(?P<run_id>.*)/(?P<try_number>.*).log$')
    """

[transforms.transform_remove_file_field]
    type = "remap"
    inputs = [ "transform_get_unique_id" ]
    source = """
        del(.file)
        del(.host)
    """

[transforms.transform_add_log_id_field]
    type = "remap"
    inputs = [ "transform_remove_file_field" ]
    source = """
        .log_id = join!([.dag_id, .task_id, .run_id, .try_number], "-")
        .offset = 1
    """

[sinks.airflow_log_sink]
    type = "console"
    inputs = [ "transform_add_log_id_field" ]
    target = "stdout"
    encoding.codec = "json"

[sinks.to_elasticsearch]
    type = "elasticsearch"
    inputs = [ "transform_add_log_id_field" ]
    endpoint = "http://127.0.0.1:9200"
    index = ".log_id"
    mode = "data_stream"
    #pipeline = "pipeline-name"
    compression = "none"

上面配置文件需要注意几点 :

  • 根据Airflow elasticsearch里面查找日志的唯一键:

{dag_id}-{task_id}-{run_id}-{try_number}

我们需要添加log_id这个字段到json里面

  • offset字段记得加,要不然airflow那边代码会把offset <= 0的记录过滤掉。
  • 删除host这个字段(好像是跟es的host冲突)
  • 把file这种长度太长的字段剔除掉
  • 如果es没有pipline的话,记得把pipline这个注释掉
  • endpoint改成自己的es地址
  1. 启动vector
vector -c vector.toml
  1. 修改elastic search provider代码:
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-APuwbvii-1637648650596)(:/0841b1f7d2804e7ca1fe19ce84adb45d)]

这个就很恶心,vector里面可能读取一行的时候是以\n或者\n\r这种来取的,但是这边的python代码在日志结束的时候不写换行符,就可能会导致vector一直卡在读取行,而不把end_of_log日志结尾标志写进es里面。进而在airflow web那边看日志的时候,就会一直有load组件(转圈圈)在界面上消不掉。

  1. 修改airflow.cfg
[logging]
remote_logging = True

[elasticsearch]
# Elasticsearch host
host = http://localhost:9200

# Format of the log_id, which is used to query for a given tasks logs
log_id_template = {dag_id}-{task_id}-{run_id}-{try_number}

注意:

  • 版本问题,在execution_date还没删除之前,第三个大括号里面是execution_date,后面版本是run_id。(具体哪个版本不太清楚)
  • 记得修改自己的es的地址。
  1. 修改完代码之后就可以重启scheduler跟webserver了。

  2. 完工

  1. 使用kibana挺方便的,可以看每一条写入es的日志长什么样。
  2. offset这个变量也挺坑的,记得写入es的时候加上。
  3. 换行符这个纯恶心人,只能看代码。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值