- 1. PromQL Fundamentals
- 2. Selectors and Matching
- 3. Core Functions
- 4. Aggregation Operators
- 5. Practical Query Patterns
- 6. Writing Alerting Rules
- 7. Grafana Dashboard Queries
- 8. Quiz
- Quiz

1. PromQL Fundamentals
PromQL (Prometheus Query Language) is the query language used to query and analyze time-series data in Prometheus. Both Grafana dashboards and alerting rules are written in PromQL.
4 Data Types
| Type | Description | Example |
|---|---|---|
| Instant Vector | A set of time series with the same timestamp | http_requests_total |
| Range Vector | Time series over a time range | http_requests_total[5m] |
| Scalar | A single numeric value | 3.14 |
| String | A string value (rarely used) | "hello" |
Metric Types
Counter — Monotonically increasing value (resets to 0) : http_requests_total
Gauge — Can go up or down : temperature, memory_usage
Histogram — Distribution measurement (bucket) : http_request_duration_seconds
Summary — Directly computes quantiles : go_gc_duration_seconds
2. Selectors and Matching
# Basic selector
http_requests_total
# Label matching
http_requests_total{method="GET", status="200"}
# Regex matching
http_requests_total{method=~"GET|POST"}
http_requests_total{status!~"2.."}
# Negation matching
http_requests_total{method!="DELETE"}
# __name__ matching (metric name is also a label)
{__name__=~"http_requests.*"}
3. Core Functions
rate() — Per-second rate of change for Counters
# Requests per second over the last 5 minutes
rate(http_requests_total[5m])
# Requests per second by job
sum(rate(http_requests_total[5m])) by (job)
# Top 5 requests per second by method
topk(5, sum(rate(http_requests_total[5m])) by (method))
Note: rate() should only be used with Counter types. For Gauges, use deriv().
increase() — Total increase over a time period
# Total requests over the last 1 hour
increase(http_requests_total[1h])
# Daily error count
sum(increase(http_requests_total{status=~"5.."}[24h]))
histogram_quantile() — Percentile calculation
# 95th percentile response time
histogram_quantile(0.95,
sum(rate(http_request_duration_seconds_bucket[5m])) by (le)
)
# 99th percentile by service
histogram_quantile(0.99,
sum(rate(http_request_duration_seconds_bucket[5m])) by (le, service)
)
# 50th percentile (median) response time
histogram_quantile(0.5,
rate(http_request_duration_seconds_bucket[5m])
)
irate() — Instantaneous rate of change
# Instantaneous rate based on the most recent 2 data points
irate(http_requests_total[5m])
rate() computes the average over the entire range, while irate() computes the rate of change between the two most recent data points. For dashboards, irate() is more responsive, while for alerting, rate() is more stable.
4. Aggregation Operators
# sum — Total
sum(rate(http_requests_total[5m])) by (instance)
# avg — Average
avg(node_cpu_seconds_total{mode="idle"}) by (instance)
# max, min
max(container_memory_usage_bytes) by (pod)
# count — Number of time series
count(up == 1) by (job)
# quantile — Quantile
quantile(0.95, rate(http_requests_total[5m]))
# stddev — Standard deviation
stddev(rate(http_requests_total[5m])) by (job)
# without — Aggregate excluding specific labels
sum without (instance)(rate(http_requests_total[5m]))
5. Practical Query Patterns
Error Rate Calculation
# HTTP 5xx error rate (%)
sum(rate(http_requests_total{status=~"5.."}[5m]))
/
sum(rate(http_requests_total[5m]))
* 100
# Availability rate by service
1 - (
sum(rate(http_requests_total{status=~"5.."}[5m])) by (service)
/
sum(rate(http_requests_total[5m])) by (service)
)
Resource Utilization
# CPU utilization (%)
100 - (avg by (instance)(
irate(node_cpu_seconds_total{mode="idle"}[5m])
) * 100)
# Memory utilization (%)
(1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100
# Disk utilization
(1 - node_filesystem_avail_bytes{mountpoint="/"}
/ node_filesystem_size_bytes{mountpoint="/"}) * 100
Saturation
# Pod CPU throttling
sum(rate(container_cpu_cfs_throttled_seconds_total[5m])) by (pod)
/
sum(rate(container_cpu_cfs_periods_total[5m])) by (pod)
# Queue length
avg_over_time(queue_length[5m])
6. Writing Alerting Rules
# prometheus-rules.yaml
groups:
- name: application
rules:
# High error rate
- alert: HighErrorRate
expr: |
sum(rate(http_requests_total{status=~"5.."}[5m])) by (service)
/
sum(rate(http_requests_total[5m])) by (service)
> 0.05
for: 5m
labels:
severity: critical
annotations:
summary: 'High error rate on {{ $labels.service }}'
description: 'Error rate is {{ $value | humanizePercentage }}'
# Slow response time
- alert: SlowResponseTime
expr: |
histogram_quantile(0.95,
sum(rate(http_request_duration_seconds_bucket[5m])) by (le, service)
) > 1.0
for: 10m
labels:
severity: warning
annotations:
summary: 'P95 latency > 1s on {{ $labels.service }}'
- name: infrastructure
rules:
# High memory usage
- alert: HighMemoryUsage
expr: |
(1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) > 0.9
for: 15m
labels:
severity: warning
# Disk fill prediction
- alert: DiskWillFillIn24h
expr: |
predict_linear(node_filesystem_avail_bytes{mountpoint="/"}[6h], 24*3600) < 0
for: 30m
labels:
severity: warning
annotations:
summary: 'Disk will fill in 24h on {{ $labels.instance }}'
# Pod CrashLooping
- alert: PodCrashLooping
expr: |
increase(kube_pod_container_status_restarts_total[1h]) > 5
for: 10m
labels:
severity: critical
7. Grafana Dashboard Queries
# RED Method Dashboard
# Rate (requests/sec)
sum(rate(http_requests_total[5m])) by (service)
# Errors (errors/sec)
sum(rate(http_requests_total{status=~"5.."}[5m])) by (service)
# Duration (latency distribution)
histogram_quantile(0.5, sum(rate(http_request_duration_seconds_bucket[$__rate_interval])) by (le))
histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket[$__rate_interval])) by (le))
histogram_quantile(0.99, sum(rate(http_request_duration_seconds_bucket[$__rate_interval])) by (le))
8. Quiz
Q1: What is the difference between rate() and irate(), and when should each be used?
rate(): Computes the average per-second rate of change over the entire range. It is smoother and more stable, making it suitable for alerting rules. irate(): Computes the instantaneous rate of change between the two most recent data points within the range. It detects spikes quickly, making it suitable for dashboard graphs.
Note: Both accept a Range Vector as an argument, but the meaning of [5m] differs. rate() computes the 5-minute average, while irate() uses only the 2 most recent points within the 5-minute window.
Q2: What is the purpose and mechanism of the predict_linear() function?
predict_linear(v range-vector, t scalar) applies linear regression to time-series data to predict the value t seconds in the future.
Example: predict_linear(node_filesystem_avail_bytes[6h], 24*3600) < 0
This analyzes the disk free space trend over the last 6 hours and predicts whether it will drop below 0 in 24 hours.
It is primarily used for capacity planning alerts to proactively detect resource exhaustion such as disk shortage or memory growth.
Q3: What is the role of the le label in histogram_quantile, and what are the caveats?
le stands for "less than or equal" and represents the upper bound of histogram buckets. http_request_duration_seconds_bucket{le="0.5"} represents the number of requests that took 0.5 seconds or less.
Caveats:
You must include by (le) -- omitting it breaks the per-le aggregation and produces incorrect values
If bucket boundaries are sparse, interpolation becomes inaccurate
Results are approximations -- if you need exact percentiles, use the Summary metric type
Quiz
Q1: What is the main topic covered in "Prometheus PromQL Mastery Guide"?
Master PromQL data types, selectors, key functions (rate/increase/histogram_quantile), alerting
rules, and dashboard queries through practical examples.
Q2: What is PromQL Fundamentals?
PromQL (Prometheus Query Language) is the query language used to query and analyze time-series
data in Prometheus. Both Grafana dashboards and alerting rules are written in PromQL. 4 Data Types
Metric Types
Q3: Explain the core concept of Core Functions.
rate() — Per-second rate of change for Counters Note: rate() should only be used with Counter
types. For Gauges, use deriv().
Q4: What are the key aspects of Practical Query Patterns?
Error Rate Calculation Resource Utilization Saturation