MyObservability

PromQL (Prometheus Query Language)

PromQL query follows three key steps

  1. Metric Selection: Choose a metric name to query.
  2. Label Filtering: Add label selectors to narrow your results.
  3. Time window selection: Use a time range to focus your query.

Example: http_requests_total {job=”web-server”} 5m

In Prometheus’s expression language, an expression or sub-expression can evaluate to one of four types:


Prometheus metric types


Time based Functions

Functions like rate(), irate(), and increase() turn counter data into powerful indicators of system behavior. Use rate() for smooth trend visualization, irate() for real-time alerting, and increase() for measuring absolute change.

Counters tell you the gradual increase (Like distance travelled etc) and rate helps converting total trends (Speed movement etc)

PromQL’s time-based targeting functions: rate(), irate(), and increase().


Aggregation

Combines many individual time series into a single, unified reading using functions like sum(). Ex: sum(), avg(), count() etc


Grouping

Group time series by targetting the system.

Ex: by(), without(),


Selectors & Matchers

Ex: node_cpu_seconds_total{mode="idle",cpu!="1"}


Modifiers

You can combine both offset and modifier. Ex: node_cpu_seconds_total{mode="idle"} @1774082700 offset 1h.

Ex: get 2mins worth of data 1m before Sunday, 22 March 2026 at 11:06:54 (which is 11:03:54 to 11:05:54). nnode_cpu_seconds_total{cpu="0", instance="host.docker.internal:9100"}[2m] @1774174014 offset 1m.


Operators

You can use operators like

Ex: node_memory_Active_bytes{instance="host.docker.internal:9100"} / 1024

Ex: Unless - Return all time series greater than 500 unless it is greater than 5000. node_file_system_avail_bytes > 500 UNLESS node_file_system_avail_bytes > 5000

Vector Matching

Vector matching: when you perform metric1 / metric2. Ptometheus tries to match all labels exactly. the results will be incorrect / confusing output. So vector matrics helps to solves the problem.

Two basic types of matching behavior:

one to one

All labels must be same for sample to match. Even extra labels do not allow to match

Ex:

Query: node_filesystem_avail_bytes / node_filesystem_size_bytes * 100

Result:

There might be certain instances where an operation needs to be perfromed on 2 vectors with different labels.

Use below methods

Ex:

If you perform http_errors:rate5m{code="500"} / http_requests it do not work as lables are different.

In this case use ignore or on

  1. use ignore

method_code:http_errors:rate5m{code="500"} / ignoring(code) method:http_requests:rate5m

  1. Use on

method_code:http_errors:rate5m{code="500"} / on(method) method:http_requests:rate5m

many-to-one/one-to-many

Eqach vector elements on the the one side can match with multiple elements on the many side.

Ex:

If you use on(method), there are multiple matches for method on left side (2 get methods, 2 post). The error will be many-to-one matching must be explicit (group_left/group.right).

So use group modifiers

Ex: method_code:http_errors:rate5m / ignoring(code) group_left method:http_requests:rate5m

Result:

group_right is the opposite

filling in missing matches

Fill modifiers are experimental and must be enabled with --enable-feature=promql-binop-fill-modifiers

By default, vector elements that do not find a match on the other side of a binary operation are not included in the result vector. Fill modifiers allow overriding this behavior by filling in missing series on either side of a binary operation with a provided default sample value:

Aggregation operators

These operators can either be used to aggregate over all label dimensions or preserve distinct dimensions by including a without or by clause. These clauses may be used before or after the expression.

ex:

using sum by(code) (http_errors)

Result:

You can also use muliple by clauses sum by(code, method) (http_errors)

using without does the oppoiste of by, tells the query which labels not to include in the aggregation

using sum without(code) (http_errors)

Result:



Next Page: Architecture

Main Page: Click Here