Chapter 15. Binary Operators
You will want to do more with your metrics than simply aggregate them, which is where the binary operators come in. Binary operators are operators that take two operands,1 such as the addition and equality operators.
Binary operators in allow for more than simple arithmetic on instant vectors; you can also apply a binary operator to two instant vectors with grouping based on labels. This is where the real power of PromQL comes out, allowing classes of analysis that few other metrics systems offer.
PromQL has three sets of binary operators: arithmetic operators, comparison operations, and logical operators. This chapter will show you how to use them.
Working with Scalars
In addition to instant vectors and range vectors, there is another type of
value known as a scalar.2 Scalars are single numbers with
no dimensionality. For example, 0
is a scalar with the value zero, while {}
0
is an instant vector containing a single sample with no labels and the value
zero.3
Arithmetic Operators
You can use scalars in arithmetic with an instant vector to change the values in the instant vector. For example:
process_resident_memory_bytes / 1024
would return:
{instance="localhost:9090",job="prometheus"} 21376 {instance="localhost:9100",job="node"} 13316
which is the process memory usage, in kilobytes.4
You will note that the division operator was applied to all time series in the
instant vector returned by the process_resident_memory_bytes
selector, and that the metric ...
Get Prometheus: Up & Running, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.