可观测场景如何使用 Vector Remap 优化日志数据的解析与存储

在现代可观测性领域,采集器收集到的原始数据往往需要裁剪或重组,才能形成我们关注的关键数据,并根据内容分发到不同的存储系统。这样的处理不仅让数据更加结构化,便于查询,还能减少无关数据对存储资源的占用。

为了解决这一需求,Vector 提供了 Transforms 功能,并配套了功能强大的 VRLVector Remap Languag),允许用户自定义数据处理逻辑,以灵活应对各种数据处理场景。

VRL 是一种面向表达式的语言,旨在以安全、高效的方式转换可观测性数据(Logs 和 Metrics)。它具有简单的语法和丰富的内置函数集,专为可观测性用例量身定制。

在上周发布的 《实战 Vector:开源日志和指标采集工具》一文中,我们已经介绍了 Vector 中的各个基本概念,并且初步的使用了简单的 Transform 来处理了 nginx 日志。Vector Transform 提供了 aggregate,dedupe,filter,remap 等方法。本章我们详细介绍 Transform 中的 remap 功能。


(图 1 :Remap 在整个 Pipeline 中的位置和作用)

TL;DR

Remap 是通过编写 VRL 语言来定制 Transform 的逻辑,首先我们看一个简单的例子:

[sources.demo_source]
type = "demo_logs"
format = "apache_common"
lines = [ "line1" ]

[transforms.transform_apache_common_log]
type = "remap"
inputs = [ "demo_source" ]
drop_on_error = true
drop_on_abort = true
reroute_dropped = true
source = """
log = parse_apache_log!(.message,format: "common")
if to_int(log.status) > 300 {
  abort
}
. = log
.mark = "transform_apache_common_log"
"""

[sinks.transform_apache_common_log_sink]
type = "console"
inputs = [ "transform_apache_common_log" ]
encoding.codec = "json"

[sinks.demo_source_sink]
type = "console"
inputs = [ "demo_source" ]
encoding.codec = "json"

[transforms.transform_apache_common_log_dropped]
type = "remap"
inputs = [ "transform_apache_common_log.dropped" ]
source = """
.mark = "dropped"
"""

[sinks.dropped_msg_sink]
type = "console"
inputs = [ "transform_apache_common_log_dropped" ]
encoding.codec = "json"

然后使用此配置文件启动 Vector ,可以看到在标准输出里出现了三种类型的 JSON,我们会结合这三种类型的的 JSON 来描述 VRL 代码的逻辑。

VRL 基本介绍

通过上述的例子我们对 transform 有了一个简单直观的认识,在上述配置中的 16 行到 19 行,我们主要定义了名为 transform_apache_common_log 的 Transform。

以下我们详细描述一下这个 Transform 到底做了什么。Transform 的定义的基本格式这里不再赘述,我们重点介绍 source 的字段中 VRL 的内容。

上述例子中 source 中的 VRL 可以分为三个部分:

1. 解析

log = parse_apache_log!(.message, format: "common")

同大部分编程语言的赋值语句类型。这一句是把等号右边表达式产生的结果赋值给 log 变量。等号右边整体的意思是按照 Apach Common 的格式解析原 event 中的 message 字段转化为更结构化的键值对结构。

我们再细化一点。这里是调用了一个内置函数 parse_apache_log 他接收两个参数:

  • .message

  • format: "common"

这里的 .message 是对当前 event 取 message 字段的操作。当前 event 的格式如下:

{
   
    "host":
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值