mirror of
https://github.com/prometheus/alertmanager
synced 2025-02-16 10:37:09 +00:00
Make loadable templates configurable
This commit is contained in:
parent
e209c8b4fc
commit
7ef293d9bc
@ -16,6 +16,7 @@ package config
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
@ -40,7 +41,28 @@ func LoadFile(filename string) (*Config, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return Load(string(content))
|
||||
cfg, err := Load(string(content))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resolveFilepaths(filepath.Dir(filename), cfg)
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// resolveFilepaths joins all relative paths in a configuration
|
||||
// with a given base directory.
|
||||
func resolveFilepaths(baseDir string, cfg *Config) {
|
||||
join := func(fp string) string {
|
||||
if len(fp) > 0 && !filepath.IsAbs(fp) {
|
||||
fp = filepath.Join(baseDir, fp)
|
||||
}
|
||||
return fp
|
||||
}
|
||||
|
||||
for i, tf := range cfg.Templates {
|
||||
cfg.Templates[i] = join(tf)
|
||||
}
|
||||
}
|
||||
|
||||
// Config is the top-level configuration for Alertmanager's config files.
|
||||
@ -48,6 +70,7 @@ type Config struct {
|
||||
Routes []*Route `yaml:"routes,omitempty"`
|
||||
InhibitRules []*InhibitRule `yaml:"inhibit_rules,omitempty"`
|
||||
NotificationConfigs []*NotificationConfig `yaml:"notification_configs,omitempty"`
|
||||
Templates []string `yaml:"templates"`
|
||||
|
||||
// Catches all undefined fields and must be empty after parsing.
|
||||
XXX map[string]interface{} `yaml:",inline"`
|
||||
|
11
main.go
11
main.go
@ -22,6 +22,7 @@ import (
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"text/template"
|
||||
|
||||
"github.com/prometheus/common/log"
|
||||
"github.com/prometheus/common/route"
|
||||
@ -153,6 +154,16 @@ func reloadConfig(filename string, rls ...types.Reloadable) error {
|
||||
return err
|
||||
}
|
||||
|
||||
t := template.New("")
|
||||
for _, tpath := range conf.Templates {
|
||||
t, err = t.ParseGlob(tpath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
notify.SetTemplate(t)
|
||||
|
||||
for _, rl := range rls {
|
||||
rl.ApplyConfig(conf)
|
||||
}
|
||||
|
@ -172,6 +172,6 @@ func (n *Slack) Notify(ctx context.Context, as ...*types.Alert) error {
|
||||
|
||||
var tmpl *template.Template
|
||||
|
||||
func init() {
|
||||
tmpl = template.Must(template.ParseGlob("templates/*.tmpl"))
|
||||
func SetTemplate(t *template.Template) {
|
||||
tmpl = t
|
||||
}
|
||||
|
@ -16,6 +16,12 @@ type Reloadable interface {
|
||||
ApplyConfig(*config.Config)
|
||||
}
|
||||
|
||||
type ReloadFunc func(*config.Config)
|
||||
|
||||
func (f ReloadFunc) ApplyConfig(cfg *config.Config) {
|
||||
f(cfg)
|
||||
}
|
||||
|
||||
// Alert wraps a model.Alert with additional information relevant
|
||||
// to internal of the Alertmanager.
|
||||
// The type is never exposed to external communication and the
|
||||
|
Loading…
Reference in New Issue
Block a user