Prometheus Matching Operators

Prometheus, an open-source monitoring and alerting system, provides several matching operators to filter and aggregate time series data. These operators are used in Prometheus's query language, PromQL (Prometheus Query Language), to define the selection criteria for metrics. Here are the commonly used matching operators in Prometheus:

1. Equality (=): Matches time series with labels that have the exact specified value.

2. Inequality (!=): Matches time series with labels that do not have the specified value.

3. Regular expression (=~): Matches time series with labels that match the specified regular expression.

4. Negated regular expression (!~): Matches time series with labels that do not match the specified regular expression.

5. Set membership ({labelname}): Matches time series that have a specific label name, regardless of its value.

6. Range matching ({labelname="value1"-"value2"}): Matches time series with labels that have values falling within the specified range.

7. Aggregation (sum, avg, min, max, etc.): Allows aggregating multiple time series into a single result using various aggregation functions.

These operators can be combined to create complex queries for selecting, filtering, and aggregating time series data in Prometheus. They provide flexibility in defining the desired metrics and conditions to monitor and analyze your system.

Examples

Here are examples for each of the matching operators in Prometheus:

1. Equality (=):

http_requests_total{method="GET"}
It matches the time series with the label "method" having the value "GET". This example selects all the time series representing HTTP GET requests.

2. Inequality (!=):

http_requests_total{status!="200"}
It matches the time series with the label "status" not equal to "200". This example selects all the time series representing HTTP requests with a status other than 200.

3. Regular expression (=~):

http_requests_total{path=~"/api/v[0-9]+/.*"}
It matches the time series with the label "path" matching the regular expression "/api/v[0-9]+/.*". This example selects all the time series representing HTTP requests with paths starting with "/api/v" followed by a version number.

4. Negated regular expression (!~):

http_requests_total{path!~"/admin/.*"}
It matches the time series with the label "path" not matching the regular expression "/admin/.*". This example selects all the time series representing HTTP requests excluding those with paths starting with "/admin/".

5. Set membership ({labelname}):

{job="prometheus",instance="localhost:9090"}
It matches the time series with the labels "job" and "instance" having the specified values. This example selects all the time series belonging to the Prometheus server running on localhost:9090.

6. Range matching ({labelname="value1"-"value2"}):

http_requests_total{response_time="0.1-1.0"}
It matches the time series with the label "response_time" falling within the range from 0.1 to 1.0. This example selects all the time series representing HTTP requests with response times between 0.1 and 1.0 seconds.

7. Aggregation (sum, avg, min, max, etc.):

sum(http_requests_total) by (job)
It aggregates all the time series representing HTTP requests, summing them up based on the "job" label. This example calculates the total number of HTTP requests per job.

These examples illustrate how each matching operator can be used in PromQL queries to filter and aggregate time series data in Prometheus.