英文:
Go Template if condition
问题
如何将and
和eq/ne
函数组合在一起?
我写了这段代码:
{{ define "opsgenie.default.tmpl" }}
<font size="+0"><b>{{.CommonLabels.alertname }}</b></font>
{{- range $i, $alert := .Alerts }}
<font size="+0">{{ .Annotations.description }}</font>
{{- end -}}
{{- "\n" -}}
{{- "\n" -}}
{{- if and eq .CommonLabels.infoalert "true" eq .CommonLabels.topic "database" -}}
Grafana: https://{{ .CommonLabels.url }}
{{- "\n" -}}{{- end -}}
{{- if and ne .CommonLabels.infoalert "true" eq .CommonLabels.topic "database" -}}
Database:
• https://{{ .CommonLabels.url }}/
• https://{{ .CommonLabels.url }}/
{{- "\n" -}}{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
目标是:
- 如果我的警报同时包含
infoalert: true
和topic: database
标签,则只显示Grafana链接。 - 如果我的警报只包含
topic: database
标签,但不包含infoalert: true
标签,则只显示数据库链接。
看起来条件语句{{- if and eq .CommonLabels.infoalert "true" eq .CommonLabels.topic "database" -}}
的语法不正确,因为当触发警报时,我在alertmanager.log中收到以下错误:
notify retry canceled due to unrecoverable error after 1 attempts: templating error: template: email.tmpl:24:17: executing "opsgenie.default.tmpl" at <eq>: wrong number of args for eq: want at least 1 got 0
英文:
how can i combine and
and eq/ne
functions together?
I wrote this snippet
{{ define "opsgenie.default.tmpl" }}
<font size="+0"><b>{{.CommonLabels.alertname }}</b></font>
{{- range $i, $alert := .Alerts }}
<font size="+0">{{ .Annotations.description }}</font>
{{- end -}}
{{- "\n" -}}
{{- "\n" -}}
{{- if and eq .CommonLabels.infoalert "true" eq .CommonLabels.topic "database" -}}
Grafana: https://{{ .CommonLabels.url }}
{{- "\n" -}}{{- end -}}
{{- if and ne .CommonLabels.infoalert "true" eq .CommonLabels.topic "database" -}}
Database:
• https://{{ .CommonLabels.url }}/
• https://{{ .CommonLabels.url }}/
{{- "\n" -}}{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
The goal is:
- if my alert contains both labels
infoalert: true
andtopic: database
then show only Grafana link - if my alert contains only label
topic: database
but notinfoalert: true
then show only Databsse link
It looks like the syntax for condition {{- if and eq .CommonLabels.infoalert "true" eq .CommonLabels.topic "database" -}}
is not correct because i get this error in alertmanager.log when alert is fired:
notify retry canceled due to unrecoverable error after 1 attempts: templating error: template: email.tmpl:24:17: executing \"opsgenie.default.tmpl\" at <eq>: wrong number of args for eq: want at least 1 got 0
答案1
得分: 1
只需使用括号将表达式分组:
{{- if and (eq .CommonLabels.infoalert "true") (eq .CommonLabels.topic "database") -}}
{{- if and (ne .CommonLabels.infoalert "true") (eq .CommonLabels.topic "database") -}}
请参考以下可测试的示例:
func main() {
t := template.Must(template.New("").Parse(src))
m := map[string]interface{}{
"infoalert": "true",
"topic": "database",
}
if err := t.Execute(os.Stdout, m); err != nil {
panic(err)
}
fmt.Println("Second round")
m["infoalert"] = "false"
if err := t.Execute(os.Stdout, m); err != nil {
panic(err)
}
}
const src = `
{{- if and (eq .infoalert "true") (eq .topic "database") -}}
infoalert is true and topic is database
{{- end -}}
{{- if and (ne .infoalert "true") (eq .topic "database") -}}
infoalert is NOT true and topic is database
{{ end }}
`
这将输出以下内容(在Go Playground上尝试):
infoalert is true and topic is database
Second round
infoalert is NOT true and topic is database
英文:
Simply use parenthesis to group the expressions:
{{- if and (eq .CommonLabels.infoalert "true") (eq .CommonLabels.topic "database") -}}
{{- if and (ne .CommonLabels.infoalert "true") (eq .CommonLabels.topic "database") -}}
See this testable example:
func main() {
t := template.Must(template.New("").Parse(src))
m := map[string]any{
"infoalert": "true",
"topic": "database",
}
if err := t.Execute(os.Stdout, m); err != nil {
panic(err)
}
fmt.Println("Second round")
m["infoalert"] = "false"
if err := t.Execute(os.Stdout, m); err != nil {
panic(err)
}
}
const src = `
{{- if and (eq .infoalert "true") (eq .topic "database") -}}
infoalert is true and topic is database
{{- end -}}
{{- if and (ne .infoalert "true") (eq .topic "database") -}}
infoalert is NOT true and topic is database
{{ end }}
`
This will output (try it on the Go Playground):
infoalert is true and topic is database
Second round
infoalert is NOT true and topic is database
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论