PromQL
PromQL
Basic Concepts
- Metric: A time series data point.
- Labels: Key-value pairs associated with metrics.
- Selectors: Expressions to filter time series based on labels.
Data Types
- Instant Vector: Set of time series containing a single sample per time series, all sharing the same timestamp.
- Range Vector: Set of time series containing a range of data points over time.
- Scalar: A single numerical value.
- String: A single string value.
Selectors
- Instant Vector Selector:
metric_name{label_name="label_value"}
Operators
- Arithmetic Operators:
+,-,*,/,%,^ - Comparison Operators:
==,!=,>,<,>=,<= - Logical/Set Operators:
and,or,unless
Functions
Aggregation Functions
sum(): Sum of values.avg(): Average of values.max(): Maximum value.min(): Minimum value.count(): Count of values.stddev(): Standard deviation of values.stdvar(): Variance of values.topk(k, metric): Top k elements by value.bottomk(k, metric): Bottom k elements by value.count_values(): Count of occurrences of each distinct value.
Special Functions
rate(): Per-second average rate of increase.increase(): Increase in the value over a specified range.delta(): Difference between the first and last value.irate(): Instantaneous rate of increase.idelta(): Instantaneous difference.histogram_quantile(): Quantile of histogram data.
Time Functions
time(): Current time as a UNIX timestamp.timestamp(): Return the UNIX timestamp of a given time series.
Examples
Basic Metric Selection
-
All metrics named
http_requests_total: -
All metrics named
http_requests_totalwith labeljob="api":
Range Queries
-
Rate of increase per second for
http_requests_totalover the last 5 minutes: -
Sum of rates of increase per second for
http_requests_totalover the last 5 minutes:
Aggregation
-
Average value of
http_requests_totalbyjob: -
Sum of
http_requests_totalbyinstance:
Mathematical Operations
-
Increase in
http_requests_totalover the last 5 minutes: -
Requests per second:
Conditional Expressions
-
All
http_requests_totalmetrics whereinstanceis not "localhost:9090": -
All
http_requests_totalmetrics whereinstancelabel regex matches "app":
Time-based Queries
-
Current UNIX timestamp:
-
Timestamp of the
upmetric:
Quantiles
- 90th percentile of
http_request_duration_secondshistogram:
Combining Metrics
-
Divide
metric_abymetric_b: -
Sum of
metric_aandmetric_b:
Best Practices
- Use Aggregation for Large Data Sets: Aggregate data to avoid overwhelming the system with too many time series.
- Leverage Label Filters: Use labels to filter and refine queries for more precise results.
- Avoid Overuse of
rate(): Userate()judiciously, especially over short intervals, to avoid misleading spikes.
For more detailed use cases and advanced queries, refer to the Prometheus documentation.