Prometheus stores data in time series, and each time series has a metric name and labels (like handler, method, code, etc.). To filter data in Prometheus, we use matchers.
Let’s explore the most common matchers with simple examples.
1. Equality Matcher (=)
This checks if a label is exactly equal to the value (case sensitive).
Example
promhttp_metric_handler_requests_total{instance="localhost:9100"}
This query return the total number of requests handled by the instance "localhost:9100".
2. Negative Equality Matcher (!=)
This selects all time series except those where the label equals the value.
Example
promhttp_metric_handler_requests_total{code != "200"}
This query return the total number of requests for all handlers where code is not equal to 200.
3. Regular Expression Matcher (=~)
This allows pattern matching using regex. It's powerful when you want to match similar strings.
prometheus_http_requests_total{handler=~"/api/v1/status.+"}
Above query return all requests where the handler starts with /api/v1/status followed by anything else (like /api/v1/status/health, /api/v1/status/uptime, etc.).
4. Negative Regex Matcher (!~)
This excludes label values that match the regex.
prometheus_http_requests_total{handler!~"/api/v1/status.+"}
Above query return all requests where the handler is not starting with /api/v1/status
In summary, Match selectors are a powerful way to filter data in Prometheus. Start with simple equality, then move to regex as your queries get more advanced.
Previous Next Home
No comments:
Post a Comment