Prometheus Grafana按计数模板化

时间:2017-10-06 18:45:08

标签: grafana prometheus

我正在尝试为每个API端点添加一个下拉列表,它将显示http请求的QPS和延迟(RED指标)。

我使用了Grafana的模板并使用了以下prometheus查询。

label_values(http_duration_milliseconds_count, api_path)

但这里的问题是排序顺序。它显示了一些longtail api请求,例如/ admin / phpMyAdmin all。

我想通过计数只显示前10个端点,以便在此下拉菜单中显示。我该如何实现这一目标?

在我的第一个仪表板上附上一张图片以供参考。

enter image description here

1 个答案:

答案 0 :(得分:2)

我们可以使用 query_result 来实现这一目标。 http://docs.grafana.org/features/datasources/prometheus/#query-variable

query_result(topk(10, sort_desc(sum(http_tt_ms_count) by (api_path))))

http_tt_ms_count - 是我的proemetheus度量时间序列,并且需要时间。

api_path - 是我的标签名称

这个query_result会给出这样的三元组值。

{api_path="/search/query"} 25704195 1507641522000

使用查询路径中的 Regex 字段来仅获取api名称。

*api_path="(.*)".*

这看起来很长,但

label_values((topk(10, sort_desc(sum(http_tt_ms_count) by (api_path)))), api_path) 

不能在grafana工作,这让我走进了这条道路。