Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PromQL if/else like expression

I'm trying to calculate easter sunday in PromQL using Gauss's Easter algorithm (I need to ignore some alert rules on public holidays).

I can calculate the day, but I'm having a problem with the month as I need something like an if/else expression. My recording rule easter_sunday_in_april returns 1 if eastern is in april and 0 if it is in march.

(How) can I express the following in PromQL?

if(easter_sunday_in_april > 0)
    return 4
else
    return 3

For the sake of completeness, I attach my recording rules here:

- 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
like image 999
Mirco Avatar asked Dec 14 '25 05:12

Mirco


2 Answers

The

if(easter_sunday_in_april > 0)
    return 4
else
    return 3

can be expressed as the following PromQL query:

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

It uses and and or logical operators and on() modifier.

P.S. This query can be expressed in more easy-to-understand form with MetricsQL via if and default operators:

(4 if (easter_sunday_in_april > 0)) default 3

MetricsQL is PromQL-like query language provided by VictoriaMetrics - the project I work on.

like image 122
valyala Avatar answered Dec 16 '25 21:12

valyala


Think I found a way:

((easter_sunday_day_of_month_temp > bool 31 ) +3)

easter_sunday_day_of_month_temp returns the "raw" day of month of easter sunday (1-31: day in march, > 31 day in april, we must calculate modulo 31 to get the day in april).

So if easter_sunday_day_of_month_temp > bool 31 is true, it returns 1 and I add 3 to get 4 (April), otherwise, I return 3 for March.

Edit: Please proof me wrong or show me a better solution :-) otherwise I'll accept mine in two days.

like image 30
Mirco Avatar answered Dec 16 '25 23:12

Mirco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!