{{ define "default.message" }}{{- if gt (len .Alerts.Firing) 0 -}}{{- range $index, $alert := .Alerts.Firing -}}{{- if eq $index 0 -}}Detail: {{ $alert.Labels.alertname }}# Here I'm going to zero $alert{{- end }}{{- end }}{{- end }}{{- end }}你好,我想歸零.Alerts.Firing,這也是$alert在迭代之后。我是golang的新手,請(qǐng)幫忙,謝謝。補(bǔ)充:其實(shí)就是一個(gè)Prometheusalert模板。原始日期由創(chuàng)建alertmanager并發(fā)送至receiver(微信)。這是微信模板 {{ define "wechat.default.message" }} {{- if gt (len .Alerts.Firing) 0 -}} {{- range $index, $alert := .Alerts.Firing -}} {{- if eq $index 0 -}} Detail: {{ $alert.Labels.alertname }} {{- end }} ---- Problem started ------ Message: {{ $alert.Annotations.description }} Time: {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }} {{ if gt (len $alert.Labels.instance) 0 }}ip: {{ $alert.Labels.instance }};{{- end }} {{- if gt (len $alert.Labels.namespace) 0 }}namespace: {{ $alert.Labels.namespace }};{{- end }} {{- if gt (len $alert.Labels.node) 0 }}Node: {{ $alert.Labels.node }};{{- end }} {{- if gt (len $alert.Labels.pod_name) 0 }}Pod: {{ $alert.Labels.pod_name }}{{- end }} ----------------- {{ $alert :="" }} {{- end }} {{- end }} {{- if gt (len .Alerts.Resolved) 0 -}} {{- range $index, $alert := .Alerts.Resolved -}} {{- if eq $index 0 }} !!!! Problem resolved !!!!! Detail: {{ $alert.Labels.alertname }} {{- end }} Started_at: {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }} Resolved_at: {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }} ------- {{ if gt (len $alert.Labels.instance) 0 -}}ip: {{ $alert.Labels.instance }};{{- end }} {{- if gt (len $alert.Labels.namespace) 0 -}}namespace: {{ $alert.Labels.namespace }};{{- end }} {{- if gt (len $alert.Labels.node) 0 -}}Node: {{ $alert.Labels.node }};{{- end }} {{- if gt (len $alert.Labels.pod_name) 0 -}}Pod: {{ $alert.Labels.pod_name }}{{- end }} {{ $alert :="" }}所以很難看到發(fā)送的原始數(shù)據(jù)alertmanager。我能看到的只有這個(gè)模板渲染的結(jié)果。請(qǐng)看下面。
將 Golang 模板中的對(duì)象置零
胡說(shuō)叔叔
2022-12-19 17:57:00