如何在LogQL查询中编写IF条件?

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

How to write an IF in LogQL query?

问题

在Grafana中,我向仪表板添加了一个排除参数。如果排除字段为空,我希望它不执行任何操作,否则排除包含在排除字段中的正则表达式的行。

我想要编写类似以下的内容:

{label="this"} ( if "$Exclude" != "" then !~ "$Exclude" else <do nothing> fi )

我该如何在LogQL中编写这个?我尝试阅读文档,但没有成功。目前,我只是将排除的默认值设置为唯一的UUID,但希望它为空。

英文:

In Grafana I added an Exclude parameter to the Dashboard. If the Exclude field is empty, I would want it to do nothing, otherwise exclude lines that contain the regex in Exclude field.

I would want to write something like:

{label=&quot;this&quot;} ( if &quot;$Exclude&quot; != &quot;&quot; then !~ &quot;$Exclude&quot; else &lt;do nothing&gt; fi )

How could I write this in LogQL? I tried reading documentation, but to no avail. Currently, I just set the default value of Exclude to a unique UUID, however would be nice for it to be empty.

答案1

得分: 2

{k8s_container_name="SOME_CONTAINER_NAME"} |
label_format custom_label = {{ if contains "GET" .httpMethod}} GET URL {{ else if contains "POST" .httpMethod}} POST URL {{end}}

英文:

You can try something like this:

{k8s_container_name=&quot;SOME_CONTAINER_NAME&quot;} |
label_format custom_label =  `
{{ if contains &quot;GET&quot; .httpMethod}} GET URL
{{ else if contains &quot;POST&quot; .httpMethod}} POST URL {{end}}`

huangapple
  • 本文由 发表于 2023年2月14日 18:49:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446767.html
匿名

发表评论

匿名网友

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

确定