How Can I 'Join' Two Metrics in a Prometheus Query?
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:
http_requests_total
: Counts the total number of HTTP requests, with labels likemethod
,status
, andinstance
.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)
-
Limitations of Prometheus Labels
Prometheus labels are a powerful feature used to add dimensional data to metrics. However, improper use or lack of understanding of their limitations can lead to inefficiencies, high resource consu...
Questions -
Prometheus Query to Count Unique Label Values
To count unique label values in Prometheus, you can use the count function along with the by clause to aggregate metrics based on a specific label. This is useful when you want to find out how many...
Questions -
How Do I Write a Prometheus Query That Returns the Value of a Label?
To write a Prometheus query that returns the value of a specific label, you need to use the basic syntax for querying metrics and utilize the appropriate label matchers. Here’s how to construct a q...
Questions -
How Can I Group Labels in a Prometheus Query?
Grouping labels in a Prometheus query allows you to aggregate metrics based on specific labels, providing a way to analyze data across different dimensions. You can use the by clause in Prometheus ...
Questions
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 usBuild 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.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github