猿问

将 Golang 模板中的对象置零

{{ 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的新手,请帮忙,谢谢。


补充:其实就是一个Prometheusalert模板。原始日期由创建alertmanager并发送至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 :="" }}

所以很难看到发送的原始数据alertmanager。我能看到的只有这个模板渲染的结果。请看下面。


胡说叔叔
浏览 99回答 1
1回答

九州编程

只是 relealize 它不能通过零对象来解决。因为:无法在微信中保留最后一个“已解析”的主机 232.20。下次原始数据仍包含“已解析”主机 232.20,因此无法以任何方式将其删除。所以输出也不能改变。谢谢。
随时随地看视频慕课网APP

相关分类

Go
我要回答