抓取 Prometheus 指标
要使用 Flux 抓取来自 HTTP 可访问端点的 Prometheus 格式的指标
- 导入
experimental/prometheus
包。 - 使用
prometheus.scrape
并指定要从中抓取指标的 url。
import "experimental/prometheus"
prometheus.scrape(url: "http://localhost:8086/metrics")
输出结构
prometheus.scrape()
返回一个包含以下列的 表流
表按 _measurement、_field 和 标签列 分组。
带有下划线前缀的列
带有下划线 (_
) 前缀的列被认为是“系统”列。一些 Flux 函数需要这些列才能正常工作。
Prometheus 查询结果示例
以下是从 InfluxDB OSS 2.x /metrics
端点抓取的 Prometheus 指标示例
# HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed.
# TYPE go_memstats_alloc_bytes_total counter
go_memstats_alloc_bytes_total 1.42276424e+09
# HELP go_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table.
# TYPE go_memstats_buck_hash_sys_bytes gauge
go_memstats_buck_hash_sys_bytes 5.259247e+06
# HELP task_executor_run_latency_seconds Records the latency between the time the run was due to run and the time the task started execution, by task type
# TYPE task_executor_run_latency_seconds histogram
task_executor_run_latency_seconds_bucket{task_type="system",le="0.25"} 4413
task_executor_run_latency_seconds_bucket{task_type="system",le="0.5"} 11901
task_executor_run_latency_seconds_bucket{task_type="system",le="1"} 12565
task_executor_run_latency_seconds_bucket{task_type="system",le="2.5"} 12823
task_executor_run_latency_seconds_bucket{task_type="system",le="5"} 12844
task_executor_run_latency_seconds_bucket{task_type="system",le="10"} 12864
task_executor_run_latency_seconds_bucket{task_type="system",le="+Inf"} 74429
task_executor_run_latency_seconds_sum{task_type="system"} 4.256783538679698e+11
task_executor_run_latency_seconds_count{task_type="system"} 74429
# HELP task_executor_run_duration The duration in seconds between a run starting and finishing.
# TYPE task_executor_run_duration summary
task_executor_run_duration{taskID="00xx0Xx0xx00XX0x0",task_type="threshold",quantile="0.5"} 5.178160855
task_executor_run_duration{taskID="00xx0Xx0xx00XX0x0",task_type="threshold",quantile="0.9"} 5.178160855
task_executor_run_duration{taskID="00xx0Xx0xx00XX0x0",task_type="threshold",quantile="0.99"} 5.178160855
task_executor_run_duration_sum{taskID="00xx0Xx0xx00XX0x0",task_type="threshold"} 2121.9758301650004
task_executor_run_duration_count{taskID="00xx0Xx0xx00XX0x0",task_type="threshold"} 570
当被 Flux 抓取时,这些指标返回以下表流
_time | _measurement | url | _field | _value |
---|
2021-01-01T00:00:00Z | prometheus | http://localhost:8086/metrics | go_memstats_alloc_bytes_total | 1422764240.0 |
_time | _measurement | url | _field | _value |
---|
2021-01-01T00:00:00Z | prometheus | http://localhost:8086/metrics | go_memstats_buck_hash_sys_bytes | 5259247.0 |
_time | _measurement | task_type | url | le | _field | _value |
---|
2021-01-01T00:00:00Z | prometheus | system | http://localhost:8086/metrics | 0.25 | task_executor_run_latency_seconds | 4413 |
_time | _measurement | task_type | url | le | _field | _value |
---|
2021-01-01T00:00:00Z | prometheus | system | http://localhost:8086/metrics | 0.5 | task_executor_run_latency_seconds | 11901 |
_time | _measurement | task_type | url | le | _field | _value |
---|
2021-01-01T00:00:00Z | prometheus | system | http://localhost:8086/metrics | 1 | task_executor_run_latency_seconds | 12565 |
_time | _measurement | task_type | url | le | _field | _value |
---|
2021-01-01T00:00:00Z | prometheus | system | http://localhost:8086/metrics | 2.5 | task_executor_run_latency_seconds | 12823 |
_time | _measurement | task_type | url | le | _field | _value |
---|
2021-01-01T00:00:00Z | prometheus | system | http://localhost:8086/metrics | 5 | task_executor_run_latency_seconds | 12844 |
_time | _measurement | task_type | url | le | _field | _value |
---|
2021-01-01T00:00:00Z | prometheus | system | http://localhost:8086/metrics | +Inf | task_executor_run_latency_seconds | 74429 |
_time | _measurement | task_type | url | _field | _value |
---|
2021-01-01T00:00:00Z | prometheus | system | http://localhost:8086/metrics | task_executor_run_latency_seconds_sum | 425678353867.9698 |
_time | _measurement | task_type | url | _field | _value |
---|
2021-01-01T00:00:00Z | prometheus | system | http://localhost:8086/metrics | task_executor_run_latency_seconds_count | 74429 |
_time | _measurement | task_type | taskID | url | quantile | _field | _value |
---|
2021-01-01T00:00:00Z | prometheus | threshold | 00xx0Xx0xx00XX0x0 | http://localhost:8086/metrics | 0.5 | task_executor_run_duration | 5.178160855 |
_time | _measurement | task_type | taskID | url | quantile | _field | _value |
---|
2021-01-01T00:00:00Z | prometheus | threshold | 00xx0Xx0xx00XX0x0 | http://localhost:8086/metrics | 0.9 | task_executor_run_duration | 5.178160855 |
_time | _measurement | task_type | taskID | url | quantile | _field | _value |
---|
2021-01-01T00:00:00Z | prometheus | threshold | 00xx0Xx0xx00XX0x0 | http://localhost:8086/metrics | 0.99 | task_executor_run_duration | 5.178160855 |
_time | _measurement | task_type | taskID | url | _field | _value |
---|
2021-01-01T00:00:00Z | prometheus | threshold | 00xx0Xx0xx00XX0x0 | http://localhost:8086/metrics | task_executor_run_duration_sum | 2121.9758301650004 |
_time | _measurement | task_type | taskID | url | _field | _value |
---|
2021-01-01T00:00:00Z | prometheus | threshold | 00xx0Xx0xx00XX0x0 | http://localhost:8086/metrics | task_executor_run_duration_count | 570 |
抓取的 Prometheus 指标的不同数据结构
Telegraf 和 InfluxDB 提供了抓取 Prometheus 指标并将其存储在 InfluxDB 中的工具。根据您用于抓取指标的工具和配置,生成的数据结构可能与 上面描述的 prometheus.scrape()
返回的结构不同。
有关存储在 InfluxDB 中的抓取的 Prometheus 指标的不同数据结构的信息,请参阅 InfluxDB Prometheus 指标解析格式。
将 Prometheus 指标写入 InfluxDB
要将抓取的 Prometheus 指标写入 InfluxDB
- 使用
prometheus.scrape
来抓取 Prometheus 指标。 - 使用
to()
将抓取的指标写入 InfluxDB。
import "experimental/prometheus"
prometheus.scrape(url: "http://example.com/metrics")
|> to(bucket: "example-bucket", host: "http://localhost:8086", org: "example-org", token: "mYsuP3R5eCR37t0K3n")
以固定间隔将 Prometheus 指标写入 InfluxDB
要抓取 Prometheus 指标并以固定间隔将其写入 InfluxDB,请在 InfluxDB 任务中抓取 Prometheus 指标。
import "experimental/prometheus"
option task = {name: "Scrape Prometheus metrics", every: 10s}
prometheus.scrape(url: "http://example.com/metrics")
|> to(bucket: "example-bucket")
支持和反馈
感谢您成为我们社区的一份子!我们欢迎并鼓励您提供关于 Flux 和本文档的反馈和错误报告。要寻求支持,请使用以下资源
拥有年度合同或支持合同的客户 可以联系 InfluxData 支持。