1---
2title: Operators
3sort_rank: 2
4---
5
6# Operators
7
8## Binary operators
9
10Prometheus's query language supports basic logical and arithmetic operators.
11For operations between two instant vectors, the [matching behavior](#vector-matching)
12can be modified.
13
14### Arithmetic binary operators
15
16The following binary arithmetic operators exist in Prometheus:
17
18* `+` (addition)
19* `-` (subtraction)
20* `*` (multiplication)
21* `/` (division)
22* `%` (modulo)
23* `^` (power/exponentiation)
24
25Binary arithmetic operators are defined between scalar/scalar, vector/scalar,
26and vector/vector value pairs.
27
28**Between two scalars**, the behavior is obvious: they evaluate to another
29scalar that is the result of the operator applied to both scalar operands.
30
31**Between an instant vector and a scalar**, the operator is applied to the
32value of every data sample in the vector. E.g. if a time series instant vector
33is multiplied by 2, the result is another vector in which every sample value of
34the original vector is multiplied by 2.
35
36**Between two instant vectors**, a binary arithmetic operator is applied to
37each entry in the left-hand side vector and its [matching element](#vector-matching)
38in the right-hand vector. The result is propagated into the result vector with the
39grouping labels becoming the output label set. The metric name is dropped. Entries
40for which no matching entry in the right-hand vector can be found are not part of
41the result.
42
43### Comparison binary operators
44
45The following binary comparison operators exist in Prometheus:
46
47* `==` (equal)
48* `!=` (not-equal)
49* `>` (greater-than)
50* `<` (less-than)
51* `>=` (greater-or-equal)
52* `<=` (less-or-equal)
53
54Comparison operators are defined between scalar/scalar, vector/scalar,
55and vector/vector value pairs. By default they filter. Their behavior can be
56modified by providing `bool` after the operator, which will return `0` or `1`
57for the value rather than filtering.
58
59**Between two scalars**, the `bool` modifier must be provided and these
60operators result in another scalar that is either `0` (`false`) or `1`
61(`true`), depending on the comparison result.
62
63**Between an instant vector and a scalar**, these operators are applied to the
64value of every data sample in the vector, and vector elements between which the
65comparison result is `false` get dropped from the result vector. If the `bool`
66modifier is provided, vector elements that would be dropped instead have the value
67`0` and vector elements that would be kept have the value `1`.
68
69**Between two instant vectors**, these operators behave as a filter by default,
70applied to matching entries. Vector elements for which the expression is not
71true or which do not find a match on the other side of the expression get
72dropped from the result, while the others are propagated into a result vector
73with the grouping labels becoming the output label set.
74If the `bool` modifier is provided, vector elements that would have been
75dropped instead have the value `0` and vector elements that would be kept have
76the value `1`, with the grouping labels again becoming the output label set.
77
78### Logical/set binary operators
79
80These logical/set binary operators are only defined between instant vectors:
81
82* `and` (intersection)
83* `or` (union)
84* `unless` (complement)
85
86`vector1 and vector2` results in a vector consisting of the elements of
87`vector1` for which there are elements in `vector2` with exactly matching
88label sets. Other elements are dropped. The metric name and values are carried
89over from the left-hand side vector.
90
91`vector1 or vector2` results in a vector that contains all original elements
92(label sets + values) of `vector1` and additionally all elements of `vector2`
93which do not have matching label sets in `vector1`.
94
95`vector1 unless vector2` results in a vector consisting of the elements of
96`vector1` for which there are no elements in `vector2` with exactly matching
97label sets. All matching elements in both vectors are dropped.
98
99## Vector matching
100
101Operations between vectors attempt to find a matching element in the right-hand side
102vector for each entry in the left-hand side. There are two basic types of
103matching behavior: One-to-one and many-to-one/one-to-many.
104
105### One-to-one vector matches
106
107**One-to-one** finds a unique pair of entries from each side of the operation.
108In the default case, that is an operation following the format `vector1 <operator> vector2`.
109Two entries match if they have the exact same set of labels and corresponding values.
110The `ignoring` keyword allows ignoring certain labels when matching, while the
111`on` keyword allows reducing the set of considered labels to a provided list:
112
113    <vector expr> <bin-op> ignoring(<label list>) <vector expr>
114    <vector expr> <bin-op> on(<label list>) <vector expr>
115
116Example input:
117
118    method_code:http_errors:rate5m{method="get", code="500"}  24
119    method_code:http_errors:rate5m{method="get", code="404"}  30
120    method_code:http_errors:rate5m{method="put", code="501"}  3
121    method_code:http_errors:rate5m{method="post", code="500"} 6
122    method_code:http_errors:rate5m{method="post", code="404"} 21
123
124    method:http_requests:rate5m{method="get"}  600
125    method:http_requests:rate5m{method="del"}  34
126    method:http_requests:rate5m{method="post"} 120
127
128Example query:
129
130    method_code:http_errors:rate5m{code="500"} / ignoring(code) method:http_requests:rate5m
131
132This returns a result vector containing the fraction of HTTP requests with status code
133of 500 for each method, as measured over the last 5 minutes. Without `ignoring(code)` there
134would have been no match as the metrics do not share the same set of labels.
135The entries with methods `put` and `del` have no match and will not show up in the result:
136
137    {method="get"}  0.04            //  24 / 600
138    {method="post"} 0.05            //   6 / 120
139
140### Many-to-one and one-to-many vector matches
141
142**Many-to-one** and **one-to-many** matchings refer to the case where each vector element on
143the "one"-side can match with multiple elements on the "many"-side. This has to
144be explicitly requested using the `group_left` or `group_right` modifier, where
145left/right determines which vector has the higher cardinality.
146
147    <vector expr> <bin-op> ignoring(<label list>) group_left(<label list>) <vector expr>
148    <vector expr> <bin-op> ignoring(<label list>) group_right(<label list>) <vector expr>
149    <vector expr> <bin-op> on(<label list>) group_left(<label list>) <vector expr>
150    <vector expr> <bin-op> on(<label list>) group_right(<label list>) <vector expr>
151
152The label list provided with the group modifier contains additional labels from
153the "one"-side to be included in the result metrics. For `on` a label can only
154appear in one of the lists. Every time series of the result vector must be
155uniquely identifiable.
156
157_Grouping modifiers can only be used for
158[comparison](#comparison-binary-operators) and
159[arithmetic](#arithmetic-binary-operators). Operations as `and`, `unless` and
160`or` operations match with all possible entries in the right vector by
161default._
162
163Example query:
164
165    method_code:http_errors:rate5m / ignoring(code) group_left method:http_requests:rate5m
166
167In this case the left vector contains more than one entry per `method` label
168value. Thus, we indicate this using `group_left`. The elements from the right
169side are now matched with multiple elements with the same `method` label on the
170left:
171
172    {method="get", code="500"}  0.04            //  24 / 600
173    {method="get", code="404"}  0.05            //  30 / 600
174    {method="post", code="500"} 0.05            //   6 / 120
175    {method="post", code="404"} 0.175           //  21 / 120
176
177_Many-to-one and one-to-many matching are advanced use cases that should be carefully considered.
178Often a proper use of `ignoring(<labels>)` provides the desired outcome._
179
180## Aggregation operators
181
182Prometheus supports the following built-in aggregation operators that can be
183used to aggregate the elements of a single instant vector, resulting in a new
184vector of fewer elements with aggregated values:
185
186* `sum` (calculate sum over dimensions)
187* `min` (select minimum over dimensions)
188* `max` (select maximum over dimensions)
189* `avg` (calculate the average over dimensions)
190* `stddev` (calculate population standard deviation over dimensions)
191* `stdvar` (calculate population standard variance over dimensions)
192* `count` (count number of elements in the vector)
193* `count_values` (count number of elements with the same value)
194* `bottomk` (smallest k elements by sample value)
195* `topk` (largest k elements by sample value)
196* `quantile` (calculate φ-quantile (0 ≤ φ ≤ 1) over dimensions)
197
198These operators can either be used to aggregate over **all** label dimensions
199or preserve distinct dimensions by including a `without` or `by` clause. These
200clauses may be used before or after the expression.
201
202    <aggr-op> [without|by (<label list>)] ([parameter,] <vector expression>)
203
204or
205
206    <aggr-op>([parameter,] <vector expression>) [without|by (<label list>)]
207
208`label list` is a list of unquoted labels that may include a trailing comma, i.e.
209both `(label1, label2)` and `(label1, label2,)` are valid syntax.
210
211`without` removes the listed labels from the result vector, while
212all other labels are preserved the output. `by` does the opposite and drops
213labels that are not listed in the `by` clause, even if their label values are
214identical between all elements of the vector.
215
216`parameter` is only required for `count_values`, `quantile`, `topk` and
217`bottomk`.
218
219`count_values` outputs one time series per unique sample value. Each series has
220an additional label. The name of that label is given by the aggregation
221parameter, and the label value is the unique sample value.  The value of each
222time series is the number of times that sample value was present.
223
224`topk` and `bottomk` are different from other aggregators in that a subset of
225the input samples, including the original labels, are returned in the result
226vector. `by` and `without` are only used to bucket the input vector.
227
228Example:
229
230If the metric `http_requests_total` had time series that fan out by
231`application`, `instance`, and `group` labels, we could calculate the total
232number of seen HTTP requests per application and group over all instances via:
233
234    sum without (instance) (http_requests_total)
235
236Which is equivalent to:
237
238     sum by (application, group) (http_requests_total)
239
240If we are just interested in the total of HTTP requests we have seen in **all**
241applications, we could simply write:
242
243    sum(http_requests_total)
244
245To count the number of binaries running each build version we could write:
246
247    count_values("version", build_version)
248
249To get the 5 largest HTTP requests counts across all instances we could write:
250
251    topk(5, http_requests_total)
252
253## Binary operator precedence
254
255The following list shows the precedence of binary operators in Prometheus, from
256highest to lowest.
257
2581. `^`
2592. `*`, `/`, `%`
2603. `+`, `-`
2614. `==`, `!=`, `<=`, `<`, `>=`, `>`
2625. `and`, `unless`
2636. `or`
264
265Operators on the same precedence level are left-associative. For example,
266`2 * 3 % 2` is equivalent to `(2 * 3) % 2`. However `^` is right associative,
267so `2 ^ 3 ^ 2` is equivalent to `2 ^ (3 ^ 2)`.
268