夜莺-Nightingale
夜莺V6
项目介绍 架构介绍
快速开始
黄埔营
安装部署
升级
采集器
使用手册
API
数据库表结构
FAQ
开源生态
Prometheus
版权声明
第1章:天降奇兵
第2章:探索PromQL
第3章:Prometheus告警处理
第4章:Exporter详解
第5章:数据与可视化
第6章:集群与高可用
第7章:Prometheus服务发现
第8章:监控Kubernetes
第9章:Prometheus Operator
参考资料

使用 Categraf 高级配置解锁隐藏功能

简介

Categraf 提供了一个采集框架,支持近百个指标采集插件,框架层面有一些通用的配置,比如 labelsinterval 等,在Categraf项目简介中也有相关介绍,这里不再赘述。

实际在做采集的时候,还有一些其他的通用需求,比如给某个插件的所有指标加个前缀,或者 Drop 掉一些多余的指标,或者对某些值做转换等等,本文就来介绍一下如何解锁这些高级功能。

代码位置

这些通用配置的代码在 InternalConfig,可以看一下这个 struct 有哪些配置。这些配置可以用于所有采集插件。

举个例子

我们拿 http_response 这个插件来做个演示,http_response.toml 的配置样例如下:

[[instances]]
targets = [
     "https://flashcat.cloud"
]

这里我是对快猫星云的域名做了探测,结果如下:

ulric@192 categraf % ./categraf --test --inputs http_response
...
16:27:08 http_response_cert_expire_timestamp agent_hostname=192.168.10.121 method=GET target=https://flashcat.cloud 1688947199
16:27:08 http_response_response_code agent_hostname=192.168.10.121 method=GET target=https://flashcat.cloud 200
16:27:08 http_response_response_time agent_hostname=192.168.10.121 method=GET target=https://flashcat.cloud 0.227738958
16:27:08 http_response_result_code agent_hostname=192.168.10.121 method=GET target=https://flashcat.cloud 0

上面是隐藏了一些无用输出,可以看到针对这个域名采集了 4 个指标,假设,我想 drop 掉以 timestamp 结尾的指标,可以这么配置:

[[instances]]
targets = [
     "https://flashcat.cloud"
]

metrics_drop = ["*timestamp"]

重新测试,可以看到结果中少了 http_response_cert_expire_timestamp 这个指标了。metrics_drop 是个数组,每一项可以支持通配符。

那如果我只想保留以 timestamp 结尾的指标,忽略其他指标,应该怎么配置呢?样例如下:

[[instances]]
targets = [
     "https://flashcat.cloud"
]

metrics_pass = ["*timestamp"]

重新测试,你会发现,结果中只有 http_response_cert_expire_timestamp 这一条指标了,另外 3 条指标则都被忽略了。

有那么点感觉了不?除了可以控制哪些指标保留、哪些指标 Drop 之外,还有另外两个通用配置我们也一起看一下。

为指标增加前缀

这个也很简单,直接上配置样例:

[[instances]]
targets = [
     "https://flashcat.cloud"
]

metrics_name_prefix = "ulricqin_"

测试结果如下:

16:37:39 ulricqin_http_response_response_time agent_hostname=192.168.10.121 method=GET target=https://flashcat.cloud 0.203779417
16:37:39 ulricqin_http_response_result_code agent_hostname=192.168.10.121 method=GET target=https://flashcat.cloud 0
16:37:39 ulricqin_http_response_cert_expire_timestamp agent_hostname=192.168.10.121 method=GET target=https://flashcat.cloud 1688947199
16:37:39 ulricqin_http_response_response_code agent_hostname=192.168.10.121 method=GET target=https://flashcat.cloud 200

所有针对 flashcat.cloud 的探测指标,统一增加了 ulricqin_ 的前缀。

数据转换

InternalConfig 中可以看到还有一个配置是 processor_enum,这个是用于数据转换的,比如某些插件采集的数据可能是可枚举的字符串,字符串无法写入 Prometheus 生态的时序库,此时要把这些可枚举的字符串转换为数字。

举个配置样例:

[[processor_enum]]
  metrics = ["*status"]
  [processor_enum.value_mappings]
    up = 1
    down = 0

这个意思是:针对 status 结尾的指标,采集到了两个值,一个是 up 一个是 down,都是字符串,然后通过 processor_enum 把字符串转换成了不同的数字。

后记

目前的版本,框架层面支持的通用配置还比较少,如果有其他需求可以再继续完善,欢迎 PR :)

开源版
Flashcat
Flashduty