Chapter 17. Recording Rules

The HTTP API is not the only way in which you can access PromQL. You can also use recording rules to have Prometheus evaluate PromQL expressions regularly and ingest their results. This is useful to speed up your dashboards, provide aggregated results for use elsewhere, and to compose range vector functions. Other monitoring systems might call their equivalent feature standing queries or continuous queries. Alerting rules (covered in Chapter 18) are also a variant of recording rules. This chapter will show you how and when to use recording rules.

Using Recording Rules

Recording rules go in separate files from your prometheus.yml, which are known as rule files. As with prometheus.yml, rule files also use the YAML format. You can specify where your rule files are located using the rule_files top-level field in your prometheus.yml. For example, Example 17-1 loads a rule file called rules.yml, in addition to scraping two targets.

Example 17-1. prometheus.yml scraping two targets and loading a rule file
global:
  scrape_interval: 10s
  evaluation_interval: 10s
rule_files:
 - rules.yml
scrape_configs:
 - job_name: prometheus
   static_configs:
    - targets:
      - localhost:9090
 - job_name: node
   static_configs:
    - targets:
      - localhost:9100

Similar to the files field of file_sd_configs, as covered in “File”, rule_files takes a list of paths, and you can use globs in the filename. Unlike file service discovery, rule_files does not use inotify nor does it ...

Get Prometheus: Up & Running now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.