首页 > 解决方案 > Prometheus 直方图度量不准确

问题描述

我们将 Nginx 日志由 Logstash 处理到 ES 中。我们可以从该数据中填充响应时间图。但是我们只能在有限的时间内保留日志。因此,普罗米修斯。使用 logstash-output-prometheus 插件,我将request_time值作为直方图指标发送。

        prometheus {
          timer => {
            http_request_duration_seconds => {
              description => "HTTP request_time from Nginx logs"
              value => "%{[request_time]}"
              type => "histogram"
              buckets => [ 0.005, 0.01, 0.025, 0.05, 0.1, 0.2, 0.3, 04, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5, 2, 3, 4, 5, 10, 60, 120, 300, 600 ]
              labels => {
                api => "%{api}"
                method => "%{method}"
                status => "%{status_agg}"
                path => "%{uri_name}"
                host => "%{host}"
              }
            }
          }
        }

当我比较 Logstash 与 Prometheus 的第 95 个百分位值时,它们有时非常不同。

例如, Prometheus 和 Logstash 图的比较

在上图中,底部图表由 Logstash ES 填充。2 台主机的值约为 10 毫秒,其余 4 台主机的值约为 11 毫秒。

(ES查询type:"nginx" AND host:"prod-lb" AND uri_name:"/api/status" AND method:"GET" AND status:"200":)

Prometheus 显示 2 个主机的值约为 10 毫秒(✅),但其余 4 个主机的显示值约为 18 毫秒!

(普罗米修斯查询histogram_quantile(0.95, sum by(le, path, host, method, status) (rate(http_request_duration_seconds_bucket{path="/api/status", host=~".*lb.*"}[2m]))):)

(请注意,这些值周围的桶是 5ms、10ms、25ms)

这种巨大的偏差是预期的吗?

我怎样才能使它更准确?

还有其他方法可以保持更准确的响应时间吗?

标签: nginxlogstashhttpresponseprometheus

解决方案


推荐阅读