In Prometheus, comparison binary operators help you filter or compare metrics based on conditions. They're crucial when you want to trigger alerts, highlight thresholds, or clean up your queries by removing irrelevant series.
Just like arithmetic operators, comparison operators work on:
· Scalar to Scalar
· Scalar to Vector
· Vector to Vector (with label matching rules)
Let’s look at the available operators and how to use them with real-world examples.
Comparison Binary Operators in Prometheus
Operator |
Description |
== |
Equal to |
!= |
Not equal to |
> |
Greater than |
< |
Less than |
>= |
Greater or equal |
<= |
Less or equal |
How They Work?
· Scalar vs Scalar: Returns a 1 (true) or 0 (false).
· Vector vs Scalar: Each time series in the vector is compared against the scalar.
· Vector vs Vector: Only matching label sets are compared.
Example 1: Scalar to Scalar
We need to use bool keyword for scalar to scalar comparison.
5 > bool 3
5 < bool 3
5 == bool 3
2. Vector > Scalar: CPU Idle Time
node_cpu_seconds_total{mode="idle"} > 40000
Returns time series where the seconds the CPUs spent in mode idle is > 40000.
3. Vector != Scalar: Filter Out Specific Values
node_cpu_seconds_total{mode="system"} != 0
4. Vector == Vector (with ignoring)
Let’s compare idle and system CPU time across CPUs, ignoring the mode label.
node_cpu_seconds_total{mode="idle"}
>
ignoring(mode)
node_cpu_seconds_total{mode="system"}
Just like arithmetic ops, vector-to-vector comparisons require label sets to match.
· ignoring(label_name) to skip matching on that label.
· on(label_name) to only match on specified labels.
node_cpu_seconds_total{mode="idle"}
>
on(cpu)
node_cpu_seconds_total{mode="system"}
In summary, Comparison binary operators are essential for:
· Filtering time series data based on thresholds.
· Building custom logic into alerts.
· Comparing metrics from different sources or labels.
No comments:
Post a Comment