O'Reilly logo

Prometheus: Up & Running by Brian Brazil

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 Prometheus 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required