You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何通过PromQL正确展示API的P99/P95延迟指标?

问题分析与解决方案

核心错误:用错了PromQL函数

你写的histogram_quantile()是专门给Histogram类型指标计算分位数的,而你代码里用的是Prometheus的Summary类型,两者完全不兼容,这是Grafana没数据的根本原因。

正确的PromQL查询方式

Summary类型本身已经直接输出了预配置的分位数(你代码里定义了0.5/0.7/0.95/0.99),直接查询对应标签的时间序列即可:

查询P95延迟

requests_latency_seconds{instance=~"$instance", application="$application", quantile="0.95", uri=~".*helloThree.*"}

查询P99延迟

requests_latency_seconds{instance=~"$instance", application="$application", quantile="0.99", uri=~".*helloThree.*"}

额外需要修正的问题

你原来的查询里写了uri!~"\".*helloThree.*\"",这是排除了/helloThree接口的数据,和你要测试的目标完全相反,改成uri=~".*helloThree.*"才能拿到对应接口的指标。

进阶优化建议

如果你的场景需要对多个实例的分位数做聚合(比如集群级别的P95),Summary类型的分位数聚合结果会不准确,建议换成Histogram类型来实现:

  1. 把代码里的Summary替换为Histogram,配置合适的延迟区间桶
  2. 之后就可以用histogram_quantile()来计算任意分位数,并且支持跨实例聚合

示例Histogram定义(参考):

@Bean
Histogram getHistogram(){
    return Histogram.build()
            .name("requests_latency_seconds")
            .help("Request latency in seconds.")
            // 定义延迟区间桶,可根据实际业务调整
            .buckets(0.05, 0.1, 0.2, 0.5, 1.0, 2.0)
            .register(registry.getPrometheusRegistry());
}

对应的聚合P95查询:

histogram_quantile(0.95, sum(rate(requests_latency_seconds_bucket{instance=~"$instance", application="$application", uri=~".*helloThree.*"}[1m])) by (le, uri))

内容的提问来源于stack exchange,提问作者esther lee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 00:43:12