如何使用promq获取我在prometheus中拥有数据的天数?

How to get the number of days on which i have data in prometheus using promq?

我需要知道过去 30 天我在 prometheus 中有多少天的数据。

我目前的方法是触发 30 个即时查询,看看我每天是否有数据。

下面的查询应该可以解决问题:

count_over_time((count_over_time(your_metric[1d]) > 0)[30d:1d])

它应该 return 名称为 your_metric 的时间序列在过去 30 天内至少有一个样本的天数。

查询使用 Prometheus subquery, which can substitute multiple calls to /api/v1/query 并调用此 API。

如果您需要计算在所有时间序列匹配your_metric中至少存在一个样本的天数,则可以使用以下查询:

count_over_time((sum(count_over_time(your_metric[1d])) > 0)[30d:1d])