From 17ea8d006a9c2b0f5ba972f0c5a2cdb9db4e4fef Mon Sep 17 00:00:00 2001 From: Levi Harrison Date: Sun, 30 May 2021 23:35:26 -0400 Subject: [PATCH] Added external URL access Signed-off-by: Levi Harrison --- cmd/prometheus/main.go | 4 ++++ cmd/promtool/rules.go | 2 +- cmd/promtool/unittest.go | 3 ++- pkg/rulefmt/rulefmt.go | 3 ++- rules/alerting.go | 8 ++++++-- rules/manager.go | 7 ++++--- template/template.go | 4 +++- 7 files changed, 22 insertions(+), 9 deletions(-) diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index 9fbd6e76b..f5b8cd3c5 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -527,6 +527,9 @@ func main() { conntrack.DialWithTracing(), ) + // This is passed to ruleManager.Update(). + var externalURL = cfg.web.ExternalURL.String() + reloaders := []reloader{ { name: "remote_storage", @@ -592,6 +595,7 @@ func main() { time.Duration(cfg.GlobalConfig.EvaluationInterval), files, cfg.GlobalConfig.ExternalLabels, + externalURL, ) }, }, diff --git a/cmd/promtool/rules.go b/cmd/promtool/rules.go index 5f2e9a33f..a97e45b68 100644 --- a/cmd/promtool/rules.go +++ b/cmd/promtool/rules.go @@ -68,7 +68,7 @@ func newRuleImporter(logger log.Logger, config ruleImporterConfig, apiClient que // loadGroups parses groups from a list of recording rule files. func (importer *ruleImporter) loadGroups(ctx context.Context, filenames []string) (errs []error) { - groups, errs := importer.ruleManager.LoadGroups(importer.config.evalInterval, labels.Labels{}, filenames...) + groups, errs := importer.ruleManager.LoadGroups(importer.config.evalInterval, labels.Labels{}, "", filenames...) if errs != nil { return errs } diff --git a/cmd/promtool/unittest.go b/cmd/promtool/unittest.go index c55db94da..624dd5ba5 100644 --- a/cmd/promtool/unittest.go +++ b/cmd/promtool/unittest.go @@ -146,6 +146,7 @@ type testGroup struct { AlertRuleTests []alertTestCase `yaml:"alert_rule_test,omitempty"` PromqlExprTests []promqlTestCase `yaml:"promql_expr_test,omitempty"` ExternalLabels labels.Labels `yaml:"external_labels,omitempty"` + ExternalURL string `yaml:"external_url,omitempty"` TestGroupName string `yaml:"name,omitempty"` } @@ -168,7 +169,7 @@ func (tg *testGroup) test(evalInterval time.Duration, groupOrderMap map[string]i Logger: log.NewNopLogger(), } m := rules.NewManager(opts) - groupsMap, ers := m.LoadGroups(time.Duration(tg.Interval), tg.ExternalLabels, ruleFiles...) + groupsMap, ers := m.LoadGroups(time.Duration(tg.Interval), tg.ExternalLabels, tg.ExternalURL, ruleFiles...) if ers != nil { return ers } diff --git a/pkg/rulefmt/rulefmt.go b/pkg/rulefmt/rulefmt.go index 8267bc90e..920c7a203 100644 --- a/pkg/rulefmt/rulefmt.go +++ b/pkg/rulefmt/rulefmt.go @@ -223,10 +223,11 @@ func testTemplateParsing(rl *RuleNode) (errs []error) { } // Trying to parse templates. - tmplData := template.AlertTemplateData(map[string]string{}, map[string]string{}, 0) + tmplData := template.AlertTemplateData(map[string]string{}, map[string]string{}, "", 0) defs := []string{ "{{$labels := .Labels}}", "{{$externalLabels := .ExternalLabels}}", + "{{$externalURL := .ExternalURL}}", "{{$value := .Value}}", } parseTest := func(text string) error { diff --git a/rules/alerting.go b/rules/alerting.go index a4074221f..53da7586b 100644 --- a/rules/alerting.go +++ b/rules/alerting.go @@ -121,6 +121,8 @@ type AlertingRule struct { annotations labels.Labels // External labels from the global config. externalLabels map[string]string + // The external URL from the --web.external-url flag. + externalURL string // true if old state has been restored. We start persisting samples for ALERT_FOR_STATE // only after the restoration. restored bool @@ -144,7 +146,7 @@ type AlertingRule struct { // NewAlertingRule constructs a new AlertingRule. func NewAlertingRule( name string, vec parser.Expr, hold time.Duration, - labels, annotations, externalLabels labels.Labels, + labels, annotations, externalLabels labels.Labels, externalURL string, restored bool, logger log.Logger, ) *AlertingRule { el := make(map[string]string, len(externalLabels)) @@ -159,6 +161,7 @@ func NewAlertingRule( labels: labels, annotations: annotations, externalLabels: el, + externalURL: externalURL, health: HealthUnknown, active: map[uint64]*Alert{}, logger: logger, @@ -318,12 +321,13 @@ func (r *AlertingRule) Eval(ctx context.Context, ts time.Time, query QueryFunc, l[lbl.Name] = lbl.Value } - tmplData := template.AlertTemplateData(l, r.externalLabels, smpl.V) + tmplData := template.AlertTemplateData(l, r.externalLabels, r.externalURL, smpl.V) // Inject some convenience variables that are easier to remember for users // who are not used to Go's templating system. defs := []string{ "{{$labels := .Labels}}", "{{$externalLabels := .ExternalLabels}}", + "{{$externalURL := .ExternalURL}}", "{{$value := .Value}}", } diff --git a/rules/manager.go b/rules/manager.go index 0b38ec187..0cbcaa74f 100644 --- a/rules/manager.go +++ b/rules/manager.go @@ -946,11 +946,11 @@ func (m *Manager) Stop() { // Update the rule manager's state as the config requires. If // loading the new rules failed the old rule set is restored. -func (m *Manager) Update(interval time.Duration, files []string, externalLabels labels.Labels) error { +func (m *Manager) Update(interval time.Duration, files []string, externalLabels labels.Labels, externalURL string) error { m.mtx.Lock() defer m.mtx.Unlock() - groups, errs := m.LoadGroups(interval, externalLabels, files...) + groups, errs := m.LoadGroups(interval, externalLabels, externalURL, files...) if errs != nil { for _, e := range errs { level.Error(m.logger).Log("msg", "loading groups failed", "err", e) @@ -1034,7 +1034,7 @@ func (FileLoader) Parse(query string) (parser.Expr, error) { return parser.Parse // LoadGroups reads groups from a list of files. func (m *Manager) LoadGroups( - interval time.Duration, externalLabels labels.Labels, filenames ...string, + interval time.Duration, externalLabels labels.Labels, externalURL string, filenames ...string, ) (map[string]*Group, []error) { groups := make(map[string]*Group) @@ -1067,6 +1067,7 @@ func (m *Manager) LoadGroups( labels.FromMap(r.Labels), labels.FromMap(r.Annotations), externalLabels, + externalURL, m.restored, log.With(m.logger, "alert", r.Alert), )) diff --git a/template/template.go b/template/template.go index ae427738e..b0d8f5cc0 100644 --- a/template/template.go +++ b/template/template.go @@ -295,14 +295,16 @@ func NewTemplateExpander( } // AlertTemplateData returns the interface to be used in expanding the template. -func AlertTemplateData(labels map[string]string, externalLabels map[string]string, value float64) interface{} { +func AlertTemplateData(labels map[string]string, externalLabels map[string]string, externalURL string, value float64) interface{} { return struct { Labels map[string]string ExternalLabels map[string]string + ExternalURL string Value float64 }{ Labels: labels, ExternalLabels: externalLabels, + ExternalURL: externalURL, Value: value, } }