Tuesday, 3 June 2025

Logical/Set Binary Operators in Prometheus (AND, OR, UNLESS)

In Prometheus, logical/set binary operators are used to perform set-based operations (like intersection, union, or complement) on instant vectors only. These operations are based on matching labels rather than actual metric values.

Following table summarizes the Logical Operators.

Operator

Meaning

Description

and

Intersection 

Returns time series present in both sides

or

Union

Returns time series present in either side

unless 

Complement 

Returns time series on left side only, not on right

 

1. and (Intersection)

For example, 'up' metric return following response.

up{instance="localhost:9090", job="prometheus"}

up{instance="localhost:9100", job="node_exporter"} 

 


'node_memory_active_bytes' return following response.

node_memory_active_bytes{instance="localhost:9100", job="node_exporter"}

 


up and node_memory_active_bytes will return following response.

up{instance="localhost:9100", job="node_exporter"}

 


If you want the value of node_memory_active_bytes after performing and operation, make the metric ‘node_memory_active_bytes’ as left operand.

 

node_memory_active_bytes and up

 


(node_memory_active_bytes and up) vs  (up and node_memory_active_bytes)

For logical/set binary operators like and, Prometheus returns the time series from the left-hand side that have matching labels on the right-hand side.

 

node_memory_active_bytes and up

·      Keeps time series from node_memory_active_bytes

·      Filters to include only those series that have matching labels in up

 

up and node_memory_active_bytes

·      Keeps time series from up

·      Filters to include only those series that have matching labels in node_memory_active_bytes

 

2. or (union)

Returns all-time series that exist in either up or node_memory_MemAvailable_bytes.

 

For example, the metric node_boot_time_seconds return following response.

node_boot_time_seconds{instance="localhost:9100", job="node_exporter"}  1744263605.246621

 


The metric 'go_gc_heap_goal_bytes' return following response.

go_gc_heap_goal_bytes{instance="localhost:9090", job="prometheus"}  40831809

 


node_boot_time_seconds or go_gc_heap_goal_bytes return below response.

node_boot_time_seconds{instance="localhost:9100", job="node_exporter"}  1744263605.246621

go_gc_heap_goal_bytes{instance="localhost:9090", job="prometheus"}  40831809

 


3. unless — Complement

up unless go_gc_heap_goal_bytes

 

Returns time series from “up” that do not exist in “go_gc_heap_goal_bytes”.

 

How matching works?

Logical operators compare labels (like job, instance, etc.) to determine whether time series are the same.

 

If you want to control how the matching happens (e.g., only by cpu), you can use the on() or ignoring() clauses:

 

node_cpu_seconds_total{mode="idle"}

and

on(cpu)

node_cpu_seconds_total{mode="system"}

 

Following statement compare by ignoring mode label.

 

node_cpu_seconds_total{mode="idle"}

/

ignoring(mode)

node_cpu_seconds_total{mode="system"}

 

 

Previous                                                    Next                                                    Home

Comparison Binary Operators in Prometheus

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.

 


Previous                                                    Next                                                    Home

Arithmetic Operations on Vectors in Prometheus

In Prometheus, when you apply an arithmetic operator (such as +, -, *, /) to a vector, the operation is applied to each time series within that vector individually.

For example, the metric node_cpu_seconds_total return the seconds the CPUs spent in each mode.

 


node_cpu_seconds_total + 10000

In this example, node_cpu_seconds_total is a vector that may contain multiple time series (one for each CPU core, mode, instance, etc.). When you add 10000 to this vector, Prometheus adds 10000 to each time series value independently.


 

Here, Arithmetic operations with a scalar (like 10000) apply the operation element-wise to all series in the vector.


Previous                                                    Next                                                    Home

Arithmetic Binary Operators in Prometheus

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?


Previous                                                    Next                                                    Home

Understanding Prometheus Matchers with Real Examples

Prometheus stores data in time series, and each time series has a metric name and labels (like handler, method, code, etc.). To filter data in Prometheus, we use matchers.

 

Let’s explore the most common matchers with simple examples.

 

1. Equality Matcher (=)

This checks if a label is exactly equal to the value (case sensitive).

 

Example

promhttp_metric_handler_requests_total{instance="localhost:9100"}

 

This query return the total number of requests handled by the instance "localhost:9100".

 

2. Negative Equality Matcher (!=)

This selects all time series except those where the label equals the value.

 

Example

promhttp_metric_handler_requests_total{code != "200"}

 

This query return the total number of requests for all handlers where code is not equal to 200.

 


3. Regular Expression Matcher (=~)

This allows pattern matching using regex. It's powerful when you want to match similar strings.

prometheus_http_requests_total{handler=~"/api/v1/status.+"}

 

Above query return all requests where the handler starts with /api/v1/status followed by anything else (like /api/v1/status/health, /api/v1/status/uptime, etc.). 

 


4. Negative Regex Matcher (!~)

This excludes label values that match the regex.

 

prometheus_http_requests_total{handler!~"/api/v1/status.+"}

 

Above query return all requests where the handler is not starting with /api/v1/status

 


In summary, Match selectors are a powerful way to filter data in Prometheus. Start with simple equality, then move to regex as your queries get more advanced.  

 

Previous                                                    Next                                                    Home