The way I sorted this problem was doing the following prometheus query for Grafana. This query allows me to identify the total_processing_time consumed by an individual customer across all PODs in a K8s cluster within a given time range. The catch is that this query handles the following corner cases:

  • If the querying time_range encompasses only the last 1h but there are older timeseries that were started before that, the query below "offsets to zero" these timeseries by removing the value of the first measurement point within the time_range. This way these series are considered as if they have started within the time range.

  • If a new timeseries (due to a new POD) show up within the querying time_range, and the first measurement value is greater than 0, then the first measurement value of the new timeseries is added to the calculation to fulfill the gap from the increment function.

sum(
    # Calculates the difference between the last measurement 
    # and the first measurement in the given time_range for each 
    # timeseries, this helps identify how much additional resources
    # were consumed in the given time_range
    increase(
        total_processing_time{customer_id="$customer_id"}[$__range]
    )
    # Adds to the difference the minimum value of each timeseries, 
    # this helps to account for the timeseries that started within 
    # the given time_range with the first measurement greater than 0
    # (not considered by the previous increase function)
    + min_over_time(
        total_processing_time{customer_id="$customer_id"}[$__range]
    )
)
# Removes the timeseries measurements being carried over from before 
# the start of the time_range, this offsets the running timeseries
# as if they had started from zero within the time_range
- sum(
    total_processing_time{customer_id="$customer_id"} @ start() or vector(0)
)
Answer from João Pedro Schmitt on Stack Overflow
🌐
Reddit
reddit.com › r/prometheusmonitoring › how to deal with increase function and no data points
r/PrometheusMonitoring on Reddit: How to deal with Increase function and no data points
September 3, 2021 -

I need to use the increase function, but the metric is very rare and when service restarts the counter is null for long time. The increase metric doesn’t play nicely with the null value and I can find a solution to consider null as zero.

Increase(my_counter[1h])

Works only if there are no null data points. Any other query I tried do not work like

Increase(my_metrics[1h] or vector(0))
Prometheus Counters - and how to deal with them May 20, 2019
r/PrometheusMonitoring
7y ago
Weird "No data points found" error, console works but not graph Aug 9, 2018
r/PrometheusMonitoring
8y ago
Avoid reading zero values after a restart Feb 3, 2022
r/PrometheusMonitoring
4y ago
Counter reset after target restart Feb 22, 2023
r/PrometheusMonitoring
3y ago
More results from reddit.com
Top answer
1 of 3
6

The way I sorted this problem was doing the following prometheus query for Grafana. This query allows me to identify the total_processing_time consumed by an individual customer across all PODs in a K8s cluster within a given time range. The catch is that this query handles the following corner cases:

  • If the querying time_range encompasses only the last 1h but there are older timeseries that were started before that, the query below "offsets to zero" these timeseries by removing the value of the first measurement point within the time_range. This way these series are considered as if they have started within the time range.

  • If a new timeseries (due to a new POD) show up within the querying time_range, and the first measurement value is greater than 0, then the first measurement value of the new timeseries is added to the calculation to fulfill the gap from the increment function.

sum(
    # Calculates the difference between the last measurement 
    # and the first measurement in the given time_range for each 
    # timeseries, this helps identify how much additional resources
    # were consumed in the given time_range
    increase(
        total_processing_time{customer_id="$customer_id"}[$__range]
    )
    # Adds to the difference the minimum value of each timeseries, 
    # this helps to account for the timeseries that started within 
    # the given time_range with the first measurement greater than 0
    # (not considered by the previous increase function)
    + min_over_time(
        total_processing_time{customer_id="$customer_id"}[$__range]
    )
)
# Removes the timeseries measurements being carried over from before 
# the start of the time_range, this offsets the running timeseries
# as if they had started from zero within the time_range
- sum(
    total_processing_time{customer_id="$customer_id"} @ start() or vector(0)
)
2 of 3
2

If you have access to the code where the metric job_invocation_total is created and the cardinality of the metric labels is bound, you can initialize the counter with the value 0.

If this is not the case, you can create a recording rule that is either 0 or equal to the metric job_invocation_total.

Discussions

increase() should consider creation of new timeseries as reset
Right now if a time series didn't exist and comes into existence with value 1, increase() returns 0 since Prometheus doesn't know if the counter actually was increased or simply scraped for the fir... More on github.com
🌐 github.com
26
May 27, 2016
kubernetes - How to provide default values for values in a Prometheus timeseries that are null - Stack Overflow
Me and my team want to write a PromQL query that is true every time a new Helm Chart is deployed in our K8S cluster, which we can use to create an alert. We are scraping Kube metrics, and are scrap... More on stackoverflow.com
🌐 stackoverflow.com
Time series: how to set nulls as zero?
What Grafana version and what operating system are you using? Docker Grafana 8.1.5 What are you trying to achieve? Plot a Prometheus Counter with various labels as a time series, and fill all null/missing values as zeros. How are you trying to achieve it? Using time series graph. More on community.grafana.com
🌐 community.grafana.com
9
2
October 1, 2021
PromQL won't handle null values during vector matching
Hi. I have two PromQL queries in Grafana. Query 1: max_over_time(counter{label="label1"}[5m]) Query 2: max_over_time(counter{label="label1"}[5m] offset 10m) There’s an exact match between the labels in both queries, so I don’t believe I need to use the on() function. More on community.grafana.com
🌐 community.grafana.com
1
0
March 18, 2021
🌐
GitHub
github.com › prometheus › prometheus › issues › 2659
increase function returns null for some data points, resulting in gaps · Issue #2659 · prometheus/prometheus
April 26, 2017 - I have complete data for "mymetric" in prometheus for every second, with no nulls. I use increase function on a counter metric over a period of 1m to see the increase for every minute: increase(mymetric[1m]). I expect a value of the increase from first to last data point of each minute.
Author: prometheus
🌐
GitHub
github.com › prometheus › prometheus › issues › 1673
increase() should consider creation of new timeseries as reset · Issue #1673 · prometheus/prometheus
May 27, 2016 - Right now if a time series didn't exist and comes into existence with value 1, increase() returns 0 since Prometheus doesn't know if the counter actually was increased or simply scraped for the fir...
Author: prometheus
🌐
Medium
nklya.medium.com › promql-how-to-return-0-instead-of-no-data-9e49f7ccb80d
PromQL / How to return 0 instead of 'no data' - Nicolai Antiferov
October 26, 2025 - Explanation: Prometheus uses label matching in expressions. If your expression returns anything with labels, it won't match the time series generated by vector(0).
🌐
Stack Overflow
stackoverflow.com › questions › 76772793 › how-to-provide-default-values-for-values-in-a-prometheus-timeseries-that-are-nul
kubernetes - How to provide default values for values in a Prometheus timeseries that are null - Stack Overflow
There are many StackOverflow posts, Medium posts & GitHub issues stating that if we add or on() vector(0), this will provide us with default values of 0 in place of nulls. This does not work for our query as our query will always have a value - because we are using kube_deployment_labels{label_helm_sh_chart=~"rideshur-web-.*"}, the wildcard means that as soon as the timeseries for rideshur-web-0.4.1 reduces, the timeseries for rideshur-web-0.4.2 begins to exists/increases.
🌐
Grafana
community.grafana.com › graph (old) panel
Time series: how to set nulls as zero? - Graph (Old) Panel - Grafana Labs Community Forums
October 1, 2021 - What Grafana version and what operating system are you using? Docker Grafana 8.1.5 What are you trying to achieve? Plot a Prometheus Counter with various labels as a time series, and fill all null/missing values a…
🌐
Grafana
community.grafana.com › prometheus
PromQL won't handle null values during vector matching - Prometheus - Grafana Labs Community Forums
March 18, 2021 - Hi. I have two PromQL queries in Grafana. Query 1: max_over_time(counter{label="label1"}[5m]) Query 2: max_over_time(counter{label="label1"}[5m] offset 10m) There’s an exact match between the labels in both queries, …
Find elsewhere
🌐
Google Groups
groups.google.com › g › prometheus-users › c › uhJ9UUAF3Hg
First value of increase(Counter[Time]) doesn't count
July 4, 2023 - This will allow Prometheus to know when a counter started at 0 so it can do appropriate rate/increase calculations for newly created series. ... -- You received this message because you are subscribed to the Google Groups "Prometheus Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-use...@googlegroups.com.
🌐
DoiT
doit.com › home › blog › making peace with prometheus rate()
Making peace with Prometheus rate() | DoiT
February 17, 2023 - Now if only we could tell Prometheus to include that extra scrape that falls between the c̶h̶a̶i̶r̶s̶ buckets… ... To get the increase over 60 seconds, we ask P8s to calculate one for 75 seconds (with that extra sample that usually falls between the buckets).
🌐
Google Groups
groups.google.com › g › prometheus-developers › c › 0-R7BOhSN0c
Sum by null - unexpected result
Prometheus has no notion of null, what it does have is empty vectors. When you are using a binary operation such as + we look for matching samples on both sides, and if there's no match then that sample is ignored. Any change here (aside from being massively breaking) would have to make sense ...
🌐
SigNoz
signoz.io › guides › how to handle null values in prometheus time series data
How to Handle Null Values in Prometheus Time Series Data | SigNoz
October 14, 2024 - For some types of data, zero might not be an appropriate replacement for a null value. Always consider the nature of your metrics when deciding how to handle null values. For counters, which only increase over time, setting nulls to zero might not be appropriate as it could indicate a reset.
🌐
GitHub
github.com › prometheus › prometheus › issues › 15543
When computing, how to handle null values. · Issue #15543 · prometheus/prometheus
December 6, 2024 - Proposal I have two metrics, A and B. I want the result of A + B to be as follows: for time points where A does not exist, the result should be the value of B at that time point; for time points where B does not exist, the result should ...
Author: prometheus
🌐
Prometheus
prometheus.io › docs › prometheus › latest › querying › functions
Query functions | Prometheus
increase acts on histogram samples by calculating a new histogram where each component (sum and count of observations, buckets) is the increase between the respective component in the first and last native histogram in v. However, each element in v that contains a mix of float samples and histogram samples within the range, will be omitted from the result vector, flagged by a warn-level annotation.
🌐
GitHub
github.com › prometheus › prometheus › issues › 13570
Variant of `increase` function that assumes uninitialized counters start at zero 0, like VictoriaMetric's increase_pure · Issue #13570 · prometheus/prometheus
February 11, 2024 - Variant of increase function that assumes uninitialized counters start at zero 0, like VictoriaMetric's increase_pure#13570 ... It seems to solve the problem discussed in #1673 and would be easier to adopt than the upcoming created timestamp feature · Put another way, our use-case is we want to fire an alert in grafana anytime a prometheus counter is increased.
Author: prometheus
🌐
Reddit
reddit.com › r/prometheusmonitoring › promql to detect null values - not absent
r/PrometheusMonitoring on Reddit: Promql to detect null values - not absent
September 7, 2023 -

I have metrics like these :

    up{client=“clientA”, job=“admin”}
    up{client=“clientA”, job=“customer”}
    up{client=“clientB”, job=“admin”}
    up{client=“clientB”, job=“customer”}
    up{client=“clientC”, job=“admin”}
    up{client=“clientD”, job=“admin”}

These metrics are from different jobs. I am trying to find clients who have no customer job. The job customer doesn't scrape any target for clientC and clientD hence there is no metric at all. Absent is not working in this case as the metric was not present at all.

🌐
The Mail Archive
mail-archive.com › prometheus-users@googlegroups.com › msg07656.html
[prometheus-users] Default value of zero when metric is null in a formula
April 4, 2021 - Or I guess the more general question ... value may go NULL but you still want a metric returned so you can make use of that. Thanks! Keith -- You received this message because you are subscribed to the Google Groups "Prometheus Users" group. To unsubscribe from this group and stop ...
🌐
Google Groups
groups.google.com › g › prometheus-users › c › 4sD0nyRcS-w
Null value in alerts
I'm having trouble setting up an alert that will send a notification when a value is different from 0 and the value is missing (i.e. null).