首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Golang模板中使对象为零

在Golang模板中使对象为零
EN

Stack Overflow用户
提问于 2022-06-02 07:01:59
回答 1查看 131关注 0票数 0
代码语言:javascript
复制
{{ 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。我刚来金刚,请帮帮忙,谢谢。

补充:实际上它是Prometheus警报的模板。原始日期由alertmanager创建并发送给receiver(微信)。这是微信的模板

代码语言:javascript
复制
    {{ 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 }}

因此,很难看到alertmanager发送的原始数据。我只能看到的是这个模板所呈现的结果。请看下面的内容。

代码语言:javascript
复制
################# 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;

-----------------

补充:

  1. 第一警报的原始数据.

代码语言:javascript
复制
[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

  1. 第二警报的原始数据.

代码语言:javascript
复制
[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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-02 09:41:29

只要释放它,就不能用零来解析一个对象。因为:

  1. 没有办法在微信保存最后的“已解析”主机232.20。
  2. 下一次原始数据仍然包含“已解析”主机232.20,因此不能以任何方式删除它。
  3. 因此输出也不能更改。

谢谢。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72471955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档