Vectors in Prometheus with Examples

In this article we are going to cover Vectors in Prometheus with Examples, What are vectors in Prometheus, Instant and Range vector in prometheus with examples.

What are Vectors in Prometheus?

In Prometheus, a vector is a fundamental concept used to represent sets of related time series. It’s like a container holding multiple time series, each tracking a specific aspect of a metric.

In a simple sentence “a set of related timeseries is called a vector“, all data is in the context of some timestamp. The series that maps a timestamp to recorded data is called a timeseries

Types of Vectors in Prometheus

There are two main types of vectors in Prometheus

1:Instant Vector in Prometheus

An instant vector in Prometheus represents a set of time series, each containing a single data point with the same timestamp. This means it captures the current state of various metrics at a specific moment in time.

Below are some key point about Instant Vector in Prometheus

  • Represent a single snapshot of data at a specific timestamp.
  • Useful for monitoring current values like system stats, resource usage, etc.
  • Use case: Capture the current state of metrics like system stats, resource usage, etc.
  • Display: Often shown in graphs with a static time range (e.g., last 5 minutes).
  • Cannot: Cannot show trends over time; use range vectors for that.
    up: Indicates whether a target is currently reachable (1) or not (0).
  • You can directly select a metric name to get an instant vector. Use label selectors to filter specific time series within the vector. PromQL functions like count()min()max(), etc., can be applied to instant vectors for aggregation.

Examples: http_requests_totalup.

  • prometheus_http_requests_total: Shows the total number of HTTP requests received by each endpoint at the moment.
prometheus_http_requests_total
Vectors in Prometheus with Examples 1

While instant vectors themselves only represent a single data point across all series at a specific timestamp, several functions can be applied to manipulate and analyze them. Here’s an overview:

Aggregation Functions in Instant Vector:

  • These functions summarize an entire instant vector into a single value:
    • count(): Calculates the total number of series in the vector.
    • min(): Returns the minimum value across all series.
    • max(): Returns the maximum value across all series.
    • avg(): Calculates the average value across all series.
    • sum(): Returns the sum of all values across all series.
    • stddev(): Calculates the standard deviation of values across all series (note: requires native histograms).
    • stdvar(): Calculates the population variance of values across all series (note: requires native histograms).

Comparison Operators in Instant Vector:

  • These operators compare values between two instant vectors element-wise, resulting in an instant vector with boolean (true/false) values:
    • ><>=<===!=

Arithmetic Operators in Instant Vector:

  • These operators perform calculations between two instant vectors element-wise, resulting in an instant vector with numerical values:
    • +-*/

Label Operations in instant vector:

  • Functions like label_replace() and label_join() modify the label sets of series within an instant vector.

Other Notable Functions in intant vector:

  • abs(): Returns the absolute value of each element in the vector.
  • ceil(): Rounds each element up to the nearest integer.
  • floor(): Rounds each element down to the nearest integer.
  • exp(): Calculates the mathematical exponent of each element.
  • ln(): Calculates the natural logarithm of each element.

Some points to keep Remember in instant vector:

  • Instant vector functions operate on the current snapshot of data.
  • For analyzing trends and changes over time, use range vectors and functions like rate() and increase().

2:Range Vector in Prometheus:

Range Vector in Prometheus Represent a set of time series with a range of data points over time for each series.

  • Used to visualize trends, calculate rates, and perform aggregations over time.
  • Use case: Show trends and changes in metrics over time intervals.
  • Display: Often used in graphs with a specified time range (e.g., last hour).
  • Can: Show trends and be used with functions like rate() and increase() to analyze changes over time.
  • Range vector queries specify a time duration in square brackets
  • Both types of vectors can be filtered using label selectors.
  • Examples: rate(prometheus_http_requests_total{code=”200″}[5m]) – will calculate the rate of successful HTTP requests (status code 200) over the past 5 minutes.
rate(prometheus_http_requests_total{code="200"}[5m])
Vectors in Prometheus with Examples 2

Average CPU usage in percentage [0 … 100]% per each node for the last 5 minutes:

100 * avg(1 - rate(node_cpu_seconds_total{mode="idle"}[5m])) by (instance)
Vectors in Prometheus with Examples 3

Below are some Range vector functions in Prometheus

Difference between Instant and Range Vector in Prometheus

FeaturesInstant VectorRange Vector
Data pointsSingle data point at a specific timestampMultiple data points over a time interval
Use caseCurrent state monitoringTrend analysis, aggregation, rate calculations
ChartingSuitable for static time ranges (e.g., last 5 minutes)Shows data points over time
DisplayStatic time range graphsTime range graphs with trend lines
PromQL functionscount(), min(), max()rate(), increase(), etc.

Conclusion:

In this article we have covered Vectors in Prometheus with Examples, What are vectors in Prometheus, Instant and Range vector in prometheus with examples.

Related Articles:

Prometheus Tutorial for Beginners

Reference:

prometheus official page

FOSS TechNix

FOSS TechNix (Free,Open Source Software's and Technology Nix*) founded in 2019 is a community platform where you can find How-to Guides, articles for DevOps Tools,Linux and Databases.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap