Prometheus Query to Count Unique Label Values
To count unique label values in Prometheus, you can use the count
function along with the by
clause to aggregate metrics based on a specific label. This is useful when you want to find out how many unique values a particular label has within a given metric.
Example Scenario
Let’s say you have a metric called http_requests_total
that includes a label named instance
. You want to count how many unique instances are being tracked.
PromQL Query
Here’s how you can construct your query:
count(http_requests_total) by (instance)
Breakdown of the Query
count(http_requests_total)
: This part of the query counts the number of occurrences of thehttp_requests_total
metric across all instances.by (instance)
: This clause specifies that the counting should be grouped by theinstance
label. As a result, you will get a count ofhttp_requests_total
for each uniqueinstance
.
Getting Distinct Unique Values
If you want to get the distinct unique values of a label (rather than just counting how many times each unique value appears), you can use the count
function on the label_replace
function. Here’s an example:
count(count(http_requests_total) by (instance))
Additional Example: Counting Unique Values of a Different Label
Suppose you want to count unique values of another label, such as status
, from the same metric:
count(http_requests_total) by (status)
This will give you the count of requests grouped by the status
code.
-
How To Manage Prometheus Counters
Prometheus counters are metrics that only increase or reset to zero. They are ideal for tracking values like requests, errors, or completed tasks. Managing counters effectively ensures accurate and...
Questions -
Limitations of Prometheus Labels
Prometheus labels are a powerful feature used to add dimensional data to metrics. However, improper use or lack of understanding of their limitations can lead to inefficiencies, high resource consu...
Questions -
What Is A Bucket In Prometheus?
In Prometheus, a bucket is a concept used in histograms to organize observed values into predefined ranges. Buckets are critical for tracking and analyzing the distribution of values, such as respo...
Questions -
What Is The Job Label In Prometheus?
The job label in Prometheus organizes monitored instances or endpoints into logical groups. It is automatically added to metrics scraped from targets defined in the same scrape configuration block ...
Questions