🎓 A1Learner
🔍
0 of 50 lessons completed

PromQL: Selectors and Label Matching

A selector is a metric name plus label rules in braces. It picks exactly the series you want out of thousands.

Match one route exactly

The = rule keeps only the home page series, in both its statuses.

http_requests_total{route="/"}

Match every route except one

The != rule drops the /metrics series and keeps the other five.

http_requests_total{route!="/metrics"}

Match a pattern of routes

The =~ rule keeps both API routes and drops the rest.

http_requests_total{route=~"/api.*",status="200"}

Example

curl -s -G "http://localhost:19102/api/v1/query" --data-urlencode 'query=http_requests_total{status!="200"}'

Key points

  • A selector is a metric name with label rules inside braces, like {route="/"}.
  • The = rule keeps only series whose label equals the given value.
  • The != rule keeps every series whose label differs from the given value.
  • The =~ rule keeps series whose label matches a pattern, like "/api.*".
  • The !~ rule keeps every series whose label fails to match the pattern.
  • A selector with no metric name matches every metric that carries those labels.

In this example, the selector keeps only the failed requests. Five orders failed with status 500, and one home page visit failed with status 503. The same selector in the expression browser shows exactly those two rows, and nothing else.

Note

A pattern is not a guess, so test it in the expression browser before trusting it.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

Which selector keeps only series whose route starts with /api?