英文:
Zero an object in Golang Template
问题
{{ define "default.message" }}
{{- if gt (len .Alerts.Firing) 0 -}}
{{- range $index, $alert := .Alerts.Firing -}}
{{- if eq $index 0 -}}
详细信息:{{ $alert.Labels.alertname }}
在这里我将 $alert 置为零
{{- end }}
{{- end }}
{{- end }}
{{- end }}
你好,我想在迭代结束后将 .Alerts.Firing
,也就是 $alert
置为零。
我对 golang 还不熟悉,请帮忙,谢谢。
补充:
实际上这是 Prometheus
告警的模板。原始数据由 alertmanager
创建并发送给 receiver
(微信)。
以下是微信模板:
{{ define "wechat.default.message" }}
{{- if gt (len .Alerts.Firing) 0 -}}
{{- range $index, $alert := .Alerts.Firing -}}
{{- if eq $index 0 -}}
详细信息:{{ $alert.Labels.alertname }}
{{- end }}
---- 问题开始 ------
消息:{{ $alert.Annotations.description }}
时间:{{ ($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 }}命名空间:{{ $alert.Labels.namespace }};{{- end }}
{{- if gt (len $alert.Labels.node) 0 }}节点:{{ $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 }}
!!!! 问题已解决 !!!!!
详细信息:{{ $alert.Labels.alertname }}
{{- end }}
开始时间:{{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
解决时间:{{ ($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 -}}命名空间:{{ $alert.Labels.namespace }};{{- end }}
{{- if gt (len $alert.Labels.node) 0 -}}节点:{{ $alert.Labels.node }};{{- end }}
{{- if gt (len $alert.Labels.pod_name) 0 -}}Pod:{{ $alert.Labels.pod_name }}{{- end }}
{{ $alert := "" }}
-----------------
{{- end }}
{{- end }}
{{- end }}
所以很难看到 alertmanager
发送的原始数据。我只能看到这个模板渲染后的结果。请看下面的示例。
################# 第一个告警 #########
##### 我们可以看到 232.20 处于 'resolved' 状态 ###
详细信息:节点宕机
---- 问题开始 ------
消息:节点宕机
VALUE = 0
LABELS = 172.16.232.18:9100
时间:2022-06-02 14:47:51
IP:172.16.232.18:9100;
-----------------
!!!! 问题已解决 !!!!!
详细信息:节点宕机
开始时间:2022-06-02 14:47:21
解决时间:2022-06-02 14:47:21
-------
IP:172.16.232.20:9100;
-----------------
##################### 第二个告警 #######
####### 232.20 仍然处于 resolved 状态,看起来是重复的,所以我要将其置为零 ####
!!!! 问题已解决 !!!!!
详细信息:节点宕机
开始时间:2022-06-02 14:47:51
解决时间:2022-06-02 14:47:51
-------
IP:172.16.232.18:9100;
-----------------
开始时间:2022-06-02 14:47:21
解决时间:2022-06-02 14:47:21
-------
IP:172.16.232.20:9100;
-----------------
补充:
- 第一个告警的原始数据。
[FIRING:1] 节点宕机 (powerdns prod node_exporter critical)
实例宕机。
触发的告警:
标签:
- alertname = 节点宕机
- app = powerdns
- env = prod
- instance = 172.16.232.18:9100
- job = node_exporter
- severity = critical
注释:
- description = 节点宕机
VALUE = 0
LABELS = 172.16.232.18:9100
- summary = 实例宕机。
来源:http://pt-prometheus-server-85944ccff6-qvtc4:9090/graph?g0.expr=up%7Bjob%3D%22node_exporter%22%7D+%3D%3D+0&g0.tab=1
已解决的告警:
标签:
- alertname = 节点宕机
- app = powerdns
- env = prod
- instance = 172.16.232.20:9100
- job = node_exporter
- severity = critical
注释:
- description = 节点宕机
VALUE = 0
LABELS = 172.16.232.20:9100
- summary = 实例宕机。
来源:http://pt-prometheus-server-85944ccff6-qvtc4:9090/graph?g0.expr=up%7Bjob%3D%22node_exporter%22%7D+%3D%3D+0&g0.tab=1
AlertmanagerUrl:
http://alertmgr.pacific-textiles.com/#/alerts?receiver=wechat
和
2. 第二个告警的原始数据。
[RESOLVED] 节点宕机 (powerdns prod node_exporter critical)
实例宕机。
已解决的告警:
标签:
- alertname = 节点宕机
- app = powerdns
- env = prod
- instance = 172.16.232.18:9100
- job = node_exporter
- severity = critical
注释:
- description = 节点宕机
VALUE = 0
LABELS = 172.16.232.18:9100
- summary = 实例宕机。
来源:http://pt-prometheus-server-85944ccff6-qvtc4:9090/graph?g0.expr=up%7Bjob%3D%22node_exporter%22%7D+%3D%3D+0&g0.tab=1
标签:
- alertname = 节点宕机
- app = powerdns
- env = prod
- instance = 172.16.232.20:9100
- job = node_exporter
- severity = critical
注释:
- description = 节点宕机
VALUE = 0
LABELS = 172.16.232.20:9100
- summary = 实例宕机。
来源:http://pt-prometheus-server-85944ccff6-qvtc4:9090/graph?g0.expr=up%7Bjob%3D%22node_exporter%22%7D+%3D%3D+0&g0.tab=1
AlertmanagerUrl:
http://alertmgr.pacific-textiles.com/#/alerts?receiver=wechat
英文:
{{ 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 }}
hello, I want to zero .Alerts.Firing
, which is also $alert
after iteration over it.
I'm new to golang, Please help, thanks.
Supplement:
Actually it's a template of Prometheus
alert. The orginal date is created by alertmanager
and send to receiver
( wechat).
Here is wechat template
{{ 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 :="" }}
-----------------
{{- end }}
{{- end }}
{{- end }}
So it's hard to see the raw data sent by alertmanager
. What I can see only it the result rendered by this template. Please see below.
################# The first alert #########
##### As we can see 232.20 is in the state of 'resolved' ###
Detail: Node is down
---- Problem started ------
Message: Node is down
VALUE = 0
LABELS = 172.16.232.18:9100
Time: 2022-06-02 14:47:51
ip: 172.16.232.18:9100;
-----------------
!!!! Problem resolved !!!!!
Detail: Node is down
Started_at: 2022-06-02 14:47:21
Resolved_at: 2022-06-02 14:47:21
-------
ip: 172.16.232.20:9100;
-----------------
##################### The second alert #######
####### 232.20 is still in the state of resolved, it seems it's duplicated, that's why I'm going to zero it ####
!!!! Problem resolved !!!!!
Detail: Node is down
Started_at: 2022-06-02 14:47:51
Resolved_at: 2022-06-02 14:47:51
-------
ip: 172.16.232.18:9100;
-----------------
Started_at: 2022-06-02 14:47:21
Resolved_at: 2022-06-02 14:47:21
-------
ip: 172.16.232.20:9100;
-----------------
Supplyment:
- raw data of first alert.
[FIRING:1] Node is down (powerdns prod node_exporter critical)
instance is down.
Alerts Firing:
Labels:
- alertname = Node is down
- app = powerdns
- env = prod
- instance = 172.16.232.18:9100
- job = node_exporter
- severity = critical
Annotations:
- description = Node is down
VALUE = 0
LABELS = 172.16.232.18:9100
- summary = instance is down.
Source: http://pt-prometheus-server-85944ccff6-qvtc4:9090/graph?g0.expr=up%7Bjob%3D%22node_exporter%22%7D+%3D%3D+0&g0.tab=1
Alerts Resolved:
Labels:
- alertname = Node is down
- app = powerdns
- env = prod
- instance = 172.16.232.20:9100
- job = node_exporter
- severity = critical
Annotations:
- description = Node is down
VALUE = 0
LABELS = 172.16.232.20:9100
- summary = instance is down.
Source: http://pt-prometheus-server-85944ccff6-qvtc4:9090/graph?g0.expr=up%7Bjob%3D%22node_exporter%22%7D+%3D%3D+0&g0.tab=1
AlertmanagerUrl:
http://alertmgr.pacific-textiles.com/#/alerts?receiver=wechat
and
2. raw data of second alert.
[RESOLVED] Node is down (powerdns prod node_exporter critical)
instance is down.
Alerts Resolved:
Labels:
- alertname = Node is down
- app = powerdns
- env = prod
- instance = 172.16.232.18:9100
- job = node_exporter
- severity = critical
Annotations:
- description = Node is down
VALUE = 0
LABELS = 172.16.232.18:9100
- summary = instance is down.
Source: http://pt-prometheus-server-85944ccff6-qvtc4:9090/graph?g0.expr=up%7Bjob%3D%22node_exporter%22%7D+%3D%3D+0&g0.tab=1
Labels:
- alertname = Node is down
- app = powerdns
- env = prod
- instance = 172.16.232.20:9100
- job = node_exporter
- severity = critical
Annotations:
- description = Node is down
VALUE = 0
LABELS = 172.16.232.20:9100
- summary = instance is down.
Source: http://pt-prometheus-server-85944ccff6-qvtc4:9090/graph?g0.expr=up%7Bjob%3D%22node_exporter%22%7D+%3D%3D+0&g0.tab=1
AlertmanagerUrl:
http://alertmgr.pacific-textiles.com/#/alerts?receiver=wechat
答案1
得分: 0
只是意识到一个对象不能通过零来解决。因为:
- 在微信中没有办法保留上次“已解决”的主机232.20。
- 下次原始数据仍然包含“已解决”的主机232.20,所以无论如何都无法删除它。
- 因此,输出也无法改变。
谢谢。
英文:
Just relealize it cannot be resolved by zero an object. Because:
- There is no way to keep last 'resolved' host 232.20 in wechat.
- Next time the raw data still contains 'resolved' host 232.20, so it cannot be removed by any means.
- so the output cannot be changed, either.
Thanks.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论