Implementing Binary Operators
1. Using Arithmetic Operators
| Operator | Description | Example | Notes |
|---|---|---|---|
| + | Addition | rate(http_requests_total[5m]) + rate(http_errors_total[5m]) |
Vector-to-vector binary operation |
| - | Subtraction | http_requests_total - http_errors_total |
Vector-to-vector binary operation |
| * | Multiplication | http_requests_total * 2 |
Scalar multiplication |
| / | Division | http_requests_total / http_errors_total |
Vector-to-vector binary operation |
| % | Modulo | http_requests_total % 100 |
Vector-to-vector binary operation |
| ^ | Power | http_requests_total ^ 2 |
Vector-to-vector binary operation |
| == | Equal to | http_requests_total == 100 |
Vector-to-scalar comparison |
| != | Not equal to | http_requests_total != 100 |
Vector-to-scalar comparison |
| < | Less than | http_requests_total < 100 |
Vector-to-scalar comparison |
| > | Greater than | http_requests_total > 100 |
Vector-to-scalar comparison |
2. Implementing Comparison Operators
| Operator | Description | Example | Notes |
|---|---|---|---|
| == | Equal to | http_requests_total == 100 |
Returns 1 if true, 0 if false |
| != | Not equal to | http_requests_total != 100 |
Returns 1 if true, 0 if false |
| < | Less than | http_requests_total < 100 |
Returns 1 if true, 0 if false |
| > | Greater than | http_requests_total > 100 |
Returns 1 if true, 0 if false |
| <= | Less than or equal to | http_requests_total <= 100 |
Returns 1 if true, 0 if false |
| >= | Greater than or equal to | http_requests_total >= 100 |
Returns 1 if true, 0 if false |
3. Using Logical Operators
| Operator | Description | Example | Notes |
|---|---|---|---|
| and | Logical AND | http_requests_total and http_errors_total |
Returns 1 if both operands are non-zero, 0 otherwise |
| or | Logical OR | http_requests_total or http_errors_total |
Returns 1 if either operand is non-zero, 0 otherwise |
| unless | Logical NOT | http_requests_total unless http_errors_total |
Returns 1 if the left operand is non-zero and right operand is zero, 0 otherwise |
4. Understanding Vector Matching
Note: Vector matching determines how to combine vectors when performing binary operations.
| Matching Type | Description | Example | Notes |
|---|---|---|---|
| One-to-One | Each element in the left vector matches one element in the right vector | http_requests_total * http_errors_total |
Default matching behavior |
| Many-to-One | Multiple elements in the left vector match one element in the right vector | http_requests_total * sum(http_errors_total) by (instance) |
Requires grouping with by or without |
| One-to-Many | One element in the left vector matches multiple elements in the right vector | sum(http_requests_total) by (instance) * http_errors_total |
Requires grouping with by or without |
5. Implementing Many-to-One Matching
Example: Many-to-One Matching
rate(http_requests_total[5m]) * sum(rate(http_errors_total[5m])) by (instance)
Note: The right operand is aggregated to a single value per instance, which then matches with each element in the left vector.
6. Implementing One-to-Many Matching
Example: One-to-Many Matching
sum(rate(http_requests_total[5m])) by (instance) * rate(http_errors_total[5m])
Note: The left operand is aggregated to a single value per instance, which then matches with each element in the right vector.
7. Using on Clause
Note: The
on clause specifies the labels to match on, overriding default matching behavior.8. Using ignoring Clause
Example: Using ignoring Clause
http_requests_total ignoring (job) * http_errors_total ignoring (job)
Note: The
ignoring clause specifies labels to ignore during matching, allowing for more flexible vector alignment.9. Understanding Operator Precedence
| Precedence Level | Operators | Description |
|---|---|---|
| 1 | ^ |
Power operator (right associative) |
| 2 | *, /, % |
Multiplication, division, modulo (left associative) |
| 3 | +, - |
Addition, subtraction (left associative) |
| 4 | ==, !=, <, >, <=, >= |
Comparison operators (left associative) |
| 5 | and, unless |
Logical operators (left associative) |
| 6 | or |
Logical OR operator (left associative) |
10. Combining Multiple Operators
Example: Combining Multiple Operators
(rate(http_requests_total[5m]) + rate(http_errors_total[5m])) / rate(http_requests_total[5m]) * 100
Note: Parentheses can be used to control operator precedence and group operations.
11. Using Bool Modifier
Note: The
bool modifier converts the result of a comparison to a boolean value (1 or 0).12. Implementing Comparison Filtering
Note: Comparison filtering allows selecting metrics based on specific conditions using logical operators.