Tuesday, 3 June 2025

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

Saturday, 24 May 2025

Quick Introduction to Prometheus Data Types

 

Prometheus is a powerful open-source monitoring and alerting toolkit. Whether you're tracking how many users visited your website or monitoring server health, Prometheus helps you to collect and analyze metrics over time. But before diving deep into PromQL (Prometheus Query Language), it’s important to understand the four core data types Prometheus works with.

 

1. First, What Is a Time Series?

A time series in Prometheus is a metric (like CPU usage or request count) that is tracked over time. Each time series is identified by:

 

·      A metric name (like http_requests_total)

·      A set of labels (like {method="GET", handler="/home"})

 

Each time series stores:

 

·      A timestamp

·      A value

·      Set of labels

 

Timestamp

Value

Labels

2025-04-11 10:00

124

{code="200", handler="/", instance="localhost:9090", job="prometheus"}

2025-04-11 10:00

34

{code="200", handler="/classic/static/*filepath", instance="localhost:9090", job="prometheus"}

2025-04-11 10:00

65

{code="200", handler="/version", instance="localhost:9090", job="prometheus"}

2025-04-11 10:00

79

{code="200", handler="/api/v1/admin/tsdb/clean_tombstones", instance="localhost:9090", job="prometheus"}

 

2. Data Types in Prometheus

 

2.1 Instant Vector

An Instant Vector is a snapshot of one or more time series at a single point in time.

 

For example, You have a microservices-based application with 3 services:

 

·      auth-service

·      user-service

·      payment-service

 

Each of these services exposes a Prometheus metric:

prometheus_http_requests_total

This metric counts the total number of HTTP requests handled by each service, broken down by method (GET, POST) and route (/login, /user, /pay, etc.).

 

At exactly 10:00 AM, Prometheus scrapes the metrics and you get:

 

Service

Method

Route

Total Requests

auth-service

POST

/login

4,521

user-service

GET

/profile

123

payment-service

POST

/pay

8675

 

Each row is a time series, and all values share the same timestamp (10:00 AM).

 


You can even get the metrics for a given handler like below.

 

prometheus_http_requests_total{handler="/metrics"} 

 


2.2 Range Vector

A Range Vector is a set of time series where each series contains multiple data points, all within a specific time range (like the last 5 minutes).

 

Let’s say you’re tracking HTTP requests across 3 microservices:

 

·      auth-service

·      user-service

·      payment-service

 

You're interested in how many requests each service received over the last 5 minutes, and how fast that number is increasing. Prometheus scrapes data every 15 seconds, so over 5 minutes, you’ll get 20 data points per time series.

 

Example PromQL:

prometheus_http_requests_total[5m]

 

This query gives you:

·      All request counter values for the last 5 minutes

 

·      For every combination of route, method, and service (i.e., time series)

 

You can’t plot this directly on a dashboard, but it’s super useful when combined with a function like rate().

 


You can filter out for a given handler for 5 minutes duration like below:

 

prometheus_http_requests_total{handler="/metrics"}[5m]

 


Prometheus scrapes data every 15 seconds as per my system configuration, so over 5 minutes, you’ll get 20 data points per time series.

 

2.3 Scalar: A Scalar is just a single numeric value (a floating-point number) with no labels or time series context.

 

Just the number 5.67 floating in space — that’s a scalar.

 

For example,

5 * rate(prometheus_http_requests_total{handler="/metrics"}[5m])

 

Here, 5 is a scalar multiplying a range vector result.

 


2.4 String: A String in Prometheus is a simple text value. However, Prometheus doesn’t really use strings in most queries.

 

In summary, understanding data types in Prometheus is key to writing effective queries.

·      Use instant vectors for current values.

·      Use range vectors to analyze trends over time.

·      Use scalars for math and logic.

·      Ignore strings unless you're deep into Prometheus internals.

 

Previous                                                    Next                                                    Home