configuration for metrics.yaml and templates

This commit is contained in:
Ulric Qin 2021-12-23 12:53:32 +08:00
parent f661a6bd37
commit 3428b11ea8
8 changed files with 22 additions and 6 deletions

View File

@ -54,6 +54,7 @@ Interval = 1000
[Alerting]
NotifyScriptPath = "./etc/script/notify.py"
NotifyConcurrency = 100
TemplatesDir = "./etc/template"
[Alerting.RedisPub]
Enable = false

View File

@ -7,6 +7,9 @@ RunMode = "release"
# do not change
AdminRole = "Admin"
# metrics descriptions
MetricsYamlFile = "./etc/metrics.yaml"
# Linkage with notify.py script
NotifyChannels = [ "email", "dingtalk", "wecom", "feishu" ]

View File

@ -57,6 +57,7 @@ Interval = 1000
[Alerting]
NotifyScriptPath = "./etc/script/notify.py"
NotifyConcurrency = 100
TemplatesDir = "./etc/template"
[Alerting.RedisPub]
Enable = false

View File

@ -7,6 +7,9 @@ RunMode = "release"
# do not change
AdminRole = "Admin"
# metrics descriptions
MetricsYamlFile = "./etc/metrics.yaml"
# Linkage with notify.py script
NotifyChannels = [ "email", "dingtalk", "wecom", "feishu" ]

View File

@ -113,6 +113,7 @@ type HeartbeatConfig struct {
type Alerting struct {
NotifyScriptPath string
NotifyConcurrency int
TemplatesDir string
RedisPub RedisPub
}

View File

@ -44,15 +44,17 @@ var fns = template.FuncMap{
}
func initTpls() error {
tplDir := path.Join(runner.Cwd, "etc", "template")
if config.C.Alerting.TemplatesDir == "" {
config.C.Alerting.TemplatesDir = path.Join(runner.Cwd, "etc", "template")
}
filenames, err := file.FilesUnder(tplDir)
filenames, err := file.FilesUnder(config.C.Alerting.TemplatesDir)
if err != nil {
return errors.WithMessage(err, "failed to exec FilesUnder")
}
if len(filenames) == 0 {
return errors.New("no tpl files under " + tplDir)
return errors.New("no tpl files under " + config.C.Alerting.TemplatesDir)
}
tplFiles := make([]string, 0, len(filenames))
@ -63,11 +65,11 @@ func initTpls() error {
}
if len(tplFiles) == 0 {
return errors.New("no tpl files under " + tplDir)
return errors.New("no tpl files under " + config.C.Alerting.TemplatesDir)
}
for i := 0; i < len(tplFiles); i++ {
tplpath := path.Join(tplDir, tplFiles[i])
tplpath := path.Join(config.C.Alerting.TemplatesDir, tplFiles[i])
tpl, err := template.New(tplFiles[i]).Funcs(fns).ParseFiles(tplpath)
if err != nil {

View File

@ -77,6 +77,7 @@ type Config struct {
RunMode string
I18N string
AdminRole string
MetricsYamlFile string
ContactKeys []ContactKey
NotifyChannels []string
Log logx.Config

View File

@ -12,7 +12,11 @@ import (
)
func metricsDescGetFile(c *gin.Context) {
fp := path.Join(runner.Cwd, "etc", "metrics.yaml")
fp := config.C.MetricsYamlFile
if fp == "" {
fp = path.Join(runner.Cwd, "etc", "metrics.yaml")
}
if !file.IsExist(fp) {
c.String(404, "%s not found", fp)
return