在Go模板中对Alertmanager电子邮件模板进行排序

huangapple go评论79阅读模式
英文:

Sorting Alertmanager email templates in Go templating

问题

我正在尝试自定义一个使用Go HTML模板的电子邮件模板,该模板从AlertManager中使用以下结构打印警报列表:

{{ range .Alerts.Firing }}

它被插入到模板中的方式如下:

func (n *Email) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
   ...
   data = n.tmpl.Data(receiverName(ctx), groupLabels(ctx), as...)
   ...
}

Alert的定义如下:

type Alert struct {
    Labels LabelSet `json:"labels"`

    Annotations LabelSet `json:"annotations"`

    StartsAt     time.Time `json:"startsAt,omitempty"`
    EndsAt       time.Time `json:"endsAt,omitempty"`
    GeneratorURL string    `json:"generatorURL"`
}

我想对StartsAt字段进行排序。

我尝试使用sort函数,但在电子邮件模板中不可用。

{{ range sort .Alerts.Firing }}

我得到了

function "sort" not defined

有什么办法可以按StartsAt字段进行排序吗?

英文:

I'm trying to customize an Email template from AlertManager that uses a Go html template that prints a list of alerts using the following construct :

{{ range .Alerts.Firing }}

It gets inserted into the template like this :

func (n *Email) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
   ...
   data = n.tmpl.Data(receiverName(ctx), groupLabels(ctx), as...)
   ...
}

Alert being defined like this :

type Alert struct {
	Labels LabelSet `json:"labels"`

	Annotations LabelSet `json:"annotations"`

	StartsAt     time.Time `json:"startsAt,omitempty"`
	EndsAt       time.Time `json:"endsAt,omitempty"`
	GeneratorURL string    `json:"generatorURL"`
}

I would like to do the sorting on the StartsAt field.

I tried using the sort function but it wasn't available in the email template.

{{ range sort .Alerts.Firing }}

I'm getting

function \"sort\" not defined

Any ideas on how I can get it to sort on StartsAt ?

答案1

得分: 2

在执行模板之前,对警报进行排序。这样做更容易,而且模板不应该改变其要显示的数据。

示例:

type ByStart []*types.Alert

func (a ByStart) Len() int           { return len(a) }
func (a ByStart) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByStart) Less(i, j int) bool { return a[i].StartAt.Before(a[j].StartAt) }

func (n *Email) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
    ...
    sort.Sort(ByStart(as))
    data = n.tmpl.Data(receiverName(ctx), groupLabels(ctx), as...)
    ...
}

编辑:

默认情况下,模板不支持排序功能。你可以注册自定义函数,以便从模板中调用这些函数,但必须在解析模板之前从 Go 代码中完成此操作(而不是从模板文本中;参见Template.Funcs())。这是因为模板必须是静态可分析的,在解析模板文本时,了解哪些自定义函数是有效的是关键。

仅凭模板文本而没有自定义函数的帮助,你无法实现这一点。

英文:

Sort the alerts before you pass it to template execution. It's easier, also a template should not alter the data it's destined to display.

Example:

type ByStart []*types.Alert

func (a ByStart) Len() int           { return len(a) }
func (a ByStart) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByStart) Less(i, j int) bool { return a[i].StartAt.Before(a[j].StartAt) }

func (n *Email) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
    ...
    sort.Sort(ByStart(as))
    data = n.tmpl.Data(receiverName(ctx), groupLabels(ctx), as...)
    ...
}

Edit:

Sorting functionality is not available from templates by default. You can register custom functions that can be called from templates, but this must be done prior to parsing the templates, and from Go code (not from the template text; see Template.Funcs()). This is so because templates must be statically analyzable, and knowing what custom functions are valid is key when parsing the template text.

Just from the template text, without the help of custom functions, you can't achieve this.

答案2

得分: -1

在那个alertmanager的邮件模板中,我注意到了这一行:

  {{ range .Annotations.SortedPairs }}{{ .Name }} = {{ .Value }}<br />{{ end }}

所以你可以尝试:

{{ range .Alerts.Firing.Sorted }}
英文:

In that alertmanager email template I noticed this line :

  {{ range .Annotations.SortedPairs }}{{ .Name }} = {{ .Value }}<br />{{ end }}

So perhaps you could try:

{{ range .Alerts.Firing.Sorted }}

huangapple
  • 本文由 发表于 2017年1月6日 20:24:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/41505689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定