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

Vector采集主机指标写入ClickHouse失败,如何配置Transforms?

Vector host_metrics 写入ClickHouse的VRL脚本配置方案

前置检查

  • 确认ClickHouse目标表结构与处理后字段完全匹配,包括列名、数据类型、大小写
  • 验证Vector的ClickHouse输出端配置:确保database、table、auth信息正确,format推荐设为JSONEachRow

通用VRL脚本示例

假设你的ClickHouse表结构如下:

CREATE TABLE host_metrics (
    timestamp DateTime64(3),
    metric_name String,
    value Float64,
    cpu_core String,
    disk_device String,
    disk_mountpoint String,
    network_interface String,
    host String
) ENGINE = MergeTree()
ORDER BY (timestamp, metric_name, host);

对应的remap类型Transforms配置:

[transforms.normalize_host_metrics]
type = "remap"
inputs = ["host_metrics_source"]
source = """
# 提取指标名称
.metric_name = .metric.name

# 适配不同类型指标的数值提取
.value = if exists(.metric.counter) {
    .metric.counter.value
} else if exists(.metric.gauge) {
    .metric.gauge.value
} else {
    null
}

# 转换时间戳为ClickHouse兼容的毫秒级DateTime64格式
.timestamp = to_timestamp!(.timestamp)
.timestamp = format_timestamp!(.timestamp, "%Y-%m-%dT%H:%M:%S.%3f")

# 展开标签为单独列(根据你的原始日志标签调整)
.host = .tags.host
.cpu_core = get!(.tags, "cpu_core", "")
.disk_device = get!(.tags, "disk_device", "")
.disk_mountpoint = get!(.tags, "disk_mountpoint", "")
.network_interface = get!(.tags, "interface", "")

# 清理冗余原始字段
del(.metric)
del(.tags)
del(.kind)
del(.source_type)
"""

分指标类型的细节处理

  • CPU指标:提取cpu_core标签区分不同内核的监控数据
  • 磁盘指标:保留disk_device(设备名)和disk_mountpoint(挂载点)标签,方便按磁盘维度筛选
  • 网络指标:提取interface标签对应具体网卡
  • 内存指标:无额外标签时,仅保留metric_name和value即可

脚本验证方法

添加一个console输出端,查看处理后的事件结构是否符合要求:

[sinks.debug_console]
type = "console"
inputs = ["normalize_host_metrics"]
encoding.codec = "json"

启动Vector后,检查控制台输出的JSON字段:

  • 确认timestamp格式为YYYY-MM-DDTHH:MM:SS.sss
  • 确认value为有效数字,无null值(除非指标本身无数据)
  • 确认所有标签字段与ClickHouse列名完全匹配

常见问题修正

  • 类型不匹配错误:检查VRL中字段类型与ClickHouse表是否一致,比如value必须为Float64/Int64,时间必须为DateTime64
  • 缺失列错误:确保VRL生成的字段名与ClickHouse列名完全一致(ClickHouse列名大小写敏感)
  • 指标值为null:补充处理其他指标类型(如summary、histogram)的数值提取逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 07:27:00