了解 Vector 的结构

Vector 配置由三种组件组成:

text
1
Sources → Transforms → Sinks
  • Sources:负责接收数据,例如 file

  • Transforms:负责处理数据,例如 remap

  • Sinks:负责发送数据,例如 elasticsearch

一个示例配置文件结构

yaml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
sources:
  app_logs:
    type: file
    include:
      - /var/log/app/*.log

transforms:
  parse_app_logs:
    type: remap
    inputs:
      - app_logs
    source: |
      .service = "payment"      

sinks:
  elasticsearch:
    type: elasticsearch
    inputs:
      - parse_app_logs

总结:Transforms 是在数据流经 Vector 拓扑时,对数据进行塑形和处理。

Transform 配置的 Schema

transform 具有下面的结构:

yaml
1
2
3
4
5
transforms: 
  <transform_id>:
    type: <transform_type> # transform 类型
    inputs: # transform 的上游组件
      - <upstream_component_id>

例如

yaml
1
2
3
4
5
6
7
transforms:
  parse_app_logs:
    type: remap
    inputs:
      - app_logs
    source: | # 要执行的 VRL 程序
      .service = "payment"

Transform 的分类

  • 字段解析与修改:remap, lua,常见用途:
    • 解析 JSON
    • 解析 Grok
    • 解析正则
    • 增加字段/删除字段/重命名字段
    • 修改字段类型
    • 处理时间
    • 规范字段名称
  • 数据筛选:filter; sample; dedupe; throttle
    • filter:根据条件保留或丢弃事件
    • sample:按比例采样
    • dedupe:删除重复事件
    • throttle:限制事件通过速率
  • 数据分流:route;exclusive_route
    • route:一条事件可以进入多个匹配分支
    • exclusive_route:一条事件只能进入第一个匹配分支
  • 多事件聚合:reduce;aggregate;window
    • reduce:数据类型为“Log”,将多条日志合并成一条
    • aggregate:数据类型为“Metrics”,将多个指标事件聚合
    • window:数据类型为“多种事件”,按窗口处理事件
  • 数据类型转换:log_to_metric;metric_to_log; trace_to_log;incremental_to_absolute
    • 错误日志 → Prometheus
    • Counter Trace → Log
    • 增量指标 → 绝对值指标
  • 元数据增强:aws_ec2_metadata;在事件中增加 AWS EC2 实例相关元数据。

VRL 的定义

VRL (Vector Remap Language) 是 Vector 用于处理可观测性事件的专用语言。通俗来讲就是 Transform 中使用的处理语言。

基础示例

给当前事件增加 service 字段。

text
1
.service = "payment"

下列内容是错误的

text
1
service = "payment"

这不是给事件增加字段。它创建的是临时变量 service。程序执行结束后,临时变量不会自动作为事件字段发送到下游。

边界示例

这会把整个当前事件替换成字符串。这不再是增加字段,它是替换根值。

text
1
. = "hello"

Reference

[1] Cluster state

[2] Shard allocation, relocation, and recovery

[3] Voting configurations

[4] Node roles

[5] Red or yellow cluster health status

[6] How to route docs of same _routing key in Elasticsearch into multiple shard?

[7] Master-eligible node

[8] Voting-only master-eligible node

[9] Coordinating only node