解析Grafana中的多值自定义变量

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

Parsing a multi valued custom variable in Grafana

问题

以下是代码片段:

max_over_time(PrometheusMetric_$Region{job="JOBNAME"}[1m])

这段代码使用了在Grafana中创建的名为Region的自定义变量,该变量可以取值{North_Europe, East_US, Southeast_Asia}。我希望在Grafana上查看指标时,能够选择同时查看2个或更多地区的图表。

当我选择2个或更多地区时,会出现以下错误:
bad_data: 1:89: parse error: unexpected character: '|'

请问有何修复建议。另外,这里的Region不是Prometheus标签,因此不能将其放在花括号内。

英文:

Following is the code snippet

max_over_time(PrometheusMetric_$Region{job="JOBNAME"}[1m])

which is taking a custom variable made in Grafana named Region which can take values from {North_Europe, East_US, Southeast_Asia}. I want that while viewing metrics on Grafana, we have the option to select 2 or more regions at a time to view the plots collectively.
When I select 2 or more regions, the following error is thrown.
bad_data: 1:89: parse error: unexpected character: '|'

What is the code fix suggested. Also Region here is not a prometheus label, so we cannot keep it inside curly braces.

答案1

得分: 1

假设您有一个具有值123的变量选择。这是在查询中如何扩展它的方式:

"$myvar" -> "(1|2|3)"

这就是unexpected character: ''|''来自的地方。

要在查询中使用多值变量,您需要使用正则匹配运算符:=~,例如:

foo=~"(1|2|3)"

幸运的是,指标名称也是一个标签,因此您可以尝试类似以下方式:

max_over_time({__name__=~"PrometheusMetric_$Region", job="JOBNAME"}[1m])
英文:

Suppose you have a variable with values 1, 2 and 3 selected. This is how it is expanded, if used in a query:

"$myvar" -> "(1|2|3)"

This is where the unexpected character: '|' comes from.

To use a multi-value variable inside a query you need to use the regex-match operator: =~, e.g.

foo=~"(1|2|3)"

Fortunately for you, metric name is also a label, so you may try something like this:

max_over_time({__name__=~"PrometheusMetric_$Region", job="JOBNAME"}[1m])

huangapple
  • 本文由 发表于 2023年3月9日 22:49:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686204.html
匿名

发表评论

匿名网友

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

确定