11

What is the meaning of =~ operator in prometheus metrics?

Can any help me what is the exact difference between = and =~ operator?

for ex .

process_cpu_seconds_total{instance="test"} 
process_cpu_seconds_total{instance=~"test"}

The results are different.

Sagar Vaghela
  • 1,165
  • 5
  • 20
  • 38

2 Answers2

9

"=~: Select labels that regex-match the provided string (or substring).

For example, this selects all http_requests_total time series for staging, testing, and development environments and HTTP methods other than GET."

http_requests_total{environment=~"staging|testing|development",method!="GET"}

Taken from the Prometheus.io docs.

Conor
  • 3,279
  • 1
  • 21
  • 35
1

You should only use the =~ operator when comparing to a regular expression.

Example:

This will match the exact string test:

process_cpu_seconds_total{instance="test"}

This will match the regular expression test.*. Which means the string test followed or not by other character(s).

process_cpu_seconds_total{instance=~"test.*"}
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56