首页 > 解决方案 > Prometheus - 基于标签算法的条件

问题描述

假设我有一个指标my_metric,并且想根据 label 计算当前指标与两天前的指标之间的差异my_label。有没有办法在 PromQL 中做到这一点而不对标签值进行硬编码?

my_metric{exported_job="my_job",instance="dr01:9091",job="pushgateway",my_label="2020-05-01"}   10
my_metric{exported_job="my_job",instance="dr01:9091",job="pushgateway",my_label="2020-05-02"}   20
my_metric{exported_job="my_job",instance="dr01:9091",job="pushgateway",my_label="2020-05-03"}   60

假设我们在2020-05-01- 预期的输出是60-10 = 50.

换句话说 - 我需要一个查询,在 SQL 中是这样的:

SELECT m_2.value - m_1.value FROM my_metric m_1, my_metric m_2 
WHERE diff_date('day', m_2.my_label, m_1.my_label) = 2

标签: prometheuspromql

解决方案


这将为您提供调用查询和度量值的时间与前 2 天之间的差异:

my_metric{exported_job="my_job",instance="dr01:9091",job="pushgateway"}
- ignoring(my_label) my_metric{exported_job="my_job",instance="dr01:9091",job="pushgateway"} offset 2d

您可以在此处阅读有关 offset 关键字的更多信息: https ://prometheus.io/docs/prometheus/latest/querying/basics/#offset-modifier


推荐阅读