Arithmetic binary operators in Prometheus take two operands (scalars or vectors) and perform standard math operations.
Operator |
Description |
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Remainder |
^ |
Exponentiation |
These operations are defined for:
· Scalar to scalar
· Vector to scalar
· Vector to vector (with matching labels)
Example 1: Add two scalars
2 + 3
Example 2: CPU Load Adjustment: Vector + Scalar
Let’s say we want to add a constant offset (for testing) to the 1-minute load average.
node_load1 + 0.5
3. Scaling Metric Values: Vector * Scalar
Convert bytes to megabytes:
node_memory_active_bytes/ (1024 * 1024)
Vector-to-Vector Operations
When operating on two vectors, Prometheus matches labels to align time series. If the labels don’t match, you can use on() or ignoring() modifiers.
For example,
node_cpu_seconds_total{mode="idle"}
/
node_cpu_seconds_total{mode="system"}
When execute above query, I see empty response.
Why This Returns Empty?
Prometheus does vector-to-vector operations only when the label sets match exactly, except for the labels you tell it to ignore or match on.
In this case:
· node_cpu_seconds_total{mode="idle"} returns one set of series.
· node_cpu_seconds_total{mode="system"} returns another.
The issue is, each of these time series has additional labels like cpu, instance, and job, and Prometheus only combines vectors where the label sets match exactly.
So unless both vectors have identical values for all labels, Prometheus won’t match them and it returns nothing.
How to Fix It?
You need to ignore the label that’s different, most likely the mode label, while preserving labels like cpu, instance, etc.
node_cpu_seconds_total{mode="idle"}
/
ignoring(mode)
node_cpu_seconds_total{mode="system"}
In summary, Arithmetic binary operators are used when it comes to transforming raw metrics into actionable insights. They help answer critical questions like:
· How much memory is being used?
· What’s the CPU load converted to a readable format?
· Are the system values within threshold?
No comments:
Post a Comment