Skip to content

How to Write Recording Rules in Prometheus

A warm Hello to all the readers!

You must be trying out Prometheus in your infrastructure. While doing so, you have encountered some rules in Prometheus. Those are called recording rules and alerting rules.

Here in this article, we are going to talk about the recording rules.

What are Recording Rules?

Recording rules are those rules that allow to run PromQL expressions that otherwise take longer to compute time series data or expressions that are very frequently needed.

For example, node exporter collects metrics about memory. It measures how much is the total memory allocated to a system. How much memory is currently available. Now, if we want to get a percentage of memory that is free, we can write a PromQL expression like below:

(node_memory_MemAvailable_bytes /  node_memory_MemTotal_bytes) * 100

But as the metrics storage gets heavy more and more, the above computation will take some time for all the instances in the infrastructure. Therefore, a recording rule can be written to compute another set of metrics consisting of free memory percentage as the value.

Let’s define the recording rule.

Defining Recording Rule

  1. Edit main prometheues config to include a rules configuration file.
$ vim prometheus.yml

rule_files:
  - "prometheus_recording_rules.yml"
  1. Write the recording rule in “prometheus_recording_rules.yml” config file
$ cat prometheus_recording_rules.yml 

groups:
  - name: memory_percent
    rules:
      - record: instance:memory_free:percent
        expr: (node_memory_MemAvailable_bytes /  node_memory_MemTotal_bytes) * 100
  1. Now, visualize the new data set.
Showing Free Memory Percentage using Prometheus Recording Rules

Decoding Recording Rule Definition

groups:
  - name: memory_percent
    rules:
      - record: instance:memory_free:percent
        expr: (node_memory_MemAvailable_bytes /  node_memory_MemTotal_bytes) * 100
  • Rules are defined within a group called as a rule group.
  • memory_percent is name of the group. You can name it anything as you like :).
  • Then, multiple rules can be defined within a group. We have defined a single rule.
  • record is name of the new time series data.
  • expr is the PromQL expression that gets evaluated at regular intervals