Vector 日志管道¶
一句话概述:Vector 是 Datadog 开源的高性能可观测性数据管道,用 Rust 编写,比 Fluentd/Logstash 快 10 倍、内存占用低 4 倍,统一处理日志、指标、追踪数据。
核心知识点¶
| 概念 | 白话解释 |
|---|---|
| Source | 源 = 数据从哪里来(文件、Kafka、HTTP) |
| Transform | 转换 = 对数据做处理(过滤、解析、映射) |
| Sink | 目标 = 数据发到哪里去(ES、S3、ClickHouse) |
| VRL | Vector Remap Language = Vector 的数据转换语言 |
安装配置¶
# 安装
curl --proto '=https' --tlsv1.2 -sSfL https://sh.vector.dev | bash
# Docker
docker run -d --name vector -v $(pwd)/vector.yaml:/etc/vector/vector.yaml timberio/vector:latest
基本使用¶
# vector.yaml
sources:
app_logs:
type: file # 读取文件
include: ["/var/log/app/*.log"]
transforms:
parse_json:
type: remap # VRL 转换
inputs: ["app_logs"]
source: |
. = parse_json!(.message) # 解析 JSON
.timestamp = now() # 添加时间戳
del(.password) # 删除敏感字段
filter_errors:
type: filter # 过滤
inputs: ["parse_json"]
condition: '.level == "error"' # 只保留错误日志
sinks:
elasticsearch:
type: elasticsearch # 输出到 ES
inputs: ["parse_json"]
endpoints: ["http://localhost:9200"]
error_alerts:
type: http # 错误发送 HTTP 告警
inputs: ["filter_errors"]
uri: "https://hooks.slack.com/services/xxx"
encoding: { codec: "json" }
速查表¶
# VRL 常用函数
parse_json!(.message) # 解析 JSON
to_string(.field) # 转字符串
del(.field) # 删除字段
.new_field = "value" # 添加字段
contains(.msg, "error") # 字符串包含
# Vector vs Fluentd: Vector 性能更好,VRL 更强大;Fluentd 插件生态更大
参考:Vector 文档 | 更新于 2026 年