Asier Gomez
Asier Gomez

Reputation: 6578

How do I write an "or" logical operator on Prometheus or Grafana

I need to write a query that use any of the different jobs I define.

{job="traefik" OR job="cadvisor" OR job="prometheus"}

Is it possible to write logical binary operators?

Upvotes: 52

Views: 86567

Answers (2)

Brad Carter
Brad Carter

Reputation: 115

Doing a regex label match like {job=~"traefik|cadvisor|prometheus”} is correct, but there’s also another option that might offer a bit more flexibility for your end users.

Grafana lets you define dashboard variables that appear as drop downs at the top of your dashboards. Your users can then use these drop downs to specifiy which metric series instances they actually care about.

To define a new dashboard variable, navigate to Dashboard Settings -> Variables Tab -> New Variable. From here, you have a couple of options:

  • You could define a ‘Custom’ variable, which is where you specify a comma seperated list of values. For you, it would be traefik,cadvisor,prometheus.
  • You could also choose the ‘Query’ variable type, which will let you select a metric and label from your data source and then automatically enumerate the different values of the label. You don’t give the metric name in your question, but your label would be ‘job’.

From here, make sure to also give it a name so you can reference it in your query in a second. Additionally, from your question, it sounds like you would also probably wish to enable the “Multi-value” and “Include All” options as well.

Next, you need to update your graph query to use the variable. Assuming that you called your variable job, this would just be {job=~”$job”}.

Finally, to set the default values of your variable, you just have to select them in the drop down and save the dashboard.

Upvotes: 0

brian-brazil
brian-brazil

Reputation: 34142

Prometheus has an or logical binary operator, but what you're asking about here is vector selectors.

You can use a regex for this {job=~"traefik|cadvisor|prometheus"}, however that you want to do this is a smell.

Upvotes: 102

Related Questions