Recording Rules In Prometheus
- Using the expression editor, view the uptime of all targets:
up
- Since we don’t want to alert on each individual job and instance we have, let’s take the average of our uptime instead:
avg (up)
- We do not want an average of everything, however. Next, use the
without
clause to ensure we’re not merging our targets by instance:avg without (instance) (up)
- Further refine the expression so we only see the uptime for our
forethought
jobs:avg without (instance) (up{job="forethought"})
- Now that we have our expression written, we can look into how to add this as a rule. Switch to your terminal.
- Open the Prometheus configuration file:
$ sudo $EDITOR /etc/prometheus/prometheus.yml
- Locate the
rule_files
parameter. Add a rule file atrules.yml
:rule_files: - "rules.yml"
Save and exit the file.
- Create the
rules.yml
file in/etc/prometheus
:$ sudo $EDITOR /etc/prometheus/rules.yml
- Every rule needs to be contained in a group. Define a group called
uptime
, which will track the uptime of anything that affects the end user:groups: - name: uptime
- We’re first going to define a recording rule, which will keep track of the results of a PromQL expression, without performing any kind of alerting:
groups: - name: uptime rules: - record: job:uptime:average:ft expr: avg without (instance) (up{job="forethought"})
Notice that format of
record
— this is the setup we need to use to define the name of our recording rule. Once defined, we can call this metric directly in PromQL.The
expr
is just the expression, as we would normally write it in the expression editor.Save and exit the file.
- Restart Promtheus for the rules changes to take effect:
$ sudo systemctl restart prometheus
- Return to the web UI and navigate to Status > Rules.
- Click on the provided rule — it will take us to the expression editor! Return to the Rules page when done.