英文:
Grafana query for distinct labels number
问题
我有一个用于具有标签“ticker”的请求的Prometheus计数器input_requests
:
{ticker: TSLA},
{ticker: AAPL},
{ticker: TSLA},
{ticker: META},
我想要制作一个图表,显示每个时间段内唯一标签的数量,例如1分钟。
英文:
I have Prometheus counter input_requests
for requests with label ticker
:
{ticker: TSLA},
{ticker: AAPL},
{ticker: TSLA},
{ticker: META},
I want to do the chart with unique labels number in each period of time, for example 1 minute.
答案1
得分: 1
以下是翻译好的内容:
以下PromQL查询应返回标签“ticker”在过去一分钟内的唯一值的数量:
count(
count(last_over_time(input_requests[1m])) by (ticker)
)
此查询的工作方式如下:
-
内部的
last_over_time(input_requests[1m])
返回每个时间序列的最后一个值,该时间序列具有名称为input_requests
的标签,并且在过去一分钟内。请参阅last_over_time()文档。 -
然后内部的
count(...) by (ticker)
按每个唯一的“ticker”值对步骤1中返回的时间序列进行分组,返回唯一时间序列的数量。请参阅count()文档。 -
然后外部的
count()
返回从步骤2中返回的系列数量。实际上,这是在过去一分钟内看到的唯一“ticker”值的数量。
请注意,上述查询在Grafana中的每个图上的每个点都是独立计算的,例如,它返回在图上每个点结束时在过去一分钟内看到的唯一“ticker”值的数量。如果您希望获得具有每分钟步长的图表,则有以下选项:
- 在图表设置中将“最小步长”设置为“1m”(例如,一分钟)。
- 使用Prometheus子查询的以下方式:
last_over_time(
count(
count(last_over_time(input_requests[1m] offset -1m)) by (ticker)
)[1m:1m]
)
它将原始查询转换为子查询,该子查询使用1分钟的步长计算,然后外部的last_over_time(...[1m:1m])
返回具有1分钟步长的图表。该查询添加了负的offset,以显示给定分钟内看到的“ticker”值的数量。如果未设置offset
,则查询将在图上的每个1分钟步长上显示上一分钟的每个1分钟步长的“ticker”值的数量。
英文:
The following PromQL query should return the number of unique values for the label ticker
over the last minute:
count(
count(last_over_time(input_requests[1m])) by (ticker)
)
This query works in the following way:
-
The inner
last_over_time(input_requests[1m])
returns the last value per each time series withinput_requests
name over the last minute. See last_over_time() docs. -
Then the inner
count(...) by (ticker)
returns the number of unique time series from the step 1 grouped per each uniqueticker
value. See count() docs. -
Then the outer
count()
returns the number of series returned from the step 2. This is actually the number of uniqueticker
values seen during the last minute.
Please note that the query above is calculated independently per each point on the graph if the query used in Grafana, e.g. it returns the number of unique ticker
values during the last minute ending at every point on the graph. If you want obtaining a graph with per-minute step, then the following options exist:
- Set the
min step
in the graph settings to1m
(e.g. one minute). - To use Prometheus subqueries in the following way:
last_over_time(
count(
count(last_over_time(input_requests[1m] offset -1m)) by (ticker)
)[1m:1m]
)
It converts the original query to a subquery, which is calculated with 1 minute step, and then the outer last_over_time(...[1m:1m])
returns a graph with 1-minute steps. The query adds negative offset in order to show the number of ticker
values seen per the given minute. If the offset
isn't set, then the query would show the number of ticker
values for the previous minute per each 1-minute step on the graph.
答案2
得分: 1
count(changes(input_requests[1m]))
英文:
count(changes(input_requests[1m]))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论