Label Matching Operators
What you'll learn: How to filter Prometheus metrics precisely using equality, inequality, regex, and negative regex matchers on label values.
In the previous lessons, you learned how to select metrics with instant and range vector selectors. Now it's time to refine those selections by filtering on specific label values. Label matching operators let you slice your time-series data exactly how you need it—like choosing specific servers, environments, or error codes from your metrics stream.
The Four Matching Operators
Prometheus provides four operators to match labels:
= (Equality): Select series where a label exactly matches a value.
http_requests_total{method="GET"}
Only GET requests are returned.
!= (Inequality): Exclude series where a label matches a value.
http_requests_total{status!="200"}
All non-200 responses (errors, redirects, etc.).
=~ (Regex Match): Select series where a label matches a regular expression.
http_requests_total{path=~"/api/v1/.*"}
All API v1 endpoints, regardless of their specific path.
!~ (Negative Regex): Exclude series matching a regex pattern.
http_requests_total{instance!~".*test.*"}
Excludes any instance with "test" in its name.
Combining Matchers
You can chain multiple matchers with commas (implicit AND logic):
http_requests_total{method="POST", status=~"5..", environment!="dev"}
This finds POST requests with 5xx errors, excluding the dev environment.
Key Takeaway:
Label matching operators (=, !=, =~, !~) are your precision tools for filtering Prometheus metrics—use equality for exact matches, inequality to exclude values, and regex patterns for flexible, pattern-based selection.