In Grafana, is there a way to re-use parts of Prometheus queries with templated parameters?

huangapple go评论53阅读模式
英文:

In Grafana, is there a way to re-use parts of Prometheus queries with templated parameters?

问题

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?

作为示例,我有以下查询:

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

现在我希望对于指标 erlang_vm_memory_ets_tables(以及许多其他指标)使用相同的查询。我希望能够将查询存储在某个地方,并使用可参数化的指标名称,例如 label_replace([[metric_name]]{instance=~"[^.]*${host}.*",env="${env}",app="${app}"}, "host", "$1", "instance", "[^.]*?(\\d+).*")。这种方式是否可行?还是有其他 Grafana 本地的方法可以重用 Prometheus 查询的部分?

英文:

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?

答案1

得分: 1

在这种情况下最好的想法是添加重新标记配置,如下所示:

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

它将根据正则表达式自动将标签host添加到每个指标中。

如果在抓取时无法重新标记,唯一重复使用查询部分的方法是创建几个类型为常量的仪表板变量:

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

并使用以下查询:

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

Grafana 将在将查询发送到 Prometheus 之前简单地将它们构成。

遗憾的是,这些是仪表板变量,这将仅在单个仪表板内起作用,并且需要在每个仪表板上重复。

另一个选择是创建自己的插件,该插件将提供您定义的格式的替代,但我怀疑在大多数情况下(如果有的话)这不会是一个明智的选择。

英文:

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.

huangapple
  • 本文由 发表于 2023年5月10日 18:24:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定