PromQL: Aggregation: sum, avg, topk
Aggregation combines many series into fewer numbers. It turns thousands of per-route series into one answer per route.
Total requests by route
This query adds requests across all status codes for each route.
sum by (route) (http_requests_total{job="shopsentry"})Average in-flight requests
Avg finds the mean of the gauge across the grouped series.
avg(shopsentry_in_flight{job="shopsentry"})The busiest routes first
Topk keeps the five largest totals and drops the rest.
topk(5, sum by (route) (http_requests_total{job="shopsentry"}))Example
sum by (route) (http_requests_total{job="shopsentry"})Key points
- Aggregation takes a set of series and returns one value per group.
- Use sum to add values across series in the same group.
- Use avg to find the mean value across series in the same group.
- Use topk to keep only the largest values in the result.
- Add by to group by the labels you name.
- Add without to group by every label except the ones you name.
- Aggregate to the level you need, and no higher.
In this example, sum adds the request counter for each route across its status labels. The result is one total per route, so the shop can see which route gets the most traffic. The numbers are the real totals from the live ShopSentry lab. The /metrics line counts Prometheus scraping the shop.
Note
A query with no by or without groups everything into one number.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
Which query gives the total requests for each route?