2

i use this query rule for alert:

- 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 }}%"

But the value is float (default of PromQL), i want to format it (the picture below, can i change it to show only 90%), how can i do it ?

enter image description here

Thank you for reading this.

Bình Bùi
  • 67
  • 8

1 Answers1

4

Prometheus templating language is based on the Go templating system. There are many examples in the documentation.

In your specific case you would use:

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

There are also some builtin functions in Prometheus that can be of interest like humanizePercentage:

- alert: HostOutOfMemory
  expr: (1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) > 0.9
  ...
  annotations:
    description: Host memory is {{ $value | humanizePercentage }}
Michael Doubez
  • 5,937
  • 25
  • 39
  • I do not know why, but when I use `{{ $value | humanizePercentage }}` and my value is like `11.76470588235294`, then the output is `1176%`. – Rohlik Jun 20 '22 at 10:26
  • 2
    humanizePercentage expects a fraction ration (between 0 and 1). See https://prometheus.io/docs/prometheus/latest/configuration/template_reference/#numbers – Michael Doubez Jun 22 '22 at 11:56