Skip to content

Commit

Permalink
修改报警消息模版:
Browse files Browse the repository at this point in the history
1.模版中使用ftm.sprintf
  对float类型进行转换,避免消息中数值以十进制的指数次格式显示,提高可读性
2.增加text/tempalte,解决飞书等消息中消息内容被编码的问题
  • Loading branch information
zhaoxiangchun committed Jul 24, 2020
1 parent bb66e2d commit 6193221
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
10 changes: 5 additions & 5 deletions pkg/monitor/alerting/notifiers/templates/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ const DefaultMarkdownTemplate = `
{{range .Matches}}
- 指标: {{.Metric}}
- 当前值: {{.Value}}
- 当前值: {{.Value | FormateFloat}}
### 触发条件:
> {{.Condition}}
- {{.Condition}}
### 标签
Expand All @@ -61,7 +61,7 @@ const EmailMarkdownTemplate = `
<p>- 级别: {{.Level}}</p>
<p>{{range .Matches}}</p>
<p>- 指标: {{.Metric}}</p>
<p>- 当前值: {{.Value}}</p>
<p>- 当前值: {{.Value | FormateFloat}}</p>
</br><p>## 触发条件:</p>
<p> {{.Condition}}</p>
</br><p>## 标签</p>
Expand All @@ -76,9 +76,9 @@ const EmailMarkdownTemplate = `
`

func (c TemplateConfig) GenerateMarkdown() (string, error) {
return CompileTEmplateFromMap(DefaultMarkdownTemplate, c)
return CompileTEmplateFromMapText(DefaultMarkdownTemplate, c)
}

func (c TemplateConfig) GenerateEmailMarkdown() (string, error) {
return CompileTEmplateFromMap(EmailMarkdownTemplate, c)
return CompileTemplateFromMapHtml(EmailMarkdownTemplate, c)
}
25 changes: 23 additions & 2 deletions pkg/monitor/alerting/notifiers/templates/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,35 @@ package templates

import (
"bytes"
"fmt"
"html/template"
t_template "text/template"
)

func CompileTEmplateFromMap(tmplt string, configMap interface{}) (string, error) {
func CompileTemplateFromMapHtml(tmplt string, configMap interface{}) (string, error) {
out := new(bytes.Buffer)
t := template.Must(template.New("commpiled_template").Parse(tmplt))
t := template.Must(template.New("commpiled_template").Funcs(
template.FuncMap{
"FormateFloat": FormateFloat,
}).Parse(tmplt))
if err := t.Execute(out, configMap); err != nil {
return "", err
}
return out.String(), nil
}

func CompileTEmplateFromMapText(tmplt string, configMap interface{}) (string, error) {
out := new(bytes.Buffer)
t := t_template.Must(t_template.New("commpiled_template").Funcs(
t_template.FuncMap{
"FormateFloat": FormateFloat,
}).Parse(tmplt))
if err := t.Execute(out, configMap); err != nil {
return "", err
}
return out.String(), nil
}

func FormateFloat(f *float64) string {
return fmt.Sprintf("%f", *f)
}

0 comments on commit 6193221

Please sign in to comment.