文档文档

使用 Prometheus 计数器

使用 Flux 查询和转换存储在 InfluxDB 中的 Prometheus 计数器指标。

计数器是一种累积指标,表示一个单调递增的单调递增计数器,其值只能增加或在重启时重置为零。

Prometheus 指标类型

Prometheus 格式的计数器指标示例
# HELP example_counter_total Total representing an example counter metric
# TYPE example_counter_total counter
example_counter_total 282327

由于计数器可能会定期重置为 0,因此任何涉及计数器指标的查询都应规范化数据,以在进一步处理之前考虑计数器重置

以下示例包括使用 prometheus.scrape()InfluxDB OSS 2.x /metrics 端点收集并存储在 InfluxDB 中的示例数据。

Prometheus 指标解析格式

查询结构取决于用于抓取 Prometheus 指标的 Prometheus 指标解析格式。 在下面选择适当的指标格式版本。

规范化计数器重置

  1. prometheus 测量和计数器指标名称字段筛选结果。
  2. 使用 increase() 规范化计数器重置。 increase() 返回列值中正向变化的累积总和。

increase() 考虑了计数器重置,但可能会在重置时损失一些精度,具体取决于您的抓取间隔。 在计数器重置时,increase() 假定没有增加。

from(bucket: "example-bucket")
    |> range(start: -1m)
    |> filter(fn: (r) => r._measurement == "prometheus" and r._field == "http_query_request_bytes")
    |> increase()
Raw Prometheus counter metric in InfluxDB
Increase on Prometheus counter metric in InfluxDB

查看示例输入和输出数据

  1. 计数器指标名称测量和 counter 字段筛选结果。
  2. 使用 increase() 规范化计数器重置。 increase() 返回列值中正向变化的累积总和。

increase() 考虑了计数器重置,但可能会在重置时损失一些精度,具体取决于您的抓取间隔。 在计数器重置时,increase() 假定没有增加。

from(bucket: "example-bucket")
    |> range(start: -1m)
    |> filter(fn: (r) => r._measurement == "http_query_request_bytes" and r._field == "counter")
    |> increase()
Raw Prometheus counter metric in InfluxDB
Increase on Prometheus counter metric in InfluxDB

查看示例输入和输出数据

计算规范化计数器值之间的变化

使用 difference()规范化的计数器数据,返回后续值之间的差值。

from(bucket: "example-bucket")
    |> range(start: -1m)
    |> filter(fn: (r) => r._measurement == "prometheus" and r._field == "http_query_request_bytes")
    |> increase()
    |> difference()
Raw Prometheus counter metric in InfluxDB
Normalize Prometheus counter metric to account for counter resets

查看示例输入和输出数据

from(bucket: "example-bucket")
    |> range(start: -1m)
    |> filter(fn: (r) => r._measurement == "http_query_request_bytes" and r._field == "counter")
    |> increase()
    |> difference()
Raw Prometheus counter metric in InfluxDB
Normalize Prometheus counter metric to account for counter resets

查看示例输入和输出数据

计算规范化计数器值的变化率

使用 derivative() 计算规范化计数器值之间的变化率。 默认情况下,derivative() 返回每秒的变化率。 使用 unit 参数 自定义速率单位。

from(bucket: "example-bucket")
    |> range(start: -1m)
    |> filter(fn: (r) => r._measurement == "prometheus" and r._field == "http_query_request_bytes")
    |> increase()
    |> derivative()
Normalized Prometheus counter metric in InfluxDB
Calculate the rate of change in Prometheus counter metric with Flux

查看示例输入和输出数据

from(bucket: "example-bucket")
    |> range(start: -1m)
    |> filter(fn: (r) => r._measurement == "http_query_request_bytes" and r._field == "counter")
    |> increase()
    |> derivative()
Normalized Prometheus counter metric in InfluxDB
Calculate the rate of change in Prometheus counter metric with Flux

查看示例输入和输出数据

计算指定时间窗口内的平均变化率

要计算指定时间窗口内规范化计数器值的平均变化率

  1. 导入 experimental/aggregate

  2. 规范化的计数器值.

  3. 使用 aggregate.rate() 计算每个时间窗口的平均变化率。

    • 使用 every 参数 定义时间窗口间隔。
    • 使用 unit 参数 自定义速率单位。默认情况下,aggregate.rate() 返回每秒 (1s) 变化率。
    • 使用 groupColumns 参数 指定在执行聚合时要分组的列。
import "experimental/aggregate"

from(bucket: "example-bucket")
    |> range(start: -1m)
    |> filter(fn: (r) => r._measurement == "prometheus" and r._field == "http_query_request_bytes")
    |> increase()
    |> aggregate.rate(every: 15s, unit: 1s)
Normalized Prometheus counter metric in InfluxDB
Calculate the rate of change in Prometheus counter metrics per time window with Flux

查看示例输入和输出数据

import "experimental/aggregate"

from(bucket: "example-bucket")
    |> range(start: -1m)
    |> filter(fn: (r) => r._measurement == "http_query_request_bytes" and r._field == "counter")
    |> increase()
    |> aggregate.rate(every: 15s, unit: 1s)
Normalized Prometheus counter metric in InfluxDB
Calculate the rate of change in Prometheus counter metrics per time window with Flux

查看示例输入和输出数据


此页面是否对您有帮助?

感谢您的反馈!


Flux 的未来

Flux 即将进入维护模式。您可以继续像目前一样使用它,而无需对您的代码进行任何更改。

阅读更多

现已全面上市

InfluxDB 3 Core 和 Enterprise

快速启动。更快扩展。

获取更新

InfluxDB 3 Core 是一个开源、高速、最近数据引擎,可实时收集和处理数据,并将其持久化到本地磁盘或对象存储。 InfluxDB 3 Enterprise 以 Core 的基础为构建,增加了高可用性、读取副本、增强的安全性以及数据压缩,从而实现更快的查询和优化的存储。 InfluxDB 3 Enterprise 的免费层可供非商业家庭或业余爱好者使用。

有关更多信息,请查看