PromQL if/else like 表达式

PromQL if/else like expression

我正在尝试使用 Gauss's Easter algorithm 在 PromQL 中计算复活节星期日(我需要在 public 假期忽略一些警报规则)。

我可以计算日期,但我对月份有疑问,因为我需要 if/else 表达式之类的东西。我的记录规则 easter_sunday_in_april returns 东部时间为 4 月为 1,3 月为 0。

(如何)我可以在 PromQL 中表达以下内容?

if(easter_sunday_in_april > 0)
    return 4
else
    return 3

为了完整起见,我在这里附上我的录制规则:

- record: a
    expr: year(europe_time) % 4

  - record: b
    expr: year(europe_time) % 7

  - record: c
    expr: year(europe_time) % 19

  - record: d
    expr: (19*c + 24) % 30

  - record: e
    expr: (2*a + 4*b + 6*d + 5) % 7

  - record: f
    expr: floor((c + 11*d + 22*e)/451)

  - record: easter_sunday_day_of_month_temp
    expr: 22 + d +e - (7*f)


  - record: easter_sunday_day_of_month_in_april
    expr: easter_sunday_day_of_month_temp > bool 31

  - record: easter_sunday_day_of_month
    expr: easter_sunday_day_of_month_temp % 31

我想我找到了一个方法:

((easter_sunday_day_of_month_temp > bool 31 ) +3)

easter_sunday_day_of_month_temp returns 复活节星期天月份的“原始”日期(1-31:三月的一天, > 4 月的 31 天,我们必须计算模 31 才能得到这一天四月)。

因此,如果 easter_sunday_day_of_month_temp > bool 31 为真,则 return 为 1,我加 3 得到 4(四月),否则,我 return 3 为三月。

编辑:请证明我错了或告诉我更好的解决方案:-) 否则我会在两天内接受我的。

来自 Julien Pivotto 的 PromCon 演讲: https://promcon.io/2019-munich/talks/improved-alerting-with-prometheus-and-alertmanager/

groups:
- name: Easter Meeus/Jones/Butcher Algorithm
interval: 60s
rules:
- record: easter_y
expr: year(belgium_localtime)
- record: easter_a
expr: easter_y % 19
- record: easter_b
expr: floor(easter_y / 100)
- record: easter_c
expr: easter_y % 100
- record: easter_d
expr: floor(easter_b / 4)
- record: easter_e
expr: easter_b % 4
- record: easter_f
expr: floor((easter_b +8 ) / 25)
- record: easter_g
expr: floor((easter_b - easter_f + 1 ) / 3)
- record: easter_h
expr: (19*easter_a + easter_b - easter_d - easter_g + 15 ) % 30
- record: easter_i
expr: floor(easter_c/4)
- record: easter_k
expr: easter_c%4
- record: easter_l
expr: (32 + 2*easter_e + 2*easter_i - easter_h - easter_k) % 7
- record: easter_m
expr: floor((easter_a + 11*easter_h + 22*easter_l) / 451)
- record: easter_month
expr: floor((easter_h + easter_l - 7*easter_m + 114) / 31)
- record: easter_day
expr: ((easter_h + easter_l - 7*easter_m + 114) %31) + 1

- record: public_holiday
expr: |
vector(1) and
day_of_month(belgium_localtime-86400) == easter_day
and month(belgium_localtime-86400) == easter_month
labels:
name: Easter Monday
- record: public_holiday
expr: |
vector(1) and
day_of_month(belgium_localtime-40*86400) == easter_day
and month(belgium_localtime-40*86400) == easter_month
labels:
name: Feast of the Ascension

if(easter_sunday_in_april > 0)
    return 4
else
    return 3

可以表示为如下PromQL查询:

(vector(4) and on() (easter_sunday_in_april > 0)) or on() vector(3)

它使用 andor logical operators and on() modifier

P.S。这个查询可以通过 ifdefault 运算符用 MetricsQL 以更 easy-to-understand 的形式表达:

(4 if (easter_sunday_in_april > 0)) default 3