Prometheus Binary Operators

While PromQL does not have traditional binary operators, it supports a range of functions and operators for manipulating and aggregating data. Here are some key operators in PromQL:

  • Arithmetic Operators:
    • Addition: +
    • Subtraction: -
    • Multiplication: *
    • Division: /
    • Modulo: %
  • Comparison Operators:
    • Equal to: ==
    • Not equal to: !=
    • Greater than: >
    • Less than: <
    • Greater than or equal to: >=
    • Less than or equal to: <=
  • Logical Operators:
    • AND: and
    • OR: or
    • NOT: unless or not
  • Set Operators:
    • Union: |
    • Intersection: &
    • Difference: \

These operators can be used to construct queries in PromQL to filter, aggregate, and perform calculations on the time-series data stored in Prometheus. It's worth noting that PromQL is specifically designed for working with time-series data, and its operators and functions are optimized for that purpose.

Examples

1. Arithmetic Operators:

- Addition: "metric1 + metric2"

Example:

http_requests_total + http_errors_total

- Subtraction: "metric1 - metric2"

Example:

cpu_usage - cpu_idle

- Multiplication: "metric1 * metric2"

Example:

price * quantity

- Division: "metric1 / metric2"

Example:

response_time / request_count

- Modulo: "metric1 % metric2"

Example:

total_requests % 100

2. Comparison Operators:

- Equal to: "metric1 == metric2"

Example:

status_code == 200

- Not equal to: "metric1 != metric2"

Example:

error_code != 0

- Greater than: "metric1 > metric2"

Example:

cpu_usage > 80

- Less than: "metric1 < metric2"

Example:

response_time < 100

- Greater than or equal to: "metric1 >= metric2"

Example:

response_time >= 200

- Less than or equal to: "metric1 <= metric2"

Example:

error_count <= 5

3. Logical Operators:

- AND: "metric1 and metric2"

Example:

cpu_usage > 80 and memory_usage > 70

- OR: "metric1 or metric2"

Example:

status_code == 500 or status_code == 503

- NOT: "unless(metric)"

Example:

unless(cpu_usage > 90)

4. Set Operators:

- Union: "metric1 | metric2"

Example:

http_requests_total{status="200"} | http_requests_total{status="500"}

- Intersection: "metric1 & metric2"

Example:

http_requests_total{status="200"} & http_requests_total{method="GET"}

- Difference: "metric1 \ metric2"

Example:

http_requests_total{status="200"} \ http_requests_total{method="POST"}

These examples demonstrate how the operators can be used in PromQL queries to perform arithmetic calculations, compare metrics, combine conditions, and manipulate sets of data.