1

I find myself repeating very similar Prometheus queries in Grafana, with minor differences (mostly to the metric name). Is there a way to reuse these and supply the metric name as a parameter?

As an example, I have the following query:

label_replace(erlang_vm_ets_limit{instance=~"[^.]*${host}.*",env="${env}",app="${app}"}, "host", "$1", "instance", "[^.]*?(\\d+).*")

Now I want the same query for the metric erlang_vm_memory_ets_tables (and many others). I'd love to be able to store the query somewhere with a parametrizable metric name, e.g. label_replace([[metric_name]]{instance=~"[^.]*${host}.*",env="${env}",app="${app}"}, "host", "$1", "instance", "[^.]*?(\\d+).*"). Is something like that possible? Or is there another Grafana-native way to re-use parts of Prometheus queries?

Adam Lindberg
  • 16,447
  • 6
  • 65
  • 85

1 Answers1

1

The best idea in this case would be to add relabeling config like

- source_labels: [instance]
  regex: "[^.]*?(\\d+).*"
  target_label: "host"
  replacement: "$1"

It will automatically add label host to every metric based on regex.


If relabeling at the scrape time is not possible, only way to reuse parts of queries is to create a couple of dashboard variable of type Constant:

  • q_prefix with value label_replace(,
  • q_postfix with value , "host", "$1", "instance", "[^.]*?(\\d+).*")

And use query like

${q_prefix}erlang_vm_ets_limit{instance=~"[^.]*${host}.*",env="${env}",app="${app}"}${q_postfix}

Grafana will simply constitute them before sending query to Prometheus.

Sadly the is not simple way to make selectors part of this variables: Grafana doesn't support defining variable based on value of another variable.

Also, since those are dashboard variables, this will work only within single dashboard, and it will be needed to repeat for every dashboard.


One more alternative, is to create own plugin, that will provide substitution of the format you'll define, but I doubt this would be a reasonable choice in most (if any) cases.

markalex
  • 8,623
  • 2
  • 7
  • 32