Hello fellow developers,

We have already seen how to use **Prometheus **and **Grafana for **monitoring nodes in previous article. Now whenever we use prometheus, we don’t just use it’s metrics directly but we create complex queries based on our need and requirements. These expressions can be very big and hard to use frequently, so what if we can use it just like other metrics? Let’s see how we can achieve it.

What are Recording Rules?

Recording rules allows us to precompute frequently needed or computationally expensive expressions and save their result as a new set of time series. Querying the precomputed result will then often be much faster than executing the original expression every time it is needed.

This is especially useful for dashboards, which need to query the same expression repeatedly every time they refresh. Recording and alerting rules exist in a rule group.

Let’s take an example, what if I wanted to monitor percentage of free node memory frequently, the expression for it is a bit complex and looks like this

100 - (100 * node_memory_MemFree_bytes / node_memory_MemTotal_bytes)

I can’t copy paste this huge expression every time that I want to use it. I need an easier way to access the value of this expression and that’s where the **Recording Rules **come into play.

How to create Recording Rules?

Assuming Prometheus and node exporters are already running, we create a prometheus_rules.yml containing the necessary rule statements and then have Prometheus load the file via the rule_files field in the prometheus configuration i.e., prometheus.yml.

We first navigate to the prometheus folder, create a rules file and add the recording rules in that file.

cd /usr/local/bin/prometheus
nano prometheus_rules.yml

Image for post

prometheus_rules.yml

The rules field specify all the recording rules added, I have added 2 recording rules one for free node memory percentage and another for free node filesystem percentage. The expr field holds the actual expression.

Once we have created the recording rules, we need to check if the rules have any faults or syntax errors or indentation problem, we can execute this command to verify.

#programming #coding #ubuntu #prometheus #grafana

Create Recording Rules in Prometheus
4.15 GEEK