Prometheus Topk Query Not Showing Expected Values Fix
In this tutorial, you'll learn about Prometheus Topk Query Not Showing Expected Values Fix. We cover key concepts, practical examples, and best practices.
Your Prometheus topk(5, ...) query returns fewer than 5 results, or the results seem wrong — the top 5 are not what you expect. topk() returns the K highest values at each step, but tied values and metric availability affect the output.
The Problem
// Returns only 3 results instead of 5
topk(5, rate(http_requests_total[5m]))
If only 3 time series have data at the query time, topk(5, ...) returns 3 results. topk() cannot produce results that do not exist — it returns at most K results, potentially fewer.
Step-by-Step Fix
1. Understand topk behavior
// topk returns K series at each evaluation time
// If there are fewer than K series, it returns what exists
topk(5, rate(http_requests_total[5m]))
// To always get K results, use topk with subquery
topk(5, rate(http_requests_total[5m]) or vector(0))
2. Use topk with aggregation
// Top 5 endpoints by request rate
topk(5, sum by (endpoint) (rate(http_requests_total[5m])))
// Top 5 services by error count
topk(5, sum by (service) (rate(http_requests_total{status=~"5.."}[5m])))
3. Handle tied values
// If multiple series have the same value, topk may return more than K
// Example: 5 services all have exactly 100 req/s
// topk(3, ...) returns all 5 because they are tied
// Use bottomk to see lowest values
bottomk(3, rate(http_requests_total[5m]))
4. Use topk in recording rules
groups:
- name: topk
interval: 1m
rules:
- record: instance:top5_cpu:rate5m
expr: topk(5, rate(cpu_seconds_total[5m]))
5. Filter before topk for accuracy
// Filter out noise before applying topk
topk(10,
sum by (service) (
rate(http_requests_total{job="api"}[5m])
)
)
Expected output:
topk(3, rate(http_requests_total[5m]))
→ service-a: 450 req/s
→ service-b: 320 req/s
→ service-c: 210 req/s
(only 3 results because 4th service has no data)
Prevention Tips
- Check if enough time series exist for your K value
- Aggregate before topk for meaningful ranking
- Use topk with
or vector(0)to always get K results - Use bottomk for least-performing endpoints
- Use recording rules for frequently-used topk queries
Common Mistakes with query topk
- Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
These mistakes appear frequently in real-world PROMETHEUS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro