InfluxDB 2.0 的 Prometheus Remote Write 支持
作者:Samantha Wang / 产品, 用例, 开发者
2021 年 6 月 28 日
导航至
在 InfluxDB 1.x 中,我们提供了 对 Prometheus remote write API 的支持。 InfluxDB 2.0 的发布未提供对相同 API 的支持。但是,随着 Telegraf 1.19 的发布,Telegraf 现在包含一个 Prometheus remote write 解析器,可用于摄取这些指标并将它们输出到 InfluxDB 1.x 或 InfluxDB 2.0。
如何使用 Telegraf 将 Prometheus remote write 指标发送到 InfluxDB
我将介绍如何将您的 Prometheus remote write 指标存储在 InfluxDB Cloud 中,但同样的方法也适用于 InfluxDB 2.x。我们将首先设置 Telegraf 以开始侦听指标,然后确保您的 Prometheus 服务器配置为开始将数据 remote write 到您的 Telegraf 实例。
设置 Telegraf 以开始侦听 Prometheus remote write 指标
如果您是 Telegraf 及其插件生态系统的新手,请查看此 入门指南 和 视频,了解如何设置 Telegraf 及其配置以将数据发送到 InfluxDB Cloud。
我们将使用 Telegraf 作为 Prometheus remote write 指标的收集器。要开始 remote write Prometheus 指标,您将使用以下内容
Telegraf 包含许多通用插件,这些插件支持使用可配置的解析器将输入数据解析为指标。 Prometheus Remote Write 解析器 将 Prometheus remote write 样本直接转换为 Telegraf 指标。
您可以指定与任何包含 data_format
选项的输入插件一起使用的解析器。我将介绍如何将 Prometheus Remote Write 解析器专门与 inputs.http_listener_v2 一起使用,以开始收集 Prometheus 指标。
设置 HTTP Listener 的端口及其侦听路径
HTTP Listener v2 插件侦听通过 HTTP 发送的指标,这就是我们设置 Telegraf 以接收 Prometheus remote write 指标的方式。在您的 HTTP Listener v2 输入插件中,您可以通过指定要 Telegraf 侦听的端口来创建端点。您可以在 Telegraf 配置文件中作为 HTTP Listener v2 插件配置的一部分,在 service_address
选项中设置该端口号。在本博客文章中,我们将使用端口 :1234
。接下来,您将设置要侦听的路径并在 path
中配置它;我将我的路径设置为 /receive
。
解析器配置相当简单,因为没有其他配置选项可以开始读取 Prometheus remote write 样本。一旦您使用 HTTP Listener 插件设置了 data_format = "prometheusremotewrite"
和 remote_write
URL 信息,您就可以开始了!
在您的 Telegraf 配置文件中,您的 HTTP Listener v2 插件定义将如下所示
[[inputs.http_listener_v2]]
## Address and port to host HTTP listener on
service_address = ":1234"
## Path to listen to.
path = "/receive"
## Data format to consume.
data_format = "prometheusremotewrite"
添加您的 InfluxDB 输出插件
要将您的数据发送到您的 InfluxDB Cloud 实例,您需要添加 InfluxDB v2 输出插件 并配置您的 URL、组织、存储桶和令牌(我们建议您避免将令牌直接存储在 telegraf.conf 中,而是将它们存储为 环境变量)。
InfluxDB Cloud 输出插件示例
[[outputs.influxdb_v2]]
urls = ["https://us-west-2-1.aws.cloud2.influxdata.com"]
token = "$INFLUX_TOKEN"
organization = "example-org"
bucket = "example-bucket"
Telegraf 提示:为了设置 Telegraf 和进行故障排除,将您的指标写入 STDOUT 以及您的输出数据库也很有帮助。只需将 outputs.file
添加到您的配置中即可。
运行 Telegraf 以开始侦听指标????
一旦您的配置就位,您可以运行以下命令来启动并运行 Telegraf
telegraf --config /path/to/custom/telegraf.conf
Telegraf 运行后,您应该看到您的 Telegraf 正在侦听您设置的端口上的指标。
2021-06-17T17:52:04Z I! Starting Telegraf 1.19.0
2021-06-17T17:52:04Z I! Loaded inputs: http_listener_v2
2021-06-17T17:52:04Z I! Loaded aggregators:
2021-06-17T17:52:04Z I! Loaded processors:
2021-06-17T17:52:04Z I! Loaded outputs: file influxdb_v2
2021-06-17T17:52:04Z I! Tags enabled: host=MBP15-SWANG.local
2021-06-17T17:52:04Z I! [agent] Config: Interval:10s, Quiet:false, Hostname:"MBP15-SWANG.local", Flush Interval:10s
2021-06-17T17:52:04Z I! [inputs.http_listener_v2] Listening on [::]:7070
在您启动并运行 Prometheus 并设置您的 Prometheus 配置文件以进行 remote write 之前,您不会开始看到指标。
设置您的 Prometheus 服务器
如果您尚未设置 Prometheus 服务器,请查看 Prometheus 的 “入门” 文档,以获得有关如何启动并运行 Prometheus 以开始监控自身的良好介绍。
将您的 Telegraf 端点添加到 Prometheus 配置文件的 remote_write
部分
在您的配置文件 (prometheus.yml
) 中,添加一个 remote_write
部分,其中包含您在 HTTP Listener v2 插件中设置的 Telegraf 端点的 URL。
您使用的 URL 应类似于 http://
+service_address
+path
,这是您在上述步骤中指定的。
remote_write
配置示例
remote_write:
- url: "http://localhost:1234/receive"
我的完整 prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets:
rule_files:
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
remote_write:
- url: "http://localhost:1234/receive"
开始将您的 Prometheus 指标 remote write 到 Telegraf
使用您更新的配置文件启动 Prometheus。
./prometheus --config.file=prometheus.yml
Telegraf 一直在侦听您设置的端口上的指标。一旦 Prometheus 启动,Telegraf 就会接收这些指标并开始将它们发送到 InfluxDB Cloud。
在 InfluxDB Cloud 中查看指标
现在您已经运行了 Telegraf 和 Prometheus,您可以在 InfluxDB Cloud 中查看您的数据。从那里,您可以开始创建仪表板或使用 Flux 查询数据。
在 Notebooks 中查看您的 Prometheus remote write 指标
在 数据浏览器 中查看您的 Prometheus remote write 指标
Writing Prometheus remote write to match the schema from InfluxDB 1.x
如果您希望与 InfluxDB 1.x 的 Prometheus remote write 模式保持一致,您需要在配置中快速添加内容。 Prometheus Remote Write 解析器与 Telegraf 中 Prometheus 解析器的 metric_version = 2
格式保持一致。此设置是我们建议 Telegraf 用户在使用任何类型的 Prometheus 插件时正确往返指标的原因。
如果您希望发送数据以与 InfluxDB 1.x 模式对齐,您将需要添加一个包含 Starlark 处理器 和 此脚本。如果您不熟悉,Starlark 是一种轻量级的类 Python 语言,用于快速转换 Telegraf 中的记录。您可能需要编辑下面的示例脚本以适应您的特定用例。
您的配置(不包括输出插件)应如下所示
[[inputs.http_listener_v2]]
## Address and port to host HTTP listener on
service_address = ":1234"
## Path to listen to.
path = "/receive"
## Data format to consume.
data_format = "prometheusremotewrite"
[[processors.starlark]]
## The Starlark source can be set as a string in this configuration file, or
## by referencing a file containing the script. Only one source or script
## should be set at once.
## Source of the Starlark script.
source = '''
def apply(metric):
if metric.name == "prometheus_remote_write":
for k, v in metric.fields.items():
metric.name = k
metric.fields["value"] = v
metric.fields.pop(k)
return metric
'''
您会注意到使用和不使用处理器时模式的以下差异。在 InfluxDB 1.x 中,Prometheus 指标名称成为 InfluxDB 测量。然后,Prometheus 样本值成为使用值字段键(始终为浮点数)的 InfluxDB 字段。然后,Prometheus 标签将成为 InfluxDB 标签。
使用 Prometheus Remote Write 解析器,测量名称是插件名称 prometheus_remote_write
。字段键是 Prometheus 指标名称,样本值为 float64。然后,Prometheus 标签转换为标签。
InfluxDB 输出 | Telegraf 配置 | 样本输出模式 |
InfluxDB 2.x | [[inputs.http_listener_v2]] data_format = "prometheusremotewrite" + [[outputs.influxdb_v2]] | prometheus_remote_write,instance=localhost:9090,job=prometheus scrape_samples_scraped=390 1622840468859000000 |
InfluxDB 1.x | [[inputs.http_listener_v2]] data_format = "prometheusremotewrite" + [[processors.starlark]] + [[outputs.influxdb]] | scrape_samples_scraped,instance=localhost:9090,job=prometheus value=390 1622843903859000000 (不推荐) |
这是 Prometheus remote write 指标的 1.x 模式作为 InfluxDB Notebook 的视图,您可以开始查询和探索数据。
将 Prometheus Remote Write 指标从 InfluxDB 1.x 迁移到 2.0
对于那些一直停留在 InfluxDB 1.x 上的人来说,因为他们一直在等待 2.0 中的 Prometheus remote write 支持,现在终于有机会了!
正如之前多次提到的,如果您在 1.19 中的 Telegraf 解析器之前一直在 remote write Prometheus 指标,您很可能正在使用 API 写入 1.x,并且由于 API 不提供支持而无法迁移到 2.0。
从您的 Prometheus 服务器双 remote write 到 InfluxDB 1.x 和 2.0
当您开始将数据 remote write 到 InfluxDB 1.x 时,您可能已 配置了您的 prometheus.yml
,其中 URL 包括 InfluxDB 正在运行的端口。要双写入 InfluxDB 2.0,我们将向您的配置添加一个额外的 remote write 端点。这可以通过简单地将另一个 -url:
指向您的 Telegraf 端点添加到 prometheus.yml
的 remote_write
部分来完成。
remote_write:
- url: "http://localhost:8086/api/v1/prom/write?db=prometheus"
- url: "http://localhost:1234/receive"
设置 Telegraf 以在 InfluxDB 2.0 中 remote write 数据
按照上面“如何使用 Telegraf 将 Prometheus Remote Write 指标发送到 InfluxDB”部分中的步骤开始将数据 remote write 到 InfluxDB 2.0 以及您现有的 InfluxDB 1.x 实例。如果您希望您的指标与 InfluxDB 1.x 的模式对齐,请记住添加 Starlark 处理器。
验证数据是否流入 InfluxDB 2.0
您可以在 InfluxDB 2.0 中查看和查询指标。请务必查看我们的 迁移文档,以确保您从 1.x 迁移到 2.0 的所有仪表板、查询以及对您重要的所有内容也都得到升级。
缓慢地从 InfluxDB 1.x 迁移
一旦您对 InfluxDB 2.0 中的一切运行情况感到满意,您就可以开始从 1.x 迁移。一旦您从 prometheus.yml
配置中删除 InfluxDB API 端点,您将不再向 1.x 发送数据,而仅使用 Telegraf remote write 到 InfluxDB 2.0。
正如之前强调的那样,从解析器按原样以 metric_version=2
格式(不带 starlark)发送的 Prometheus 指标是我们建议 Telegraf 用户正确往返指标的选项。除非您正在重用 InfluxQL 或其他类似的迁移,否则会导致您的设置中断,否则朝着迁移到此模式的方向努力也非常重要。
注册 InfluxDB Cloud 并下载 Telegraf 以开始写入!
继续开始 remote write 您的 Prometheus 指标!下载或升级到 Telegraf 1.19 并 注册 InfluxDB Cloud 以试用它。如果您有任何问题,请随时通过我们的 Telegraf GitHub 页面、InfluxData Slack 频道 或 社区网站 与我们联系。