如何查询不同pod之间的最后一个值

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

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

  1. 移除 pod 标签。可以使用聚合函数进行移除,可以选择使用或不使用。对于您的示例,我会使用以下查询:max by (project) (test_coverage{project="a"})

  2. 获取聚合结果的最后一个值,可以使用 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&#39;d use `max by (project) (test_coverage{project=&quot;a&quot;})`
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>



huangapple
  • 本文由 发表于 2023年5月29日 17:14:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356061.html
匿名

发表评论

匿名网友

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

确定