【问题标题】:PromQL if/else like expressionPromQL if/else like 表达式
【发布时间】:2020-10-05 08:21:56
【问题描述】:

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

我可以计算日期,但我遇到了月份问题,因为我需要 if/else 表达式之类的东西。我的记录规则 easter_sunday_in_april 如果东部是四月返回 1,如果是三月返回 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

【问题讨论】:

    标签: prometheus promql


    【解决方案1】:

    我想我找到了办法:

    ((easter_sunday_day_of_month_temp > bool 31 ) +3)
    

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

    所以如果easter_sunday_day_of_month_temp > bool 31 为真,它返回 1,我加 3 得到 4(四月),否则,我返回三月的 3。

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

    【讨论】:

      【解决方案2】:

      来自 Julien Pivoto 的 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
      

      【讨论】:

        【解决方案3】:

        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 operatorson() modifier

        附:这个查询可以用MetricsQL通过ifdefault运算符以更易于理解的形式表达:

        (4 if (easter_sunday_in_april > 0)) default 3
        

        【讨论】:

          猜你喜欢
          • 2022-12-11
          • 1970-01-01
          • 2012-10-26
          • 1970-01-01
          • 2019-09-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多