将字符串附加到 grafana 查询中的变量?

append string to variable in a grafana query?

在 Grafana 中,我有一个变量 $topic 的下拉列表,其值为 "topic_A" "topic_B"

"topic_A" 被选中所以 $topic = "topic_A"

我想使用

查询prometheus
function{topic=$topic}

而且效果很好。

我将如何实施

function{topic="$topic" + "_ERROR"}

(失败)如果选择了 "topic_A",我想查询的是 "topic_A_ERROR"。

如何在查询中组合变量 $topic 和字符串“_ERROR”?

更新 2020-08-17:

Grafana 变量有新语法,新格式是在美元符号后使用花括号:

function{topic=~"${topic}_ERROR"}

双括号语法已弃用,很快就会被删除。

现在您还可以定义变量的格式,这可能有助于解决一些空格字符问题。示例:${topic:raw}

文档:https://grafana.com/docs/grafana/latest/variables/syntax/


如果你想在中间包含文本,你需要使用不同的语法:

function{topic=~"[[topic]]_ERROR"}

不仅要注意双括号,还要注意从 ==~ 的变化。它记录在我评论末尾的 link 中,基本上它说:

When the Multi-value or Include all value options are enabled, Grafana converts the labels from plain text to a regex compatible string. Which means you have to use =~ instead of =.

你可以在这里查看官方解释: https://grafana.com/docs/grafana/latest/features/datasources/prometheus/#using-variables-in-queries

在较新的 grafana 中,您应该添加 ${varname} 和 () 正则表达式

[[varname]] is nearly deprecated this will perform the multivalue variable in prometheus

func{instance=~"(${topic})_ERROR"}

它通常用于实例指标

比如说实例 [198.10.99.9,198.10.99.10] 如果您的收集器在 9100 上 node_memory_MemTotal_bytes{instance=~"(${instance}):9100"} 如果您的收集器在另一个端口上,例如 9111 another_metric{instance=~"(${instance}):9111"}

过去遇到过这样的问题。

第一个出口商 - 在我的港口定制一个 9100

上的第二个 node_exporter

在 Grafana 变量中,如:

host label_values(asterisk_active_calls, host)

在仪表板中: CPU忙

100 - (avg(irate(node_cpu_seconds_total{instance=~"$host:9100",mode="idle"}[30m])) * 100)

instance=~"$host:9100" - working like a charm