Advanced Query Patterns
This module covers advanced PromQL patterns including rate functions, subqueries, logical operators, and time-based functions. You will learn how to write complex queries for real-world monitoring scenarios.
Rate Functions
Rate functions calculate the per-second average rate of increase of a time series. They are essential for working with counter metrics.
The rate() Function
The rate() function calculates the per-second average rate of increase:
rate(http_requests_total[5m])
This calculates the average per-second rate of HTTP requests over the last 5 minutes.
The irate() Function
The irate() function calculates the per-second instant rate based on the last two data points:
irate(http_requests_total[5m])
Use irate() when you need the most recent rate and rate() for smoother averages.
Working with Gauges
Gauges represent values that can go up or down. Different functions are used for gauges compared to counters.
Average Over Time
Calculate the average value over a time range:
avg_over_time(container_memory_usage_bytes[5m])
Subqueries
Logical Operators
Logical operators allow you to combine multiple conditions in queries.
AND Operator
The and operator returns the left-hand side if the right-hand side has a matching time series:
# Memory usage only for containers that also have CPU metrics
container_memory_usage_bytes and container_cpu_usage_seconds_total
Mathematical Functions
PromQL includes various mathematical functions for transforming values.
Basic Math Functions
# Absolute value
abs(container_memory_usage_bytes - 1073741824)
# Square root
sqrt(container_memory_usage_bytes)
# Logarithm (natural log)
ln(container_memory_usage_bytes)
# Logarithm base 10
log10(container_memory_usage_bytes)
# Exponentiation
exp(rate(http_requests_total[5m]))
Label Manipulation
Advanced Patterns for OpenShift
CPU Utilization Percentage
Calculate CPU utilization as a percentage:
# CPU utilization per container
100 * rate(container_cpu_usage_seconds_total[5m])
# Average CPU utilization per namespace
avg(100 * rate(container_cpu_usage_seconds_total[5m])) by (namespace)
Memory Utilization Percentage
Calculate memory utilization relative to limits:
# Memory utilization percentage (if limits are available)
100 * container_memory_usage_bytes / container_spec_memory_limit_bytes
Request Rate with Error Rate
Calculate error rates:
# Error rate as percentage of total requests
100 * sum(rate(http_requests_total{status=~"5.."}[5m])) by (service) /
sum(rate(http_requests_total[5m])) by (service)
Query Optimization
Avoid Overly Long Ranges
Long ranges increase query time and memory usage:
# Good: 5-minute range
rate(http_requests_total[5m])
# Less efficient: 1-day range
rate(http_requests_total[1d])
Debugging Queries
Summary
In this module, you learned:
-
Rate functions:
rate(),irate(), andincrease()for counters -
Functions for gauges:
avg_over_time(),max_over_time(),delta(),deriv() -
Subqueries for time-based analysis
-
Logical operators:
and,or,unless -
Time-based functions:
time(),timestamp(),offset -
Mathematical functions for value transformation
-
Label manipulation functions
-
Advanced patterns for OpenShift monitoring
-
Query optimization techniques
-
Debugging strategies for complex queries
You now have the knowledge to write sophisticated Prometheus queries for real-world monitoring scenarios. Practice these patterns in your OpenShift environment to become proficient with PromQL.