How Can I Group Labels in a Prometheus Query?

Better Stack Team
Updated on December 2, 2024

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 Query Language (PromQL) to group your results according to one or more labels. Here’s how to do it effectively.

Basic Syntax for Grouping

The general syntax for grouping in a Prometheus query looks like this:

 
aggregation_function(metric_name) by (label1, label2, ...)

Step-by-Step Examples

1. Counting Total Requests by Method

Suppose you have a metric called http_requests_total that counts HTTP requests. If you want to count the total number of requests grouped by the method label (e.g., GET, POST), you can use:

 
sum(http_requests_total) by (method)

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.

2. Average Response Time by Status

If you want to calculate the average response time grouped by the status label, and you have a metric called http_response_time_seconds, you can use:

 
avg(http_response_time_seconds) by (status)

This returns the average response time for each unique status code.

3. Grouping by Multiple Labels

You can also group by multiple labels. For instance, if you want to count the total requests grouped by both method and status, you can do:

 
sum(http_requests_total) by (method, status)

This query returns the total number of requests for each combination of HTTP method and status.

Step 4: Using Without and Without Keywords

1. Using without

If you want to aggregate metrics while ignoring certain labels, you can use the without keyword. For example, if you want to sum total requests but ignore the instance label, you can write:

 
sum(http_requests_total) without (instance)

This aggregates the total requests across all instances.

2. Using on and ignoring

You can also combine groupings using on and ignoring. For example, if you want to group by method but ignore the instance label:

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

This allows you to perform operations while maintaining focus on the method label.

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