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
The common case of adding all data labels can be achieved by omitting the 2nd argument of the info function entirely, simplifying the example even more: info(rate(http_server_request_duration_seconds_count[2m]))
Discussions

promql - Prometheus count specific value occurrences - Stack Overflow
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: More on stackoverflow.com
🌐 stackoverflow.com
Prometheus: Count metric value over a period of time - Stack Overflow
There is another Prometheus-like system, which allows counting the number of raw samples with the given value on the specified lookback window - VictoriaMetrics (I'm the core developer of this system). It provides count_eq_over_time function for this task. For example, the following MetricsQL ... More on stackoverflow.com
🌐 stackoverflow.com
Help with this simple query
For each function? More on reddit.com
🌐 r/PrometheusMonitoring
4
1
November 25, 2023
Calculating the Avg with Gaps in Data
I've had to do this for work multiple times and Prometheus isn't the correct tool unfortunately. It isn't made to store high cardinality fields like IP addresses. As you're using sFlow, this is more log-based and OLAP databases are more appropriate. But trying to answer your question: avg_over_time((sflow_asn_bps OR on() vector(0) )[10h]) and taking the instant value (haven't tested it but you'd complete the timeseries with zero values then doing the average) would summing sum_over_time and dividing by 10 hours work? More on reddit.com
🌐 r/PrometheusMonitoring
4
2
November 29, 2024
🌐
client_python
prometheus.github.io › client_python › instrumenting › counter
Counter | client_python
April 9, 2026 - If your value can go down, use a Gauge instead. from prometheus_client import Counter c = Counter('my_failures', 'Description of counter') c.inc() # Increment by 1 c.inc(1.6) # Increment by given value If there is a suffix of _total on the metric name, it will be removed.
🌐
Prometheus
prometheus.io › docs › prometheus › latest › querying › examples
Query examples | Prometheus
Assuming this metric contains one time series per running instance, you could count the number of running instances per application like this:
🌐
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 - 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: ... This will give you the count of requests grouped by the status code. ... Get notified with a radically better infrastructure monitoring platform. Explore more ... This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. ... Prometheus counters are metrics that only increase or reset to zero.
🌐
GitConnected
levelup.gitconnected.com › prometheus-counter-metrics-d6c393d86076
Working With Prometheus Counter Metrics | Level Up Coding
February 28, 2022 - In this example, I prefer the rate variant. I think seeing we process 6.5 messages per second is easier to interpret than seeing we are processing 390 messages per minute. The Prometheus counter is a simple metric, but one can create valuable insights by using the different PromQL functions which ...
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)
Find elsewhere
🌐
HexDocs
hexdocs.pm › prometheus_ex › Prometheus.Metric.Counter.html
Prometheus.Metric.Counter — Prometheus.ex v5.1.0
Increments the counter identified by spec by value. Raises Prometheus.InvalidValueError exception if value isn't a positive number.<br> Raises Prometheus.UnknownMetricError exception if a counter for spec can't be found.<br> Raises Prometheus.InvalidMetricArityError exception if labels count mismatch.
🌐
INNOQ
innoq.com › en › blog › 2019 › 05 › prometheus-counters
Prometheus Counters and how to deal with them – INNOQ
May 20, 2019 - A range vector can be seen as a continuous subset of the instant vector, in our example all values of the orders_created_total instant vector within the last 5 minutes. The range is defined in square brackets and appended to the instant vector selector (the counter name in our case). If we execute this query, we would expect to get the value 60 as a result, because our counter is increased by 1 every 5 seconds over the last 5 minutes. What we really get is something like 59.035953240763114. How come? Prometheus scrapes its targets on a regular basis.
🌐
Mkaz
mkaz.me › blog › 2023 › simple-prometheus-queries-for-metrics-inspection
Simple Prometheus queries for metrics inspection | Michal Kazmierczak
A fine example is a metric counting HTTP requests having path, method and response_code labels. Let’s consider a scenario in which five paths are observed with three methods and three response codes.
🌐
Coralogix
coralogix.com › home › promql tutorial: 5 tricks to become a prometheus god
PromQL Tutorial: 5 Tricks to Become a Prometheus God
June 3, 2025 - ... PromQL has two operators for counting up elements in a time series. Count() simply gives the total number of elements. Count_values() gives the number of elements within a time series that have a specified value.
🌐
OneUptime
oneuptime.com › home › blog › how to count unique label values in prometheus
How to Count Unique Label Values in Prometheus
December 17, 2025 - However, understanding how many unique values exist for a given label is crucial for cardinality management and query optimization. This guide shows you how to count unique label values using PromQL. Use count(group by (label_name) (metric_name)) to ...
🌐
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 - Be mindful of this to avoid overwhelming your Prometheus server. Misleading Aggregations: Ensure your by() clauses make logical sense to avoid misleading aggregations. Double-check that the labels used align with your intended analysis. The count_values() function offers an alternative approach to counting unique label values:
🌐
Logz.io
logz.io › home › blog › how to › an intro to promql: basic concepts & examples
An Intro to PromQL: Basic Concepts & Examples | Logz.io
September 2, 2023 - There are the metric types of metrics and the data types of PromQL expressions. ... Counters give the absolute value of something, such as prometheus_http_requests_total or prometheus_sd_consul_rpc_duration_seconds_count.
🌐
MetricFire
metricfire.com › blog › what-are-prometheus-functions
What are Prometheus Functions? | MetricFire
June 25, 2024 - As another example, you can use the increase() Prometheus function to count the number of HTTP requests over the past 5 minutes, e.g.:
🌐
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.
🌐
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 - As another example, you can use the increase() Prometheus function to count the number of HTTP requests over the past 5 minutes, e.g.:
🌐
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