RousselTMLEARNING

TP · Prometheus: From Discovery to Expertise

Lab: Writing, testing and measuring recording rules

What you will learn in this TP
  • Write recording rules named after the level:metric:operations convention
  • Compute a robust error ratio and percentile
  • Test the rules with promtool
  • Measure the query performance gain
Mode :

All steps and instructions are shown. Recommended for this lab: semi-guided

From expensive queries to recorded series #

Context. Your dashboard recomputes the same aggregates on every refresh. You will record them in dedicated series, using Prometheus's own metrics, with no other dependency.

Prerequisites: Docker Compose, curl and jq  ·  Estimated time: 40 min

Create a working folder for this lab.
  1. 1. Start Prometheus

    Why? Prometheus scrapes itself, so you immediately get real HTTP metrics.

    Write docker-compose.yml and prometheus.yml. Enable hot reload and reference a rules.yml file.
  2. 2. Record a first aggregate

    Why? Staged aggregation avoids re-reading raw series at every level.

    Create rules.yml with the rate per job and handler, then the rate per job reusing the previous rule. Also create an empty tests.yml (touch tests.yml), check, start and query the new series after a minute.
  3. 3. Add a robust error ratio and a percentile

    Why? Without care, the ratio stays empty until an error exists.

    Add the 5xx error rate, the error ratio (which must be 0 with no errors) and the 95th percentile of the duration. Reload and check the health of the rules.
  4. 4. Write the unit tests

    Why? A wrong rule gives a plausible but wrong dashboard: only a test reveals it.

    In tests.yml, cover two cases: 10% errors (ratio 0.1) and no errors (ratio 0). Run the tests, then remove the or ... * 0 part of the ratio, see the second test fail, and put it back.
  5. 5. Measure the gain

    Why? An optimisation is measured.

    Compare the samples read by the original query and by the recorded series, then find the evaluation time of your rules with prometheus_rule_group_last_duration_seconds. You should see a single sample for the recorded series and several hundred for the original query (the value depends on your instance).

Validate your work

Check that each goal above is reached and that you get the result described in the introduction.

Difficulty level : ●●●○○ (3/5)

My progress

  • ⏱ Estimated time : ≈ 40 min

Sign in to track your progress and take notes. Sign in

⭐ Rate this lesson

5.0 initial rating from the teaching team

Sign in to rate this lesson →

💬 Recent discussions about this course

Ask a question about this lesson →See all discussions about this course

Course Glossary

Alerting

The process of notifying teams (via email, Slack, Teams) when a specific event, threshold breach, or anomaly is detected.

Architecture

The overall structural design of an IT system, defining its various components, their relationships, and the principles guiding its evolution.

OpenTelemetry

An open source framework providing standards, APIs, and SDKs to collect and export observability data (traces, metrics, logs) in an agnostic way.

Recommended Articles

Prometheus recording rules: speeding up your queries and dashboards

Understand and master Prometheus recording rules: evaluation, naming convention, pitfalls (ratios...

Writing good Prometheus alerting rules (and testing them)

Design useful alerts: alert on symptoms, choose for and thresholds, severity and team labels, ann...

Measuring SLIs with Prometheus

Implement availability and latency SLIs with Prometheus: required metrics, ratio queries, histogr...

Prometheus best practices: metrics, cardinality, rules and operations

Prometheus best practices: metric naming, labels and cardinality, scrape intervals, recording rul...

Thanos Ruler: rules and alerts on the global view

Evaluate recording and alerting rules across all clusters with Thanos Ruler: configuration, Alert...

Alerting on logs with the Loki Ruler

Set up LogQL alerts and recording rules with the Loki Ruler: configuration, rule files, Alertmana...

Prometheus: alerting with Alertmanager (rules, routing, silences)

Set up Prometheus alerting: alert rules, Alertmanager, routing, grouping, inhibition, silences, S...

Mimir: ruler, Alertmanager and mimirtool

Evaluate recording and alerting rules in Mimir, configure the multi-tenant Alertmanager and autom...

Transforming with VRL and testing Vector pipelines

The VRL language (Vector Remap Language): parsing logs (JSON, regex, syslog), error handling, enr...

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!