PromQL: Functions You Will Use Every Day
Functions reshape a query result without changing the data underneath. They round, sort, clamp, and count changes in a series.
Round the in-flight count up
Ceil rounds a gauge value up to a whole number.
ceil(shopsentry_in_flight{job="shopsentry"})Keep a value inside limits
Clamp holds a value between a floor of 0 and a ceiling of 100.
clamp(shopsentry_in_flight{job="shopsentry"}, 0, 100)Sort routes by request total
Sort_desc lists the largest totals first.
sort_desc(sum by (route) (http_requests_total{job="shopsentry"}))Count changes in the shop gauge
Changes counts how many times the gauge moved in the range.
changes(shopsentry_up{job="shopsentry"}[5m])Example
sort_desc(sum by (route) (http_requests_total{job="shopsentry"}))Key points
- A function takes a series and returns a new series.
- Use abs to strip the minus sign from a PromQL result.
- Use ceil to round a value up to the next whole number.
- Use clamp to hold a PromQL value between the floor and the ceiling you set.
- Use sort to order results from smallest to largest.
- Use sort_desc to order results from largest to smallest.
- Use changes to count how often a series changed value.
- Use resets to count how often a counter fell back to zero.
In this example, sum adds the requests for each route, and sort_desc orders the routes from busiest to quietest. The result is a ranked list the shop can read at a glance. The numbers are the real totals from the live ShopSentry lab.
Note
Functions work on the values you already have. They cannot create data that was never scraped.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
Which function orders results from largest to smallest?