count(count by (a) (hello_info))

First you want an aggregator with a result per value of a, and then you can count them.

Answer from brian-brazil on Stack Overflow
🌐
OpenObserve
openobserve.ai › home › blog › prometheus metrics count basics
Prometheus Metrics Count Basics
September 26, 2025 - Learn how Prometheus metrics counts unique label values using basic queries and methods like `count(count by (label) (metric))`.
🌐
Prometheus
prometheus.io › docs › prometheus › latest › querying › functions
Query functions | Prometheus
Use rate for alerts and slow-moving counters, as brief changes in the rate can reset the FOR clause and graphs consisting entirely of rare spikes are hard to read. Note that when combining irate() with an aggregation operator (e.g. sum()) or a function aggregating over time (any function ending in _over_time), always take an irate() first, then aggregate.
🌐
Better Stack
betterstack.com › community › questions › prometheus-to-count-unique-label-values
Prometheus Query to Count Unique Label Values | Better Stack Community
December 2, 2024 - 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.
🌐
client_python
prometheus.github.io › client_python › instrumenting › counter
Counter | client_python
April 9, 2026 - A Counter tracks a value that only ever goes up. Use it for things you count — requests served, errors raised, bytes sent. When the process restarts, the counter resets to zero. If your value can go down, use a Gauge instead. from prometheus_client import Counter c = Counter('my_failures', ...
🌐
HexDocs
hexdocs.pm › prometheus_ex › Prometheus.Metric.Counter.html
Prometheus.Metric.Counter — Prometheus.ex v5.1.0
Increments the counter identified by spec by 1 when body executed. Read more about bodies: Prometheus.Injector.
🌐
GitConnected
levelup.gitconnected.com › prometheus-counter-metrics-d6c393d86076
Working With Prometheus Counter Metrics | Level Up Coding
February 28, 2022 - We should only use Irate with counters. Prometheus’ resets function gives you the number of counter resets over a specified time window². The following PromQL expression calculates the number of job execution counter resets over the past 5 minutes.
🌐
Last9
last9.io › blog › prometheus-functions
Prometheus Functions: How to Make the Most of Your Metrics | Last9
February 28, 2025 - Prometheus excels at data aggregation using functions like: sum, avg, min, max, count – Aggregate metrics across different dimensions.
🌐
OneUptime
oneuptime.com › home › blog › how to count unique label values in prometheus
How to Count Unique Label Values in Prometheus
December 17, 2025 - Use count(group by (label_name) (metric_name)) to count unique label values · The group aggregation operator deduplicates time series by label combinations · Use Grafana's label_values() query helper for dropdown variable population · Monitor ...
Find elsewhere
🌐
Prometheus
prometheus.io › docs › prometheus › latest › querying › operators
Operators | Prometheus
In case the threshold value is not aligned to one of the bucket boundaries of the histogram, either linear (for NHCB and zero buckets of exponential histogram) or exponential (for non zero bucket of exponential histogram) interpolation is applied to compute the estimated count of observations that remain in the bucket containing the threshold. In case when some observations get trimmed, the new sum of observation values is recomputed (approximately) based on the remaining observations. The following binary comparison operators exist in Prometheus:
Top answer
1 of 2
3

I came up with two solutions to this, choose whichever suits you best. For the purpose of simplifying things, let's assume your Prometheus scrape at 15 seconds interval and the error state lasted for 1 minute. Then, the gathered data would look like this:

state_metric 0 @t
state_metric 1 @t+15s
state_metric 1 @t+30s
state_metric 1 @t+45s
state_metric 1 @t+60s
state_metric 0 @t+75s

With changes()

This shows how many state changes were there. It would return 1 for the exemplary data above and it only gives adequate results if the gauge in question can hold exactly two possible values (for example 1 and 0).

changes(state_metric[1d])/2

changes() shows how many times the metric value has changed during the interval, while division by 2 is to compensate the state change back to normal. This is the downside of this method, which makes it only usable for detecting quick changes of state. But you probably have an alert when the error state hangs for some time, so I think this shouldn't be really a problem.

With a subquery

This is more precisely what you asked:

the number of times when metrics were 1

But there is a catch: with the exemplary data above, the query below will return you 4:

sum_over_time(count(state_metric == 1)[1d:])

[1d:] means repeat that instant query (count(state_metric == 1)) for each data point during last 1d. This is precisely the number of times when state_metric was 1 and it can be useful, for example, to calculate the downtime (just multiply by the scrape interval). Unlike the first method, this can work with any number of possible states, since you can define what you need in the condition.

2 of 2
1

I need to calculate the number of times when metrics were 1 by range variable provided in Grafana.

The following query should return the number of times the time series matching aqa_device_health_checker{env="dev", device="FOO"} series selector had value 1 on the selected time range in Grafana (aka $__range):

last_over_time(
  sum_over_time(
    aqa_device_health_checker{env="dev", device="FOO"}[$__range] offset -$__range
  )[$__range:$__range]
)

The query returns individual results per each matching time series. If you need summary result over all the matching time series, then just wrap the query above into sum():

sum(
  last_over_time(
    sum_over_time(
      aqa_device_health_checker{env="dev", device="FOO"}[$__range] offset -$__range
    )[$__range:$__range]
  )
)

Note that both queries above allow calculating the number of times the metric had 1 value if the metric could have either 0 or 1 values. If the metric can have other values, then these queries won't work as expected. Unfortunately, Prometheus doesn't provide easy to use functionality for counting the number of raw samples with some pre-defined value N. If you know beforehand the interval between samples (aka scrape_interval), then the following hack based on Prometheus subquery can be used:

count_over_time(
  (
    last_over_time(m[scrape_interval]) == N
  )[$__range:scrape_interval]
)

This query counts the number of raw samples with values equal to N on the time range $__range selected in Grafana.

If the interval between samples isn't known beforehand, then it is impossible to calculate the number of samples with a particular value in Prometheus. If you still need this functionality, then take a look at count_eq_over_time() function provided by VictoriaMetrics - this is Prometheus-like monitoring solution I work on. For example, the following query returns the exact number of samples with the value 10 over the last hour for time series m:

count_eq_over_time(m[1h], 10)
🌐
SigNoz
signoz.io › guides › how to count unique label values with prometheus queries
How to Count Unique Label Values with Prometheus Queries | SigNoz
September 6, 2024 - While count() is a fundamental PromQL function, its role in counting unique label values is limited. Understanding its usage and combining it with other functions is key to getting the most out of your Prometheus metrics.
🌐
Medium
pramodshehan.medium.com › prometheus-counter-metrics-1b0a4cbb79e1
Prometheus Counter metrics. There are three functions to calculate… | by Pramod Shehan | Medium
January 18, 2026 - Prometheus Counter metrics There are three functions to calculate the rate of increase for counter metrics. rate()- Calculates the average per-second rate of increase over the entire time window.It …
🌐
OneUptime
oneuptime.com › home › blog › how to implement prometheus counter best practices
How to Implement Prometheus Counter Best Practices
January 30, 2026 - All counters MUST end with _total. This is a Prometheus convention that makes metric types immediately recognizable. # Good http_requests_total errors_total processed_bytes_total # Bad http_requests errors_count processed_bytes
🌐
Prometheus
prometheus.io › docs › concepts › metric_types
Metric types | Prometheus
Remember, however, that these buckets ... bucket counts all observations less than or equal to the upper boundary provided as a label. With native histograms, you can look at observations within given boundaries with the histogram_fraction() function (to calculate fractions of observations) and the trim operators (to filter for the desired band of observations). See histograms and summaries for details of histogram usage and differences to summaries. NOTE: Beginning with Prometheus v3.0, the ...
🌐
MetricFire
metricfire.com › blog › what-are-prometheus-functions
What are Prometheus Functions? | MetricFire
June 25, 2024 - Summary: Similar to a histogram, ... a total count of observations and a sum of observed values. It processes the information while computing configurable quantities for a sliding window of time.
🌐
OneUptime
oneuptime.com › home › blog › how to create counter over time graph in prometheus
How to Create Counter Over Time Graph in Prometheus
December 17, 2025 - Learn how to properly visualize Prometheus counters over time using rate(), increase(), and irate() functions.
🌐
Medium
medium.com › @MetricFire › what-are-prometheus-functions-4ff7270f9bcb
What are Prometheus Functions?. Prometheus is a platform for real-time… | by MetricFire | Medium
August 10, 2023 - Summary: Similar to a histogram, ... a total count of observations and a sum of observed values. It processes the information while computing configurable quantities for a sliding window of time.
🌐
Google Groups
groups.google.com › g › prometheus-users › c › QsY_OkdH_gU
number of occurrences in a day
The only way I know to get an exact answer is to send the range vector query "foo[24h]" to the *instant* query endpoint, then filter and count the samples client-side. A range vector like that gives the raw values with their raw timestamps as stored in the TSDB. For this use case it would be nice if Prometheus were to allow certain operators to work directly on range vectors, so you could write