Search code examples
prometheuspromql

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

Solution

  • 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.