Telegraf 简介
Telegraf 作为一个指标层面的 all-in-one 的采集器,用途广泛,Telegraf 具备多种 output 插件,本篇介绍如何使用 prometheus remote write 方式发送数据给后端。
Telegraf 具体的介绍之前讲过很多了,不在这里展开了,如果想看历史文章,可以关注公众号 SRETalk,搜索(搜索关键字 Telegraf)历史文章,有好几篇调研笔记。
核心摘要
- Telegraf 可以通过
outputs.http插件,把指标按prometheusremotewrite格式发送到后端。 - Nightingale 的
n9e-server支持接收 Prometheus remote write 数据,因此可以作为 Telegraf 的写入目标。 - 如果直接写入 Prometheus,需要 Prometheus 开启 remote write receiver。
- Telegraf 默认会给指标附带
host标签,和已有监控标签可能冲突;通过omit_hostname和agent_hostname可以降低冲突风险。 - 本文适合已经使用 Telegraf 采集指标,并希望对接 Nightingale 或 Prometheus remote write 入口的场景。
术语解释:Prometheus remote write 是什么
Prometheus remote write 是 Prometheus 生态常用的远程写入协议。采集端或中转组件可以把时序数据写到支持该协议的后端,而不是只依赖 Prometheus 主动抓取。
在本文场景里,Telegraf 不是让 Prometheus 来 scrape 自己,而是通过 HTTP output 主动把指标按 remote write 格式推给 Nightingale 或 Prometheus 的接收地址。
Telegraf 输出插件
Telegraf 支持多种 output plugin,可以把采集到的数据发给多种后端,比如 OpenTSDB、Datadog 等。之前讲解 Nightingale 和 Telegraf 对接的时候,大都推荐使用 outputs.opentsdb 插件与 Nightingale 对接。
OpenTSDB 的方式比较简单易于理解,但性能不好。更推荐的发送方式,是通过 Prometheus remote write 协议发送指标。
配置 Telegraf 使用 remote write 发送指标
在 telegraf.conf 里增加下面的配置:
[[outputs.http]]
url = "http://localhost:19000/prometheus/v1/write"
data_format = "prometheusremotewrite"
[outputs.http.headers]
Content-Type = "application/x-protobuf"
Content-Encoding = "snappy"
X-Prometheus-Remote-Write-Version = "0.1.0"
这是开启了 Telegraf 的 HTTP output 插件,通过 HTTP 方式发送指标给后端。发送地址配置为 http://localhost:19000/prometheus/v1/write,这是 n9e-server 的地址。因为 n9e-server 支持接收 Prometheus remote write 数据,所以通过这种方式,Telegraf 就可以把数据推给 Nightingale。
当然,也可以把数据直接推给 Prometheus,url 配置为 Prometheus 的 remote write 地址即可,比如:
http://localhost:9090/api/v1/write。前提是 Prometheus 需要开启 remote-write 的 feature,怎么开启?在 Prometheus 的启动参数中增加:--web.enable-remote-write-receiver即可。
处理 host 标签冲突
Telegraf 发给后端的监控指标,都会带有一个 host 标签,用来标识 Telegraf 所在机器的机器名。host 这个标签比较通用,容易和实际监控数据里的标签冲突。
通过 remote write 方式发数据给 Nightingale 的时候,我们建议做如下调整。
1, 在 [global_tags] 中增加一个标签,标签名是 agent_hostname,标签值设置为本机的机器名,比如
[global_tags]
agent_hostname = "telegraf-flashcat.cloud"
2, 在 [agent] 配置段中忽略 hostname 字段,配置如下:
[agent]
## Override default hostname, if empty use os.Hostname()
hostname = ""
## If set to true, do no set the "host" tag in the telegraf agent.
omit_hostname = true
这样一来,Telegraf 就会忽略 host 字段。同时,我们又设置了全局标签 agent_hostname,Telegraf 会给所有时序数据都附上这个标签。Nightingale 服务端收到数据之后,看到 agent_hostname 标签,就会当做机器标识,自动 rename 成 ident,写入 target 表,这样就可以在对象列表里看到对应机器。
结论
如果已经使用 Telegraf 采集指标,并希望接入 Nightingale 或 Prometheus remote write 入口,outputs.http 加 prometheusremotewrite 是更直接的方式。落地时要重点检查两个点:后端 remote write 接收地址是否可用,以及 Telegraf 默认 host 标签是否会和现有标签体系冲突。
FAQ
Q1:Telegraf 可以直接把指标推给 Prometheus 吗?
A:可以,但 Prometheus 需要开启 remote write receiver,并把 URL 配置为 Prometheus 的 remote write 接收地址,例如 http://localhost:9090/api/v1/write。
Q2:为什么示例里使用 outputs.http?
A:因为这里要通过 HTTP 把指标以 prometheusremotewrite 格式写到后端,outputs.http 可以承担这个发送动作。
Q3:为什么要设置 agent_hostname?
A:Telegraf 默认的 host 标签容易和已有标签冲突。设置 agent_hostname 并忽略 host 后,Nightingale 可以把 agent_hostname 识别为机器标识并写入对象列表。
Q4:OpenTSDB output 和 remote write 方式怎么选? A:原文口径是 OpenTSDB 方式简单易理解,但性能不好;通过 Prometheus remote write 协议发送指标是更推荐的方式。
