格式化 PromQL 值

Format PromQL values

我将此查询规则用于警报:

- alert: HostOutOfMemory

    expr: (1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100 > 90

    for: 5m

    labels:

      severity: warning

    annotations:

      summary: "{{ $labels.name }} out of memory "

      description: "Host memory is {{ $value }}%"

但是这个值是float(PromQL的默认值),我想格式化它(下图,我可以把它改成只显示90%),我该怎么办?

感谢您阅读本文。

Prometheus 模板语言基于 Go templating system. There are many examples in the documentation.

在您的特定情况下,您将使用:

 description: Host memory is {{ $value | printf "%.2f%" }}.

Prometheus 中也有一些值得关注的内置函数,例如 humanizePercentage:

- alert: HostOutOfMemory
  expr: (1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) > 0.9
  ...
  annotations:
    description: Host memory is {{ $value | humanizePercentage }}