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

No comments:

Post a Comment