Create Recording Rules in Prometheus

Why do we need Recording Rules and How to create them?

Vinesh
Javarevisited

--

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)

--

--

Vinesh
Javarevisited

Coder 👨‍💻, Gamer🎮 and a dog person🐶. That says a lot about me, I guess😛.