How to write OR with Prometheus

I have been trying to figure out how to write OR function with prometheus inside Grafana. I ahve been reading Operators Prometheus and so far I was only able to get a positive sum where the negative doesn’t seem to work.


The positive scenario is that when the response is either 200 or 404 sum(scraper_request_count_total{http_status=~"200|404"}) then its a successful request and everything else is failed requests. I thought it would work by doing sum(scraper_request_count_total{http_status!="200|404"}) but it doesn’t, that gives me an ouput that its failed request even though the request is returning 200.

My question is, how can I write a “negative” OR function where I want the response to NOT be either 200 OR 404, meaning all other response status code is counted as false if not 200/404

  • What Grafana version and what operating system are you using?

Grafana 6, Debian

the =~ operator is doing a regex (regular expression) matching. So for the reverse of that it would be !~

The operators = or != are just doing EXACT string equal/not equal matching, so you can’t use regex patterns like the pipe | in the comparison.

See here:

Is it correct then to do !~“200|404” if you dont want it to be either 200 or 404?

This topic was automatically closed after 365 days. New replies are no longer allowed.