What is PromQL
- User
What is PromQL
Assistant
PromQL (Prometheus Query Language) is the domain-specific language used to query and analyze time series data stored in Prometheus, a popular open-source monitoring and alerting system. It is designed to efficiently retrieve, aggregate, and transform metrics for monitoring, alerting, and visualization.
Key Features of PromQL
-
Time Series Data Focus:
- Works with timestamped data (each data point has a value and a timestamp).
- Metrics are typically labeled (e.g.,
http_requests_total{method="post", handler="/api/v1"}).
-
Label-Based Filtering:
- Filter metrics using labels (e.g.,
{method="post"}selects metrics with themethodlabel set topost). - Exclude labels with
withoutor group by labels withby(e.g.,sum by (handler)).
- Filter metrics using labels (e.g.,
-
Aggregation Functions:
- Mathematical:
sum,avg,max,min,count,stddev, etc. - Rate Calculations:
rate()(per-second rate over a time window),irate()(instantaneous rate). - Range-Based:
increase(),changes(), andabsent().
- Mathematical:
-
Vector Matching:
- Combine metrics with matching labels using
on()andignoring()(e.g.,A on(instance) + Bmatches on theinstancelabel).
- Combine metrics with matching labels using
-
Operators:
- Arithmetic:
+,-,*,/,^. - Relational:
==,!=,>=,<=,>,<. - Logical:
and,or,unless.
- Arithmetic:
-
Range Vectors and Instant Vectors:
- Instant queries return the latest value (e.g.,
http_requests_total). - Range queries return a series of values over a time window (e.g.,
http_requests_total[5m]for the last 5 minutes).
- Instant queries return the latest value (e.g.,
-
Subqueries and Offset:
- Use
offsetto shift time series backward (e.g.,http_requests_total offset 1hcompares to data from an hour ago).
- Use
Basic Syntax Elements
-
Metric Selection:
metric_name{label1="value1", label2="value2"}
Example:http_requests_total{method="post"} -
Aggregation:
sum(metric_name) by (label)
Example:sum(rate(http_requests_total[5m])) by (handler) -
Function Chaining:
function1(function2(metric))
Example:rate(http_requests_total[5m])(calculates per-second rate over 5 minutes). -
Vector Matching:
A * on(label) group_left B
Example: Multiply two metrics by matching on theinstancelabel.
Common Use Cases
- Monitoring:
up{job="api-server"} == 0alerts if an API server is down. - Performance Analysis:
rate(http_requests_total[5m])shows request rate trends. - Resource Utilization:
avg(node_cpu{mode="idle"}) by (instance)monitors CPU idle time across instances. - Alerting Rules:
sum(rate(http_errors_total{job="web"}[5m])) > 10triggers an alert for high error rates.
Example Queries
- Filter by Labels:
http_requests_total{method="get", handler="/health"} - Aggregate and Group:
sum by (handler) (http_requests_total) - Rate of Counters:
rate(prometheus_http_requests_total[5m]) - Comparison with Offset:
node_load1 - node_load1 offset 1h
Why PromQL?
- Efficiency: Optimized for fast queries on large-scale time series data.
- Flexibility: Combines metrics, labels, and functions to answer complex questions.
- Integration: Used in Prometheus dashboards (e.g., Grafana), alerts, and alerting rules.
PromQL is essential for anyone working with Prometheus to extract insights from metrics, set up alerts, and troubleshoot systems.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.