Added external URL access

Signed-off-by: Levi Harrison <git@leviharrison.dev>
This commit is contained in:
Levi Harrison 2021-05-30 23:35:26 -04:00
parent 2826fbeeb7
commit 17ea8d006a
No known key found for this signature in database
GPG key ID: 23A7BDEAE860B57D
7 changed files with 22 additions and 9 deletions

View file

@ -527,6 +527,9 @@ func main() {
conntrack.DialWithTracing(), conntrack.DialWithTracing(),
) )
// This is passed to ruleManager.Update().
var externalURL = cfg.web.ExternalURL.String()
reloaders := []reloader{ reloaders := []reloader{
{ {
name: "remote_storage", name: "remote_storage",
@ -592,6 +595,7 @@ func main() {
time.Duration(cfg.GlobalConfig.EvaluationInterval), time.Duration(cfg.GlobalConfig.EvaluationInterval),
files, files,
cfg.GlobalConfig.ExternalLabels, cfg.GlobalConfig.ExternalLabels,
externalURL,
) )
}, },
}, },

View file

@ -68,7 +68,7 @@ func newRuleImporter(logger log.Logger, config ruleImporterConfig, apiClient que
// loadGroups parses groups from a list of recording rule files. // loadGroups parses groups from a list of recording rule files.
func (importer *ruleImporter) loadGroups(ctx context.Context, filenames []string) (errs []error) { 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 { if errs != nil {
return errs return errs
} }

View file

@ -146,6 +146,7 @@ type testGroup struct {
AlertRuleTests []alertTestCase `yaml:"alert_rule_test,omitempty"` AlertRuleTests []alertTestCase `yaml:"alert_rule_test,omitempty"`
PromqlExprTests []promqlTestCase `yaml:"promql_expr_test,omitempty"` PromqlExprTests []promqlTestCase `yaml:"promql_expr_test,omitempty"`
ExternalLabels labels.Labels `yaml:"external_labels,omitempty"` ExternalLabels labels.Labels `yaml:"external_labels,omitempty"`
ExternalURL string `yaml:"external_url,omitempty"`
TestGroupName string `yaml:"name,omitempty"` TestGroupName string `yaml:"name,omitempty"`
} }
@ -168,7 +169,7 @@ func (tg *testGroup) test(evalInterval time.Duration, groupOrderMap map[string]i
Logger: log.NewNopLogger(), Logger: log.NewNopLogger(),
} }
m := rules.NewManager(opts) 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 { if ers != nil {
return ers return ers
} }

View file

@ -223,10 +223,11 @@ func testTemplateParsing(rl *RuleNode) (errs []error) {
} }
// Trying to parse templates. // 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{ defs := []string{
"{{$labels := .Labels}}", "{{$labels := .Labels}}",
"{{$externalLabels := .ExternalLabels}}", "{{$externalLabels := .ExternalLabels}}",
"{{$externalURL := .ExternalURL}}",
"{{$value := .Value}}", "{{$value := .Value}}",
} }
parseTest := func(text string) error { parseTest := func(text string) error {

View file

@ -121,6 +121,8 @@ type AlertingRule struct {
annotations labels.Labels annotations labels.Labels
// External labels from the global config. // External labels from the global config.
externalLabels map[string]string 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 // true if old state has been restored. We start persisting samples for ALERT_FOR_STATE
// only after the restoration. // only after the restoration.
restored bool restored bool
@ -144,7 +146,7 @@ type AlertingRule struct {
// NewAlertingRule constructs a new AlertingRule. // NewAlertingRule constructs a new AlertingRule.
func NewAlertingRule( func NewAlertingRule(
name string, vec parser.Expr, hold time.Duration, 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, restored bool, logger log.Logger,
) *AlertingRule { ) *AlertingRule {
el := make(map[string]string, len(externalLabels)) el := make(map[string]string, len(externalLabels))
@ -159,6 +161,7 @@ func NewAlertingRule(
labels: labels, labels: labels,
annotations: annotations, annotations: annotations,
externalLabels: el, externalLabels: el,
externalURL: externalURL,
health: HealthUnknown, health: HealthUnknown,
active: map[uint64]*Alert{}, active: map[uint64]*Alert{},
logger: logger, logger: logger,
@ -318,12 +321,13 @@ func (r *AlertingRule) Eval(ctx context.Context, ts time.Time, query QueryFunc,
l[lbl.Name] = lbl.Value 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 // Inject some convenience variables that are easier to remember for users
// who are not used to Go's templating system. // who are not used to Go's templating system.
defs := []string{ defs := []string{
"{{$labels := .Labels}}", "{{$labels := .Labels}}",
"{{$externalLabels := .ExternalLabels}}", "{{$externalLabels := .ExternalLabels}}",
"{{$externalURL := .ExternalURL}}",
"{{$value := .Value}}", "{{$value := .Value}}",
} }

View file

@ -946,11 +946,11 @@ func (m *Manager) Stop() {
// Update the rule manager's state as the config requires. If // Update the rule manager's state as the config requires. If
// loading the new rules failed the old rule set is restored. // 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() m.mtx.Lock()
defer m.mtx.Unlock() defer m.mtx.Unlock()
groups, errs := m.LoadGroups(interval, externalLabels, files...) groups, errs := m.LoadGroups(interval, externalLabels, externalURL, files...)
if errs != nil { if errs != nil {
for _, e := range errs { for _, e := range errs {
level.Error(m.logger).Log("msg", "loading groups failed", "err", e) 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. // LoadGroups reads groups from a list of files.
func (m *Manager) LoadGroups( 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) { ) (map[string]*Group, []error) {
groups := make(map[string]*Group) groups := make(map[string]*Group)
@ -1067,6 +1067,7 @@ func (m *Manager) LoadGroups(
labels.FromMap(r.Labels), labels.FromMap(r.Labels),
labels.FromMap(r.Annotations), labels.FromMap(r.Annotations),
externalLabels, externalLabels,
externalURL,
m.restored, m.restored,
log.With(m.logger, "alert", r.Alert), log.With(m.logger, "alert", r.Alert),
)) ))

View file

@ -295,14 +295,16 @@ func NewTemplateExpander(
} }
// AlertTemplateData returns the interface to be used in expanding the template. // 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 { return struct {
Labels map[string]string Labels map[string]string
ExternalLabels map[string]string ExternalLabels map[string]string
ExternalURL string
Value float64 Value float64
}{ }{
Labels: labels, Labels: labels,
ExternalLabels: externalLabels, ExternalLabels: externalLabels,
ExternalURL: externalURL,
Value: value, Value: value,
} }
} }