How to get the difference between two Prometheus metrics in PromQL
Codemia Knowledge Hub ·
In PromQL you can apply arithmetic operators (addition, subtraction, multiplication, division) directly between two metrics. For two gauges, gauge_metric_one - gauge_metric_two returns the difference for every series whose labels match. For counters, compare their rate() instead of the raw values.
Metric types and why they matter
Prometheus stores metrics as time series, each identified by a metric name and a set of labels. There are four main types:
Counter: a cumulative value that only ever goes up.Gauge: a value that can go up or down arbitrarily.Histogram: a cumulative metric that counts observations in configurable buckets.Summary: similar to a histogram, but provides a total count of observed values and the sum of observed values.
Gauges can be subtracted directly. Counters only increase or reset, so the raw difference is rarely useful.
Subtracting two gauges
Say you have two gauge metrics, gauge_metric_one and gauge_metric_two. Subtract one from the other:
This returns the difference for all series where the labels match. If the labels do not match and you want to compare anyway, use the ignoring keyword:
This subtracts the two metrics while ignoring their job labels. Run the query in the Prometheus expression browser or in Grafana and the result appears as a new time series.
Comparing two counters with rate()
For counters, take the rate of change of each and subtract those:
This calculates the per-second average rate of increase of each counter over the last 5 minutes, then finds the difference.
Example use cases
- Resource utilization: compare
memory_usedandmemory_freeto understand memory saturation. - Traffic analysis: subtract
incoming_trafficfromoutgoing_trafficto monitor net network traffic.
Quick reference
| Operation | Description | Example Query |
| Direct subtraction | Subtract one gauge from another | gauge_metric_one - gauge_metric_two |
| Rate difference | Difference of rates for counters | rate(metric_one[5m]) - rate(metric_two[5m]) |
| Ignoring labels | Subtract without considering specific labels | metric_one - ignoring(job) metric_two |
Related reading
- How is tf.summary.tensor_summary meant to be used?
- How many hash functions are required in a minhash algorithm
- How many kinds of Distance Function can we use?
- How many principal components to take?
- How I create new namespace in Kubernetes
- How is CPU usage calculated?
- How shall we read the Kafka topics in a given time range?
- How should I handle input data with nan values in TensorFlow?
System design interview problems on this topic
- Design a Metrics Monitoring and Alerting System: solution and walkthrough
- Design a Distributed Tracing System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.