How Can I 'Join' Two Metrics in a Prometheus Query?

Better Stack Team
Updated on December 2, 2024

Joining two metrics in Prometheus is commonly achieved through the use of the * operator or the group function, allowing you to perform operations between metrics based on their labels. Unlike traditional SQL databases, Prometheus does not have a direct JOIN function; instead, you leverage label matching and arithmetic operations to combine metric data. Here’s how to do it:

Step 1: Understand the Metrics

Let’s assume you have two metrics:

  1. http_requests_total: Counts the total number of HTTP requests, with labels like method, status, and instance.
  2. http_response_time_seconds: Measures the response time for HTTP requests, with similar labels.

Step 2: Simple Metric Operation (Multiplication)

If you want to combine these two metrics through arithmetic operations, you can simply use the * operator. For instance, if you want to calculate the total request time for each request type, you can multiply the number of requests by their average response time:

 
sum(http_requests_total) * sum(http_response_time_seconds)

However, you may need to ensure both metrics are appropriately aligned. If they have different labels, you might want to use the ignoring or on keywords.

Step 3: Using the on and ignoring Keywords

1. Using on

If both metrics share common labels (like method), you can join them based on those labels:

 
sum(http_requests_total) by (method)
  * on(method)
  sum(http_response_time_seconds) by (method)

This query sums up the total requests and the total response times for each HTTP method and then multiplies them based on the shared method label.

2. Using ignoring

If you want to join metrics while ignoring specific labels, you can use ignoring. For example, if you want to ignore the status label:

 
sum(http_requests_total)
  * ignoring(status)
  sum(http_response_time_seconds)

This will join the metrics without considering the status label, focusing only on the common labels that remain.

Step 4: Example Query

Here’s an example that combines both metrics to calculate the average response time per request:

 
sum(rate(http_requests_total[5m])) by (method)
  / sum(rate(http_response_time_seconds[5m])) by (method)

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github