This is the expected behavior when using a binary operator: both side must have a matching label set to be taken into account.

If you want to be able to aggregate both side and get the single one, you first must get the union of different metrics using the __name__ label:

 sum by(__name__,type)(metric_a{job=~"provision-dev"}) or on(__name__) sum by(__name__,type)(metric_b{job=~"provision-dev"})

You can cascade the aggregation operator:

sum by (type) (sum by (__name__,type)(metric_a{job=~"provision-dev"}) or on(__name__) sum by(__name__,type)(metric_b{job=~"provision-dev"}))

Finally, you can also compact everything into:

sum by (type) ({__name__=~"metric_a|metric_b",job=~"provision-dev"})
Answer from Michael Doubez on Stack Overflow
🌐
Prometheus
prometheus.io › docs › prometheus › latest › querying › operators
Operators | Prometheus
If two histogram samples are matched, only + and - are valid operations, each adding or subtracting all matching bucket populations and the count and the sum of observations. All other operations result in the removal of the corresponding element from the output vector, flagged by an info-level annotation. The + and - operations should generally only be applied to gauge histograms, but PromQL allows them for counter histograms, too, to cover specific use cases, for which special attention is required to avoid problems with unaligned counter resets.
🌐
Promlabs
promlabs.com › promql-cheat-sheet
PromLabs | PromQL Cheat Sheet
Want to learn PromQL from the ground up? Go get our self-paced in-depth PromQL training! Select latest sample for series with a given metric name: ... Available aggregation operators: sum(), min(), max(), avg(), stddev(), stdvar(), count(), count_values(), group(), bottomk(), topk(), quantile()
Discussions

Prometheus : how do i sum by with 2 different metrics - Stack Overflow
@Panic, try the following query: sum(redis_max_bytes and on(redis_url) redis_up=1) by (redis_url). The and operator works like if in PromQL, so this query will sum only those redis_max_bytes metrics, which have the associated redis_up=1 for the corresponding redis_url. More on stackoverflow.com
🌐 stackoverflow.com
prometheus - Difference between PromQL "by" and "without" unclear - Stack Overflow
I have a question about calculating response times with Prometheus summary metrics. I created a summary metric that does not only contain the service name but also the complete path and the http-m... More on stackoverflow.com
🌐 stackoverflow.com
prometheus - PromQL sum by label over time - Stack Overflow
I am trying to sum up a counter metric over time and grouped by the label applied. I've tried this sum by (searchTerm) (bot_guides_failed_total) but I want to have out in a table form like this: More on stackoverflow.com
🌐 stackoverflow.com
promql function to sum distinct for gauge value
I have some issue with trying to build promql query from metrics that are being scraped from telegraf. For example · query: my_metrics{app="telegraf"}[5m] result: {app="telegraf"} 336 @1637319417.799 336 @1637319427.803 336 @1637319437.799 510 @1637319497.799 317 @1637319507.799 317 @1637319517.799 317 @1637319527.799 317 @1637319537.799 317 @1637319547.799 317 @1637319557.799 · So I'm trying to get the sum... More on github.com
🌐 github.com
11
November 19, 2021
People also ask

What is PromQL?
PromQL (Prometheus Query Language) is the query language built into Prometheus for selecting, filtering, and aggregating time series data. You use it to write expressions that power dashboards, alerts, and ad-hoc metric analysis.
🌐
last9.io
last9.io › blog › promql-cheat-sheet
PromQL Cheat Sheet: Queries, Functions, and Labels | Last9
How to use group by in PromQL?
Place the by clause after an aggregation function like sum(), avg(), or count(). For example: sum(http_requests_total) by (service, region) This aggregates the metric and groups results by service and region.
🌐
last9.io
last9.io › blog › prometheus-group-by-label
Prometheus Group By Label: Advanced Aggregation Techniques for ...
How do I calculate a per-second rate in PromQL?
Use the rate() function with a counter metric and a time window: rate(http_requests_total[5m]). This returns the per-second average rate over the past 5 minutes. Always use rate() with counters. Never use it with gauges.
🌐
last9.io
last9.io › blog › promql-cheat-sheet
PromQL Cheat Sheet: Queries, Functions, and Labels | Last9
🌐
Reddit
reddit.com › r/prometheusmonitoring › help with promql query (sum over time)
r/PrometheusMonitoring on Reddit: Help with PromQL query (sum over time)
July 17, 2024 -

Hello,

I have this graph monitoring the bandwidth of a VLAN on a switch every 1m using SNMP Exporter, but I also what to get the total/sum data over time, so if I select the last hour it will show x amount inbound and x amount outbound.

sum by(ifName) (irate(ifHCInOctets{instance=~"192.168.200.10", job="snmp_exporter", ifName=~".*(1001).*"}[1m])) * 8

My current graph:

I'd like to duplicate and create a stat panel show how much data in total has passed over what period I choose that's all.

For the metric I'm not sure whether to use bytes(SI) or bytes(IEC), but are similar if I change to either.

Not sure how to calculate this, but I have this created for the past 1 hour.

by copying the PromQL in Grafana and changing to a stat panel and then editing to use this:

Not sure if this is ok as I'm not sure how to calculate it all, maths was never my best subject.

Any help would be great.

I think something like is close: with sum_over_time

sum by(ifName) (sum_over_time(ifHCInOctets{instance=~"192.168.200.10", job="snmp_exporter", ifName=~".*(1001).*"}[1m])) * 8

but it comes back as 85.8 Pib when it should be 85.8 TB with my calculations.

EDIT

Observium:

What Grafana shows

Top answer
1 of 2
17

This is the expected behavior when using a binary operator: both side must have a matching label set to be taken into account.

If you want to be able to aggregate both side and get the single one, you first must get the union of different metrics using the __name__ label:

 sum by(__name__,type)(metric_a{job=~"provision-dev"}) or on(__name__) sum by(__name__,type)(metric_b{job=~"provision-dev"})

You can cascade the aggregation operator:

sum by (type) (sum by (__name__,type)(metric_a{job=~"provision-dev"}) or on(__name__) sum by(__name__,type)(metric_b{job=~"provision-dev"}))

Finally, you can also compact everything into:

sum by (type) ({__name__=~"metric_a|metric_b",job=~"provision-dev"})
2 of 2
10

The following PromQL query should sum metric_a and metric_b by type:

(sum(metric_a) by (type) + sum(metric_b) by (type))
or
(sum(metric_a) by (type) unless sum(metric_b) by (type))
or
(sum(metric_b) by (type) unless sum(metric_a) by (type))

How it works:

  • The sum(metric_a) by (type) + sum(metric_b) by (type) sums time series with matching type label values on both sides of + according to matching rules
  • The sum(metric_a) by (type) unless sum(metric_b) by (type) returns sum(metric_a) by (type) results for type label values missing in sum(metric_b) by (type). See docs about unless operator.
  • The sum(metric_b) by (type) unless sum(metric_a) by (type) returns sum(metric_a) by (type) results for type label values missing in sum(metric_a) by (type).

Then results from these three queries are joined with or operator.

This query is equivalent to the query proposed by Michael: sum({__name__=~"metric_a|metric_b"}) by (type) .

P.S. This query can be simplified further when using MetricsQL:

sum(metric_a, metric_b) by (type)

This query works, since sum() function in MetricsQL accepts and sums arbitrary number of arguments.

🌐
Medium
medium.com › @suchitasharma1106 › a-comprehensive-guide-to-grouping-and-functions-in-promql-cc3c438be320
A Comprehensive Guide to Grouping and Functions in PromQL | by Suchita Sharma | Medium
October 7, 2024 - Here are some common functions and how they can be applied: Aggregation functions are used to aggregate data across multiple time-series, typically with by() or without(). sum(): Adds up the values of all selected time series.
🌐
Chronosphere
chronosphere.io › home › top 3 queries to add to your promql cheat sheet
Top 3 queries to add to your PromQL cheat sheet
April 2, 2025 - You can achieve this in PromQL by first selecting both the error rates and the total rates, and then dividing those sets of rates by each other. For example, you could count any requests that had a response status code starting with a “5” ...
🌐
Coralogix
coralogix.com › home › promql tutorial: 5 tricks to become a prometheus god
PromQL Tutorial: Basic Concepts & Examples - Coralogix
June 3, 2025 - Count_values() gives the number of elements within a time series that have a specified value. For example, we could count the number of binaries running each build version with the query: ... Sum() does what it says.
Find elsewhere
🌐
Last9
last9.io › blog › prometheus-group-by-label
Prometheus Group By Label: Advanced Aggregation Techniques for Monitoring | Last9
June 12, 2026 - group by defines which labels to keep when aggregating data. sum by combines the sum() aggregation function with group by to add up matching time series while retaining only the specified labels.
🌐
Last9
last9.io › blog › promql-cheat-sheet
PromQL Cheat Sheet: Queries, Functions, and Labels | Last9
September 12, 2024 - This cheat sheet collects practical PromQL snippets organized by use case — from quick incident queries to advanced aggregation, label manipulation, and capacity planning. When everything’s on fire and you need to know what’s going on ASAP: sum(rate(http_requests_total[5m])) by (status_code)
🌐
VictoriaMetrics
docs.victoriametrics.com › metricsql
VictoriaMetrics: MetricsQL
This function is supported by PromQL. See also stddev_over_time . sum_eq_over_time(series_selector[d], eq) is a rollup function , which calculates the sum of raw sample values equal to eq on the given lookbehind window d per each time series returned from the given series_selector .
🌐
Grafana
grafana.com › blog › inside-promql-a-closer-look-at-the-mechanics-of-a-prometheus-query
Inside PromQL: A closer look at the mechanics of a Prometheus query | Grafana Labs
October 9, 2024 - PromQL is defined in great detail in the documentation, so we won’t go too deep here, but briefly a query is built up from: Selectors, with a metric name and label matchers. For example, http_requests_total{status="200"}. Functions, such as abs to take the absolute value or rate to compute the rate of increase per second. Aggregations, like sum and max, with optional dimensions, e.g., sum by (status).
🌐
Prometheus
prometheus.io › docs › prometheus › latest › querying › functions
Query functions | Prometheus
This function has to be enabled via the feature flag --enable-feature=promql-experimental-functions.
🌐
SigNoz
signoz.io › guides › how to measure total requests with prometheus - a time-based guide
How to Measure Total Requests with Prometheus - A Time-Based Guide | SigNoz
July 25, 2024 - Use the increase() function in ... over the specified time range. To get the total requests across all instances, use the sum() function:...
🌐
Fiberplane
fiberplane.com › blog › why-are-prometheus-queries-hard
Why are Prometheus queries hard? | Fiberplane Blog
July 4, 2023 - When querying our data, we often care about some of those label dimensions and not others. In PromQL, sum by (label1, label2,…) groups the time series by the labels you specify and uses the sum function to merge series together (similar to ...
🌐
Prometheus
prometheus.io › docs › prometheus › latest › querying › examples
Query examples | Prometheus
sum by (app, proc) ( instance_memory_limit_bytes - instance_memory_usage_bytes ) / 1024 / 1024
🌐
GitHub
github.com › prometheus › prometheus › issues › 9822
promql function to sum distinct for gauge value · Issue #9822 · prometheus/prometheus
November 19, 2021 - I have some issue with trying to build promql query from metrics that are being scraped from telegraf. For example · query: my_metrics{app="telegraf"}[5m] result: {app="telegraf"} 336 @1637319417.799 336 @1637319427.803 336 @1637319437.799 510 @1637319497.799 317 @1637319507.799 317 @1637319517.799 317 @1637319527.799 317 @1637319537.799 317 @1637319547.799 317 @1637319557.799 · So I'm trying to get the sum_over_time from that gauge, but since Prometheus keeps pulling the same value until telegraf flushes the gauge from memory.
Author: prometheus
🌐
Better Stack
betterstack.com › community › questions › how-can-group-labels-in-prometheus-query
How Can I Group Labels in a Prometheus Query? | Better Stack Community
August 5, 2025 - This query sums the total requests and groups the result by the method label, giving you the total number of requests for each HTTP method.
🌐
SigNoz
signoz.io › guides › how to group labels in prometheus queries - a practical guide
How to Group Labels in Prometheus Queries - A Practical Guide | SigNoz
July 24, 2024 - group() drops specified labels without modifying values, while sum() aggregates values across the grouped labels.