Raj Nandan Sharma

linkPractical PromQL Examples

PromQL is a powerful query language that allows you to query metrics from Prometheus. Here are some practical examples that you can use to query metrics from Prometheus.

linkWorking with counters

Let's assume we have a metric called http_request_duration_seconds_count that records the total number of HTTP requests made to a server. The metric has the following labels:

linkTime Durations

1link# Count the total number of HTTP requests in the last 1 second

2linksum(rate(http_request_duration_seconds_count))

1link# Count the total number of HTTP requests in the last 15 minutes

2linksum(rate(http_request_duration_seconds_count[15m]) * 60 * 15)

1link# Count the total number of HTTP requests in the last 1 day

2linksum(rate(http_request_duration_seconds_count[1d]) * 60 * 60 * 24)

1link# Count the total number of HTTP requests in the last 1 week

2linksum(rate(http_request_duration_seconds_count[1w]) * 60 * 60 * 24 * 7)

The idea for these queries are simple - you are calculating the rate of change of the counter metric over a specific time period and then multiplying it by the number of seconds in that time period to get the total count.

linkGroup by

1link# Count the total number of HTTP requests by method in the last 1 minute

2linksum(rate(http_request_duration_seconds_count[1m] * 60)) by (method)

1link# Count the total number of HTTP requests by status code where handler is equal to /users in the last 5 minutes

2linksum(rate(http_request_duration_seconds_count{handler = "/users"}[5m] * 300)) by (status)

linkGreater than

1link# Count the total number of HTTP requests by method in the last 1 minute if the count is greater than 50

2linksum(rate(http_request_duration_seconds_count[1m] * 60)) by (method) > 50

linkLess than

1link# Count the total number of HTTP requests by method in the last 1 minute if the count is less than 100

2linksum(rate(http_request_duration_seconds_count[1m] * 60)) by (method) < 100

linkWithin Range

1link# Count the total number of HTTP requests by method in the last 1 minute if the count is between 50 and 100

2linksum(rate(http_request_duration_seconds_count[1m] * 60)) by (method) > 50 < 100

linkRegular Expressions

Let us suppose we have rest apis for users like

1link# Count the total number of HTTP requests by handler and status code for similar handlers in the last 5 minutes

2linksum(rate(http_request_duration_seconds_count{handler =~ "/users.*"}[5m] * 300)) by (status, handler)

1link# 5xx errors for the last 1 hour for each handler and method

2linksum(rate(http_request_duration_seconds_count{status=~"5.."}[1h]) * 60 * 60) by (handler, method)

linkOffset

1link# Count the total number of HTTP requests by handler and status code for similar handlers in the last to last 5 minutes

2linksum(rate(http_request_duration_seconds_count{handler =~ "/users.*"}[5m] offset 5m) * 300) by (status, handler)

linkDelta

1link# Count the difference in the total number of HTTP requests by handler and status code for similar handlers in the last 5 minutes

2linksum(rate(http_request_duration_seconds_count{handler =~ "/users.*"}[5m] * 300) - rate(http_request_duration_seconds_count{handler =~ "/users.*"}[5m] offset 5m) * 300) by (status, handler)

linkPercentage

1link# Calculate the percentage of 5xx errors for each handler in the last 1 hour

2linksum(rate(http_request_duration_seconds_count{status=~"5.."}[1h]) * 60 * 60) / (sum(rate(http_request_duration_seconds_count[1h]) * 60 * 60) by (handler) > 0) * 100


Practical PromQL ExamplesWorking with countersTime DurationsGroup byGreater thanLess thanWithin RangeRegular ExpressionsOffsetDeltaPercentage

Home