如何获取Loki/Grafana查询中唯一标签的数量

huangapple go评论88阅读模式
英文:

How to get the amount of unique labels in a Loki/Grafana Query

问题

为了一些背景信息,我们从Elastic/Kibana迁移到Loki/Grafana,并导入了数年的日志,一切都进展顺利,我可以像预期的那样查询日志并进行探索。现在我正在构建Kibana中的仪表板,但在获取所需结果方面遇到了问题。

我遇到问题的面板只显示了某个时间段内活跃用户的数量,其中active_user_id是一个标签,我只需要获取唯一active_user_id值的计数。

我有以下LogQL查询,如果运行它,将给我提供97个独立值,每个标签值在时间间隔内出现的次数计数,但我只需要在仪表板上得到97,但我无法实现这一点。

count by (active_user_id) (count_over_time({type="request"}[$__interval]))

我还尝试了以下查询,它返回272,这是存在日志的不同时间戳的数量:

count(count by (active_user_id) (count_over_time({type="request"}[$__interval])))

我认为我已经很接近了,但不确定我错过了什么,因为这是我第一次使用Loki和Grafana。

任何帮助将不胜感激。

英文:

for some context we migrated from Elastic/Kibana to Loki/Grafana and imported several years of logs, all is going well, I can query the logs and explore as expected. Now I'm in the process of building the dashboards we had in Kibana and I'm having problems getting the results I need.

The panel I'm having problems with just has a number of active users in a time period, in which case active_user_id is a label and I just need to get the count of unique active_user_id values.

I have this LogQL query, which if I run it gives me 97 independent values with the count of times each label value is present in the time interval, but I just need to get 97 in the dashboard, however I can't accomplish that.

count by (active_user_id) (count_over_time({type="request"}[$__interval]))

I also tried this query, which returns 272, this being the amount of different timestamps where there are logs:

count(count by (active_user_id) (count_over_time({type="request"}[$__interval])))

I think I'm pretty close but not sure what I'm missing as this is my first time using loki and grafana.

Any help will be greatly appreciated.

答案1

得分: 1

你的第一个查询很好,因为它提供了活跃用户与出现次数的表格。你需要将 $__interval 替换为 $__range,以便计数是在整个时间范围内进行,而不是每个间隔。所以,不要使用

count by (active_user_id) (count_over_time({type="request"}[$__interval]))

而是使用

count(count(count_over_time({type="request"}[$__range])) by (active_user_id))

这将为你提供你在面板中期望的 97 活跃用户数量。

让我知道进展如何 如何获取Loki/Grafana查询中唯一标签的数量

英文:

Your first query is good since it gives you a table of the active users against the number of occurrences. You need to use the $__range in place of $__interval so that the count is done over the whole time range instead of each interval. So instead of

count by (active_user_id) (count_over_time({type="request"}[$__interval]))

use

count(count(count_over_time({type="request"}[$__range])) by (active_user_id))

which will give you the number of active_user_id in the whole time range you are interested in. I believe that should give you the 97 you desire in your panel.

Let me know how it goes 如何获取Loki/Grafana查询中唯一标签的数量

huangapple
  • 本文由 发表于 2023年6月15日 08:27:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定