PromQL: Subqueries and Vector Matching
Vector matching joins two series that share labels. It lets one query divide errors by requests for each route.
Error rate per route
Errors divided by requests, matched on route.
sum by (route) (rate(http_requests_total{job="shopsentry",status="500"}[5m])) / on(route) sum by (route) (rate(http_requests_total{job="shopsentry"}[5m]))A subquery over ten minutes
The inner query runs first, then the outer query reads its results.
avg_over_time(rate(http_requests_total{job="shopsentry"}[5m])[10m:1m])Share of traffic per route
One total matches many routes, so group_left is required for the match.
sum by (route) (rate(http_requests_total{job="shopsentry"}[5m])) / on() group_left sum(rate(http_requests_total{job="shopsentry"}[5m]))Example
sum by (route) (rate(http_requests_total{job="shopsentry",status="500"}[5m])) / on(route) sum by (route) (rate(http_requests_total{job="shopsentry"}[5m]))Key points
- Vector matching pairs series from two sides of an operator.
- Use on to match only on the labels you name.
- Use ignoring to match on every label except the ones you name.
- Use group_left when one series on the right matches many on the left.
- A subquery runs a query over a range and returns its results.
- Write a subquery as a query followed by a range in square brackets.
- An error rate is errors divided by requests, per route.
In this example, the left side counts error speed per route and the right side counts total speed per route. The on(route) match divides each route by its own total, so every route gets its own error rate. The numbers are the real rates from the live ShopSentry lab.
Note
Sides with no matching partner drop out of the result, silently.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
What does on(route) do in a division?