英文:
How to query the last value across different pods
问题
I have a service to collect projects' test coverage data from their pull-requests, it was put into different pods internally by the collector. So the data will be like:
example input data:
test_coverage{project="a", pod="pod-a"} 80@1684921872
82@1684921882
test_coverage{project="a", pod="pod-b"} 90@1684982920
How can I get the last coverage value across different pods, so that I can get the value 90.
I tried last_over_time(test_coverage{project="a"}[1w])
but get 2 results with
test_coverage{project="a", pod="pod-a"} 82
test_coverage{project="a", pod="pod-b"} 90
My expectation is only the 90 be returned.
英文:
I have a service to collect projects' test coverage data from their pull-requests, it was put into different pods internally by the collector. So the data will be like:
example input data:
test_coverage{project="a", pod="pod-a"} 80@1684921872
82@1684921882
test_coverage{project="a", pod="pod-b"} 90@1684982920
How can I get the last coverage value across different pods, so that I can get the value 90.
I tried last_over_time(test_coverage{project="a"}[1w])
but get 2 results with
test_coverage{project="a", pod="pod-a"} 82
test_coverage{project="a", pod="pod-b"} 90
My expectation is only the 90 be returned.
答案1
得分: 1
-
移除
pod
标签。可以使用聚合函数进行移除,可以选择使用或不使用。对于您的示例,我会使用以下查询:max by (project) (test_coverage{project="a"})
-
获取聚合结果的最后一个值,可以使用
last_over_time
。请注意,由于参数不是即时选择器,您需要使用subquery语法。
最终的查询如下所示:
last_over_time((max by (project) (test_coverage{project="a"}))[1w:])
<details>
<summary>英文:</summary>
You need to do it in two steps:
1. Remove `pod` label. It can be done with aggregation by/without. For your example I'd use `max by (project) (test_coverage{project="a"})`
2. Take last value of aggregation with `last_over_time`. Notice that since argument is not a instant selector you need ti use [subquery][1] syntax.
Resulting query would look like this:
last_over_time((max by (project) (test_coverage{project="a"}))[1w:])
[1]: https://prometheus.io/docs/prometheus/latest/querying/basics/#subquery
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论