Prometheus topk 函数返回所有结果

发布于 2025-01-15 13:59:44 字数 316 浏览 1 评论 0原文

我试图根据过去 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)

enter image description here

How do I limit it to only top 10?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七度光 2025-01-22 13:59:45

首先,您需要启用即时查询 在 Grafana 中。在这种情况下,Grafana 将仅返回每个结果时间序列所选时间范围上的最后一个数据点,而不是返回所选时间范围内每个时间序列的许多数据点。

其他注意事项:

  • 如果 my_app_login计数器,则以下查询应返回过去 24 小时内登录操作次数最多的用户:
topk(10, sum(increase(my_app_login[24h])) by (User_Name))

请注意,即使 my_app_login 仅包含整数值,Prometheus 也可能会从此查询返回小数结果。这是因为 Prometheus 中 increase() 函数的数据模型怪癖 - 请参阅 此评论本文了解详细信息。如果您需要精确的整数结果,请查看 VictoriaMetrics 的 MetricsQL

  • 如果 my_app_login 是一个 gauge,其中包含自上一个示例以来应用程序登录,则以下查询应该有效:
topk(10, sum(sum_over_time(my_app_login[24h])) by (User_Name))

请参阅 sum_over_time() 文档 了解更多详细信息。

请注意,topk(N, ...) 函数可能返回超过 N 时间序列,当此函数用于构建随时间变化的图表时(又名 范围查询)。这是因为它根据图表上的每个时间戳独立返回前 N 个时间序列。如果您只需要图表上的前 N 个时间序列,请查看 topk_maxtopk_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:

  • if my_app_login is a counter, then the following query should returns top users with the most login actions during the last 24 hours:
topk(10, sum(increase(my_app_login[24h])) by (User_Name))

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 for increase() 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.

  • if my_app_login is a gauge, which contains the number of app logins since the previous sample, then the following query should work:
topk(10, sum(sum_over_time(my_app_login[24h])) by (User_Name))

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 top N time series independently per each timestamp on the graph. If you need no more than top N time series on the graph, then take a look at topk_max, topk_last and other topk_* functions from MetricsQL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文