文档文档

使用 Prometheus 直方图

使用 Flux 查询和转换存储在 InfluxDB 中的 Prometheus 直方图指标。

直方图 抽样观测值(通常是请求持续时间或响应大小之类的内容),并在可配置的存储桶中对其进行计数。它还提供所有观测值的总和。

Prometheus 指标类型

Prometheus 数据中的直方图指标示例
# HELP example_histogram_duration Duration of given tasks as example histogram metric
# TYPE example_histogram_duration histogram
example_histogram_duration_bucket{le="0.1"} 80
example_histogram_duration_bucket{le="0.25"} 85
example_histogram_duration_bucket{le="0.5"} 85
example_histogram_duration_bucket{le="1"} 87
example_histogram_duration_bucket{le="2.5"} 87
example_histogram_duration_bucket{le="5"} 88
example_histogram_duration_bucket{le="+Inf"} 88
example_histogram_duration_sum 6.833441910000001
example_histogram_duration_count 88

以下示例包括从 InfluxDB OSS 2.x /metrics 端点收集并存储在 InfluxDB 中的示例数据。

Prometheus 指标解析格式

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

在 InfluxDB 中可视化 Prometheus 直方图

InfluxDB 目前不支持将 Prometheus 直方图指标可视化为传统直方图。 现有的 InfluxDB 直方图可视化 与存储在 InfluxDB 中的 Prometheus 直方图数据的格式不兼容

从 Prometheus 直方图计算分位数

  1. 导入 experimental/prometheus
  2. prometheus 测量和直方图指标名称字段过滤结果。
  3. (推荐)使用 aggregateWindow() 对数据进行降采样并优化查询。
  4. 使用 prometheus.histogramQuantile() 计算特定分位数。
import "experimental/prometheus"

from(bucket: "example-bucket")
    |> start(range: -1h)
    |> filter(fn: (r) => r._measurement == "prometheus")
    |> filter(fn: (r) => r._field == "qc_all_duration_seconds")
    |> aggregateWindow(every: 1m, fn: mean, createEmpty: false)
    |> prometheus.histogramQuantile(quantile: 0.99)
  1. 导入 experimental/prometheus
  2. 直方图指标名称测量过滤结果。
  3. (推荐)使用 aggregateWindow() 对数据进行降采样并优化查询。 createEmpty 参数设置为 false
  4. 使用 prometheus.histogramQuantile() 计算特定分位数。 将 metricVersion 指定为 1
import "experimental/prometheus"

from(bucket: "example-bucket")
    |> start(range: -1h)
    |> filter(fn: (r) => r._measurement == "qc_all_duration_seconds")
    |> aggregateWindow(every: 1m, fn: mean, createEmpty: false)
    |> prometheus.histogramQuantile(quantile: 0.99, metricVersion: 1)
Calculate a quantile from Prometheus histogram metrics

将 createEmpty 设置为 false

当使用 aggregateWindow()prometheus.histogramQuantile 降采样数据时,createEmpty 参数设置为 false。 从 aggregateWindow() 生成的空表会导致以下错误。

histogramQuantile: unexpected null in the countColumn

从 Prometheus 直方图计算多个分位数

  1. 使用 上面步骤 1-2(可选步骤 3) 查询直方图数据。
  2. 使用 union() 联合计算唯一分位数的多个表流。
import "experimental/prometheus"

data =
    from(bucket: "example-bucket")
        |> start(range: -1h)
        |> filter(fn: (r) => r._measurement == "prometheus")
        |> filter(fn: (r) => r._field == "qc_all_duration_seconds")
        |> aggregateWindow(every: 1m, fn: mean, createEmpty: false)

union(
    tables: [
        data |> prometheus.histogramQuantile(quantile: 0.99),
        data |> prometheus.histogramQuantile(quantile: 0.5),
        data |> prometheus.histogramQuantile(quantile: 0.25),
    ],
)
import "experimental/prometheus"

data =
    from(bucket: "example-bucket")
        |> start(range: -1h)
        |> filter(fn: (r) => r._measurement == "qc_all_duration_seconds")
        |> aggregateWindow(every: 1m, fn: mean, createEmpty: false)

union(
    tables: [
        data |> prometheus.histogramQuantile(quantile: 0.99, metricVersion: 1),
        data |> prometheus.histogramQuantile(quantile: 0.5, metricVersion: 1),
        data |> prometheus.histogramQuantile(quantile: 0.25, metricVersion: 1),
    ],
)
Calculate multiple quantiles from Prometheus histogram metrics

此页是否对您有帮助?

感谢您的反馈!


Flux 的未来

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

阅读更多

现已全面上市

InfluxDB 3 Core 和 Enterprise

快速启动。更快扩展。

获取更新

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

有关更多信息,请查看