Create Recording Rules in Prometheus

Create Recording Rules in Prometheus

Why do we need Recording Rules and How to create them? To include rules in Prometheus, create a file containing the necessary rule statements and have Prometheus load the file via the rule_files field in the Prometheus configuration. Rule files use YAML. The rule files can be reloaded at runtime by sending SIGHUP to the Prometheus process.

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


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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

13 Free/Low-Cost Sites to Supercharge Your Programming Self-Education

Although we still talk about programming as a standalone career, the dominance of technology in our lives makes it clear that coding is much more than a career path. In my opinion, computer science is more than a college major or a high-paid job; it’s a skill, essential for thriving in a modern-day economy. Whether you work in healthcare, marketing, business, or other fields, you will see more coding and have to deal with a growing number of technologies throughout your entire life.

How to Structure Your Programming Code

I remember my first fumble with basic on my ZX Spectrum computer back in the 1980s, ploughing through pages of basic commands and example code without any real idea of how I could write programs myself

What Is Code Golfing And Biggest Such Tournaments

Code golf is a type of recreational computer programming competition in which participants strive to achieve the shortest possible source code that implements a certain algorithm. Playing code golf is known as "golf scripting". Code golf challenges and tournaments may also be named with the ... Some code golf questions, such as those posed on general programming ...

Coding 101: Programming Language Building Blocks

This article will introduce the concepts and topics common to all programming languages, that beginners and experts must know!

Static Code Analysis: What It Is? How to Use It?

Static code analysis is a method of debugging by examining source code before a program is run. It's done by analyzing a set of code against a set (or multiple sets) of coding rules. Static code analysis and static analysis are often used interchangeably, along with source code analysis.