抓取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抓取时,这些指标返回以下表的流:
| 时间 | 测量值 | 网址 | 字段 | 值 |
|---|
| 2021-01-01T00:00:00Z | prometheus | http://localhost:8086/metrics | go_memstats_alloc_bytes_total | 1422764240.0 |
| _时间 | _测量 | 网址 | _字段 | _值 |
|---|
| 2021-01-01T00:00:00Z | prometheus | http://localhost:8086/metrics | go_memstats_buck_hash_sys_bytes | 5259247.0 |
| 时间 | 测量 | 任务类型 | 网址 | le | 字段 | 值 |
|---|
| 2021-01-01T00:00:00Z | prometheus | system | http://localhost:8086/metrics | 0.25 | task_executor_run_latency_seconds | 4413 |
| 时间 | 测量 | 任务类型 | 网址 | 小于或等于 | 字段 | 值 |
|---|
| 2021-01-01T00:00:00Z | prometheus | 系统 | http://localhost:8086/metrics | 0.5 | task_executor_run_latency_seconds | 11901 |
| 时间 | 测量 | 任务类型 | 网址 | le | 字段 | 值 |
|---|
| 2021-01-01T00:00:00Z | prometheus | 系统 | http://localhost:8086/metrics | 1 | task_executor_run_latency_seconds | 12565 |
| 时间 | 测量 | 任务类型 | 网址 | le | 字段 | 值 |
|---|
| 2021-01-01T00:00:00Z | prometheus | 系统 | http://localhost:8086/metrics | 2.5 | task_executor_run_latency_seconds | 12823 |
| 时间 | 测量 | 任务类型 | 网址 | le | 字段 | 值 |
|---|
| 2021-01-01T00:00:00Z | prometheus | 系统 | http://localhost:8086/metrics | 5 | task_executor_run_latency_seconds | 12844 |
| 时间 | 测量 | 任务类型 | 网址 | 小于或等于 | 字段 | 值 |
|---|
| 2021-01-01T00:00:00Z | prometheus | 系统 | http://localhost:8086/metrics | +无穷大 | task_executor_run_latency_seconds | 74429 |
| 时间 | 测量 | 任务类型 | 网址 | 字段 | 值 |
|---|
| 2021-01-01T00:00:00Z | prometheus | 系统 | http://localhost:8086/metrics | task_executor_run_latency_seconds_sum | 425678353867.9698 |
| 时间 | 测量 | 任务类型 | 网址 | 字段 | 值 |
|---|
| 2021-01-01T00:00:00Z | prometheus | 系统 | http://localhost:8086/metrics | task_executor_run_latency_seconds_count | 74429 |
| 时间 | 测量 | 任务类型 | 任务ID | 网址 | 分位数 | 字段 | 值 |
|---|
| 2021-01-01T00:00:00Z | prometheus | 阈值 | 00xx0Xx0xx00XX0x0 | http://localhost:8086/metrics | 0.5 | task_executor_run_duration | 5.178160855 |
| 时间 | 测量 | 任务类型 | 任务ID | 网址 | 分位数 | 字段 | 值 |
|---|
| 2021-01-01T00:00:00Z | prometheus | 阈值 | 00xx0Xx0xx00XX0x0 | http://localhost:8086/metrics | 0.9 | 任务执行者运行持续时间 | 5.178160855 |
| 时间 | 测量 | 任务类型 | 任务ID | 网址 | 分位数 | 字段 | 值 |
|---|
| 2021-01-01T00:00:00Z | prometheus | 阈值 | 00xx0Xx0xx00XX0x0 | http://localhost:8086/metrics | 0.99 | 任务执行者运行时长 | 5.178160855 |
| 时间 | 测量 | 任务类型 | 任务ID | 网址 | 字段 | 值 |
|---|
| 2021-01-01T00:00:00Z | prometheus | 阈值 | 00xx0Xx0xx00XX0x0 | http://localhost:8086/metrics | task_executor_run_duration_sum | 2121.9758301650004 |
| 时间 | 测量 | 任务类型 | 任务ID | 网址 | 字段 | 值 |
|---|
| 2021-01-01T00:00:00Z | prometheus | 阈值 | 00xx0Xx0xx00XX0x0 | http://localhost:8086/metrics | task_executor_run_duration_count | 570 |
将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")