Prometheus topk 函数返回所有结果
我试图根据过去 24 小时内的登录次数绘制出排名前 5 位的用户。但是当我使用下面的 topk
函数时,我得到了该时间范围内的所有用户登录。
询问: topk(10, sum(my_app_login) by (User_Name) != 0)
如何将其限制为仅前 10 个?
I am trying to graph out the top 5 users by their login count in the last 24 hours. But when I use the below topk
function, I am getting all the user login within that time range.
Query:topk(10, sum(my_app_login) by (User_Name) != 0)
How do I limit it to only top 10?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要启用即时查询 在 Grafana 中。在这种情况下,Grafana 将仅返回每个结果时间序列所选时间范围上的最后一个数据点,而不是返回所选时间范围内每个时间序列的许多数据点。
其他注意事项:
my_app_login
是 计数器,则以下查询应返回过去 24 小时内登录操作次数最多的用户:请注意,即使
my_app_login
仅包含整数值,Prometheus 也可能会从此查询返回小数结果。这是因为 Prometheus 中increase()
函数的数据模型怪癖 - 请参阅 此评论 和 本文了解详细信息。如果您需要精确的整数结果,请查看 VictoriaMetrics 的 MetricsQL。my_app_login
是一个 gauge,其中包含自上一个示例以来应用程序登录,则以下查询应该有效:请参阅 sum_over_time() 文档 了解更多详细信息。
请注意,topk(N, ...) 函数可能返回超过
N
时间序列,当此函数用于构建随时间变化的图表时(又名 范围查询)。这是因为它根据图表上的每个时间戳独立返回前N
个时间序列。如果您只需要图表上的前N
个时间序列,请查看 topk_max、topk_last 等MetricsQL 中的topk_*
函数。First of all, you need to enable instant query in Grafana. In this case Grafana will return only the last data point on the selected time range per each resulting time series instead of returning many data points per time series on the selected time range.
Other considerations:
my_app_login
is a counter, then the following query should returns top users with the most login actions during the last 24 hours:Note that Prometheus may return fractional results from this query even if
my_app_login
contains only integer values. This is because of data model quirks forincrease()
function in Prometheus - see this comment and this article for details. If you need exact integer results, then take a look at MetricsQL from VictoriaMetrics.my_app_login
is a gauge, which contains the number of app logins since the previous sample, then the following query should work:See sum_over_time() docs for more details.
Note that topk(N, ...) function may return more than
N
time series when this function is used for building a graph over time (aka range query). This is because it returns topN
time series independently per each timestamp on the graph. If you need no more than topN
time series on the graph, then take a look at topk_max, topk_last and othertopk_*
functions from MetricsQL.