Return specific value if label regex not match

28 views
Skip to first unread message

Simon

unread,
Aug 12, 2022, 4:35:52 AM8/12/22
to Prometheus Developers
Hello everyone,
I have a query: probe_success{__name__=~"name1|name2|name3"}.
Prometheus does not have label __name___ = name2 and i want it return -1 if prometheus does not have that label value.
How can i do that?

Matthias Rampke

unread,
Aug 12, 2022, 4:46:27 AM8/12/22
to Simon, Prometheus Users
Hi, this mailing list is for development of Prometheus and related projects. Since your question is about usage, I'm moving the thread to the prometheus-users mailing list.

To answer your question, in general a regular expression can have an unbounded number of matches, so Prometheus cannot automatically determine from the matcher alone that name2 should be there.

You can set up recording rules with all the names you expect to be there:

- record: probe_success:expected_name
  expr: 1
  labels:
    name: name1
- record: probe_success:expected_name
  expr: 1
  labels:
    name: name2
- record: probe_success:expected_name
  expr: 1
  labels:
    name: name3

and then use it in the your query like

probe_success{name=~"name1|name2|name3"} or -1*probe_success:expected_name

I am using the value 1 for this metric because it is customary to do that for "metadata metrics" like this – you can multiply it with the desired value in the query like I did here.

Another thing about your query – you are matching __name__ but that is a special label representing the metric name. Since your query specifies probe_success as the metric name, the two are in conflict.

/MR



--
You received this message because you are subscribed to the Google Groups "Prometheus Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-devel...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-developers/eccbaad3-9bb0-41a0-a626-25403d34a4d9n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages