Prometheus Metrics
Data Analysis
Metric Difference
Monitoring Tools
System Monitoring

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:

plaintext
gauge_metric_one - gauge_metric_two

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:

plaintext
gauge_metric_one - ignoring(job) gauge_metric_two

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:

plaintext
rate(counter_metric_one[5m]) - rate(counter_metric_two[5m])

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_used and memory_free to understand memory saturation.
  • Traffic analysis: subtract incoming_traffic from outgoing_traffic to monitor net network traffic.

Quick reference

OperationDescriptionExample Query
Direct subtractionSubtract one gauge from anothergauge_metric_one - gauge_metric_two
Rate differenceDifference of rates for countersrate(metric_one[5m]) - rate(metric_two[5m])
Ignoring labelsSubtract without considering specific labelsmetric_one - ignoring(job) metric_two

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design