--- title: Operators sort_rank: 2 --- # Operators ## Binary operators Prometheus's query language supports basic logical and arithmetic operators. For operations between two instant vectors, the [matching behavior](#vector-matching) can be modified. ### Arithmetic binary operators The following binary arithmetic operators exist in Prometheus: * `+` (addition) * `-` (subtraction) * `*` (multiplication) * `/` (division) * `%` (modulo) * `^` (power/exponentiation) Binary arithmetic operators are defined between scalar/scalar, vector/scalar, and vector/vector value pairs. **Between two scalars**, the behavior is obvious: they evaluate to another scalar that is the result of the operator applied to both scalar operands. **Between an instant vector and a scalar**, the operator is applied to the value of every data sample in the vector. E.g. if a time series instant vector is multiplied by 2, the result is another vector in which every sample value of the original vector is multiplied by 2. **Between two instant vectors**, a binary arithmetic operator is applied to each entry in the left-hand-side vector and its [matching element](#vector-matching) in the right hand vector. The result is propagated into the result vector and the metric name is dropped. Entries for which no matching entry in the right-hand vector can be found are not part of the result. ### Comparison binary operators The following binary comparison operators exist in Prometheus: * `==` (equal) * `!=` (not-equal) * `>` (greater-than) * `<` (less-than) * `>=` (greater-or-equal) * `<=` (less-or-equal) Comparison operators are defined between scalar/scalar, vector/scalar, and vector/vector value pairs. By default they filter. Their behaviour can be modified by providing `bool` after the operator, which will return `0` or `1` for the value rather than filtering. **Between two scalars**, the `bool` modifier must be provided and these operators result in another scalar that is either `0` (`false`) or `1` (`true`), depending on the comparison result. **Between an instant vector and a scalar**, these operators are applied to the value of every data sample in the vector, and vector elements between which the comparison result is `false` get dropped from the result vector. If the `bool` modifier is provided, vector elements that would be dropped instead have the value `0` and vector elements that would be kept have the value `1`. **Between two instant vectors**, these operators behave as a filter by default, applied to matching entries. Vector elements for which the expression is not true or which do not find a match on the other side of the expression get dropped from the result, while the others are propagated into a result vector with their original (left-hand-side) metric names and label values. If the `bool` modifier is provided, vector elements that would have been dropped instead have the value `0` and vector elements that would be kept have the value `1` with the left-hand-side metric names and label values. ### Logical/set binary operators These logical/set binary operators are only defined between instant vectors: * `and` (intersection) * `or` (union) * `unless` (complement) `vector1 and vector2` results in a vector consisting of the elements of `vector1` for which there are elements in `vector2` with exactly matching label sets. Other elements are dropped. The metric name and values are carried over from the left-hand-side vector. `vector1 or vector2` results in a vector that contains all original elements (label sets + values) of `vector1` and additionally all elements of `vector2` which do not have matching label sets in `vector1`. `vector1 unless vector2` results in a vector consisting of the elements of `vector1` for which there are no elements in `vector2` with exactly matching label sets. All matching elements in both vectors are dropped. ## Vector matching Operations between vectors attempt to find a matching element in the right-hand-side vector for each entry in the left-hand side. There are two basic types of matching behavior: **One-to-one** finds a unique pair of entries from each side of the operation. In the default case, that is an operation following the format `vector1 vector2`. Two entries match if they have the exact same set of labels and corresponding values. The `ignoring` keyword allows ignoring certain labels when matching, while the `on` keyword allows reducing the set of considered labels to a provided list: ignoring(