2020-05-10 16:58:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"hash/fnv"
|
2020-06-01 10:46:37 +00:00
|
|
|
"sync"
|
2020-05-10 16:58:17 +00:00
|
|
|
"time"
|
|
|
|
|
2020-06-01 10:46:37 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/config"
|
2020-05-10 16:58:17 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/datasource"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/notifier"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/remotewrite"
|
2020-06-29 19:21:03 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/utils"
|
2020-05-10 16:58:17 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Group is an entity for grouping rules
|
|
|
|
type Group struct {
|
2021-05-22 21:26:01 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
Name string
|
|
|
|
File string
|
|
|
|
Rules []Rule
|
|
|
|
Type datasource.Type
|
|
|
|
Interval time.Duration
|
|
|
|
Concurrency int
|
|
|
|
Checksum string
|
|
|
|
ExtraFilterLabels map[string]string
|
2020-05-10 16:58:17 +00:00
|
|
|
|
2020-05-17 14:12:09 +00:00
|
|
|
doneCh chan struct{}
|
|
|
|
finishedCh chan struct{}
|
|
|
|
// channel accepts new Group obj
|
|
|
|
// which supposed to update current group
|
2020-06-01 10:46:37 +00:00
|
|
|
updateCh chan *Group
|
app/vmalert: extend metrics set exported by `vmalert` #573 (#654)
* app/vmalert: extend metrics set exported by `vmalert` #573
New metrics were added to improve observability:
+ vmalert_alerts_pending{alertname, group} - number of pending alerts per group
per alert;
+ vmalert_alerts_acitve{alertname, group} - number of active alerts per group
per alert;
+ vmalert_alerts_error{alertname, group} - is 1 if alertname ended up with error
during prev execution, is 0 if no errors happened;
+ vmalert_recording_rules_error{recording, group} - is 1 if recording rule
ended up with error during prev execution, is 0 if no errors happened;
* vmalert_iteration_total{group, file} - now contains group and file name labels.
This should improve control over specific groups;
* vmalert_iteration_duration_seconds{group, file} - now contains group and file name labels. This should improve control over specific groups;
Some collisions for alerts and recording rules are possible, because neither
group name nor alert/recording rule name are unique for compatibility reasons.
Commit contains list of TODOs for Unregistering metrics since groups and rules
are ephemeral and could be removed without application restart. In order to
unlock Unregistering feature corresponding PR was filed - https://github.com/VictoriaMetrics/metrics/pull/13
* app/vmalert: extend metrics set exported by `vmalert` #573
The changes are following:
* add an ID label to rules metrics, since `name` collisions within one group is
a common case - see the k8s example alerts;
* supports metrics unregistering on rule updates. Consider the case when one rule
was added or removed from the group, or the whole group was added or removed.
The change depends on https://github.com/VictoriaMetrics/metrics/pull/16
where race condition for Unregister method was fixed.
2020-08-09 06:41:29 +00:00
|
|
|
|
|
|
|
metrics *groupMetrics
|
|
|
|
}
|
|
|
|
|
|
|
|
type groupMetrics struct {
|
|
|
|
iterationTotal *counter
|
|
|
|
iterationDuration *summary
|
|
|
|
}
|
|
|
|
|
|
|
|
func newGroupMetrics(name, file string) *groupMetrics {
|
|
|
|
m := &groupMetrics{}
|
|
|
|
labels := fmt.Sprintf(`group=%q, file=%q`, name, file)
|
|
|
|
m.iterationTotal = getOrCreateCounter(fmt.Sprintf(`vmalert_iteration_total{%s}`, labels))
|
|
|
|
m.iterationDuration = getOrCreateSummary(fmt.Sprintf(`vmalert_iteration_duration_seconds{%s}`, labels))
|
|
|
|
return m
|
2020-06-01 10:46:37 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 20:41:15 +00:00
|
|
|
func newGroup(cfg config.Group, qb datasource.QuerierBuilder, defaultInterval time.Duration, labels map[string]string) *Group {
|
2020-06-01 10:46:37 +00:00
|
|
|
g := &Group{
|
2021-05-22 21:26:01 +00:00
|
|
|
Type: cfg.Type,
|
|
|
|
Name: cfg.Name,
|
|
|
|
File: cfg.File,
|
|
|
|
Interval: cfg.Interval,
|
|
|
|
Concurrency: cfg.Concurrency,
|
|
|
|
Checksum: cfg.Checksum,
|
|
|
|
ExtraFilterLabels: cfg.ExtraFilterLabels,
|
|
|
|
|
|
|
|
doneCh: make(chan struct{}),
|
|
|
|
finishedCh: make(chan struct{}),
|
|
|
|
updateCh: make(chan *Group),
|
2020-06-01 10:46:37 +00:00
|
|
|
}
|
app/vmalert: extend metrics set exported by `vmalert` #573 (#654)
* app/vmalert: extend metrics set exported by `vmalert` #573
New metrics were added to improve observability:
+ vmalert_alerts_pending{alertname, group} - number of pending alerts per group
per alert;
+ vmalert_alerts_acitve{alertname, group} - number of active alerts per group
per alert;
+ vmalert_alerts_error{alertname, group} - is 1 if alertname ended up with error
during prev execution, is 0 if no errors happened;
+ vmalert_recording_rules_error{recording, group} - is 1 if recording rule
ended up with error during prev execution, is 0 if no errors happened;
* vmalert_iteration_total{group, file} - now contains group and file name labels.
This should improve control over specific groups;
* vmalert_iteration_duration_seconds{group, file} - now contains group and file name labels. This should improve control over specific groups;
Some collisions for alerts and recording rules are possible, because neither
group name nor alert/recording rule name are unique for compatibility reasons.
Commit contains list of TODOs for Unregistering metrics since groups and rules
are ephemeral and could be removed without application restart. In order to
unlock Unregistering feature corresponding PR was filed - https://github.com/VictoriaMetrics/metrics/pull/13
* app/vmalert: extend metrics set exported by `vmalert` #573
The changes are following:
* add an ID label to rules metrics, since `name` collisions within one group is
a common case - see the k8s example alerts;
* supports metrics unregistering on rule updates. Consider the case when one rule
was added or removed from the group, or the whole group was added or removed.
The change depends on https://github.com/VictoriaMetrics/metrics/pull/16
where race condition for Unregister method was fixed.
2020-08-09 06:41:29 +00:00
|
|
|
g.metrics = newGroupMetrics(g.Name, g.File)
|
2020-06-01 10:46:37 +00:00
|
|
|
if g.Interval == 0 {
|
|
|
|
g.Interval = defaultInterval
|
|
|
|
}
|
2020-06-09 12:21:20 +00:00
|
|
|
if g.Concurrency < 1 {
|
|
|
|
g.Concurrency = 1
|
|
|
|
}
|
2020-06-01 10:46:37 +00:00
|
|
|
rules := make([]Rule, len(cfg.Rules))
|
|
|
|
for i, r := range cfg.Rules {
|
2020-07-28 11:20:31 +00:00
|
|
|
// override rule labels with external labels
|
|
|
|
for k, v := range labels {
|
|
|
|
if prevV, ok := r.Labels[k]; ok {
|
|
|
|
logger.Infof("label %q=%q for rule %q.%q overwritten with external label %q=%q",
|
|
|
|
k, prevV, g.Name, r.Name(), k, v)
|
|
|
|
}
|
|
|
|
if r.Labels == nil {
|
|
|
|
r.Labels = map[string]string{}
|
|
|
|
}
|
|
|
|
r.Labels[k] = v
|
|
|
|
}
|
2021-04-28 20:41:15 +00:00
|
|
|
rules[i] = g.newRule(qb, r)
|
2020-06-01 10:46:37 +00:00
|
|
|
}
|
|
|
|
g.Rules = rules
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:41:15 +00:00
|
|
|
func (g *Group) newRule(qb datasource.QuerierBuilder, rule config.Rule) Rule {
|
2020-06-01 10:46:37 +00:00
|
|
|
if rule.Alert != "" {
|
2021-04-28 20:41:15 +00:00
|
|
|
return newAlertingRule(qb, g, rule)
|
2020-06-01 10:46:37 +00:00
|
|
|
}
|
2021-04-28 20:41:15 +00:00
|
|
|
return newRecordingRule(qb, g, rule)
|
2020-05-10 16:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID return unique group ID that consists of
|
|
|
|
// rules file and group name
|
2020-05-17 14:12:09 +00:00
|
|
|
func (g *Group) ID() uint64 {
|
2020-05-10 16:58:17 +00:00
|
|
|
hash := fnv.New64a()
|
|
|
|
hash.Write([]byte(g.File))
|
|
|
|
hash.Write([]byte("\xff"))
|
|
|
|
hash.Write([]byte(g.Name))
|
2021-02-01 13:02:44 +00:00
|
|
|
hash.Write([]byte(g.Type.Get()))
|
2020-05-10 16:58:17 +00:00
|
|
|
return hash.Sum64()
|
|
|
|
}
|
|
|
|
|
2020-06-01 10:46:37 +00:00
|
|
|
// Restore restores alerts state for group rules
|
2021-04-28 20:41:15 +00:00
|
|
|
func (g *Group) Restore(ctx context.Context, qb datasource.QuerierBuilder, lookback time.Duration, labels map[string]string) error {
|
2020-05-10 16:58:17 +00:00
|
|
|
for _, rule := range g.Rules {
|
2020-06-01 10:46:37 +00:00
|
|
|
rr, ok := rule.(*AlertingRule)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if rr.For < 1 {
|
|
|
|
continue
|
2020-05-10 16:58:17 +00:00
|
|
|
}
|
2021-05-22 21:26:01 +00:00
|
|
|
// ignore g.ExtraFilterLabels on purpose, so it
|
|
|
|
// won't affect the restore procedure.
|
2021-04-28 20:41:15 +00:00
|
|
|
q := qb.BuildWithParams(datasource.QuerierParams{})
|
2020-07-28 11:20:31 +00:00
|
|
|
if err := rr.Restore(ctx, q, lookback, labels); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("error while restoring rule %q: %w", rule, err)
|
2020-05-10 16:58:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateWith updates existing group with
|
2020-06-01 10:46:37 +00:00
|
|
|
// passed group object. This function ignores group
|
|
|
|
// evaluation interval change. It supposed to be updated
|
|
|
|
// in group.start function.
|
2020-05-17 14:12:09 +00:00
|
|
|
// Not thread-safe.
|
2020-06-01 10:46:37 +00:00
|
|
|
func (g *Group) updateWith(newGroup *Group) error {
|
|
|
|
rulesRegistry := make(map[uint64]Rule)
|
2020-05-10 16:58:17 +00:00
|
|
|
for _, nr := range newGroup.Rules {
|
2020-06-01 10:46:37 +00:00
|
|
|
rulesRegistry[nr.ID()] = nr
|
2020-05-10 16:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, or := range g.Rules {
|
2020-06-01 10:46:37 +00:00
|
|
|
nr, ok := rulesRegistry[or.ID()]
|
2020-05-10 16:58:17 +00:00
|
|
|
if !ok {
|
|
|
|
// old rule is not present in the new list
|
2020-05-15 06:55:22 +00:00
|
|
|
// so we mark it for removing
|
app/vmalert: extend metrics set exported by `vmalert` #573 (#654)
* app/vmalert: extend metrics set exported by `vmalert` #573
New metrics were added to improve observability:
+ vmalert_alerts_pending{alertname, group} - number of pending alerts per group
per alert;
+ vmalert_alerts_acitve{alertname, group} - number of active alerts per group
per alert;
+ vmalert_alerts_error{alertname, group} - is 1 if alertname ended up with error
during prev execution, is 0 if no errors happened;
+ vmalert_recording_rules_error{recording, group} - is 1 if recording rule
ended up with error during prev execution, is 0 if no errors happened;
* vmalert_iteration_total{group, file} - now contains group and file name labels.
This should improve control over specific groups;
* vmalert_iteration_duration_seconds{group, file} - now contains group and file name labels. This should improve control over specific groups;
Some collisions for alerts and recording rules are possible, because neither
group name nor alert/recording rule name are unique for compatibility reasons.
Commit contains list of TODOs for Unregistering metrics since groups and rules
are ephemeral and could be removed without application restart. In order to
unlock Unregistering feature corresponding PR was filed - https://github.com/VictoriaMetrics/metrics/pull/13
* app/vmalert: extend metrics set exported by `vmalert` #573
The changes are following:
* add an ID label to rules metrics, since `name` collisions within one group is
a common case - see the k8s example alerts;
* supports metrics unregistering on rule updates. Consider the case when one rule
was added or removed from the group, or the whole group was added or removed.
The change depends on https://github.com/VictoriaMetrics/metrics/pull/16
where race condition for Unregister method was fixed.
2020-08-09 06:41:29 +00:00
|
|
|
g.Rules[i].Close()
|
2020-05-15 06:55:22 +00:00
|
|
|
g.Rules[i] = nil
|
2020-05-10 16:58:17 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-06-01 10:46:37 +00:00
|
|
|
if err := or.UpdateWith(nr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(rulesRegistry, nr.ID())
|
2020-05-10 16:58:17 +00:00
|
|
|
}
|
|
|
|
|
2020-06-01 10:46:37 +00:00
|
|
|
var newRules []Rule
|
2020-05-15 06:55:22 +00:00
|
|
|
for _, r := range g.Rules {
|
|
|
|
if r == nil {
|
|
|
|
// skip nil rules
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newRules = append(newRules, r)
|
|
|
|
}
|
|
|
|
// add the rest of rules from registry
|
2020-05-10 16:58:17 +00:00
|
|
|
for _, nr := range rulesRegistry {
|
2020-05-15 06:55:22 +00:00
|
|
|
newRules = append(newRules, nr)
|
2020-05-10 16:58:17 +00:00
|
|
|
}
|
2021-02-01 13:02:44 +00:00
|
|
|
g.Type = newGroup.Type
|
2020-06-09 12:21:20 +00:00
|
|
|
g.Concurrency = newGroup.Concurrency
|
2021-05-22 21:26:01 +00:00
|
|
|
g.ExtraFilterLabels = newGroup.ExtraFilterLabels
|
2020-09-11 19:14:30 +00:00
|
|
|
g.Checksum = newGroup.Checksum
|
2020-05-15 06:55:22 +00:00
|
|
|
g.Rules = newRules
|
2020-06-01 10:46:37 +00:00
|
|
|
return nil
|
2020-05-10 16:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
alertsFired = metrics.NewCounter(`vmalert_alerts_fired_total`)
|
|
|
|
alertsSent = metrics.NewCounter(`vmalert_alerts_sent_total`)
|
|
|
|
alertsSendErrors = metrics.NewCounter(`vmalert_alerts_send_errors_total`)
|
|
|
|
)
|
|
|
|
|
|
|
|
func (g *Group) close() {
|
2020-05-17 14:12:09 +00:00
|
|
|
if g.doneCh == nil {
|
2020-05-10 16:58:17 +00:00
|
|
|
return
|
|
|
|
}
|
2020-05-17 14:12:09 +00:00
|
|
|
close(g.doneCh)
|
|
|
|
<-g.finishedCh
|
app/vmalert: extend metrics set exported by `vmalert` #573 (#654)
* app/vmalert: extend metrics set exported by `vmalert` #573
New metrics were added to improve observability:
+ vmalert_alerts_pending{alertname, group} - number of pending alerts per group
per alert;
+ vmalert_alerts_acitve{alertname, group} - number of active alerts per group
per alert;
+ vmalert_alerts_error{alertname, group} - is 1 if alertname ended up with error
during prev execution, is 0 if no errors happened;
+ vmalert_recording_rules_error{recording, group} - is 1 if recording rule
ended up with error during prev execution, is 0 if no errors happened;
* vmalert_iteration_total{group, file} - now contains group and file name labels.
This should improve control over specific groups;
* vmalert_iteration_duration_seconds{group, file} - now contains group and file name labels. This should improve control over specific groups;
Some collisions for alerts and recording rules are possible, because neither
group name nor alert/recording rule name are unique for compatibility reasons.
Commit contains list of TODOs for Unregistering metrics since groups and rules
are ephemeral and could be removed without application restart. In order to
unlock Unregistering feature corresponding PR was filed - https://github.com/VictoriaMetrics/metrics/pull/13
* app/vmalert: extend metrics set exported by `vmalert` #573
The changes are following:
* add an ID label to rules metrics, since `name` collisions within one group is
a common case - see the k8s example alerts;
* supports metrics unregistering on rule updates. Consider the case when one rule
was added or removed from the group, or the whole group was added or removed.
The change depends on https://github.com/VictoriaMetrics/metrics/pull/16
where race condition for Unregister method was fixed.
2020-08-09 06:41:29 +00:00
|
|
|
|
|
|
|
metrics.UnregisterMetric(g.metrics.iterationDuration.name)
|
|
|
|
metrics.UnregisterMetric(g.metrics.iterationTotal.name)
|
|
|
|
for _, rule := range g.Rules {
|
|
|
|
rule.Close()
|
|
|
|
}
|
2020-05-10 16:58:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 22:00:55 +00:00
|
|
|
var skipRandSleepOnGroupStart bool
|
|
|
|
|
2021-04-28 20:41:15 +00:00
|
|
|
func (g *Group) start(ctx context.Context, nts []notifier.Notifier, rw *remotewrite.Client) {
|
2020-06-09 12:21:20 +00:00
|
|
|
defer func() { close(g.finishedCh) }()
|
2020-09-02 22:00:55 +00:00
|
|
|
|
|
|
|
// Spread group rules evaluation over time in order to reduce load on VictoriaMetrics.
|
|
|
|
if !skipRandSleepOnGroupStart {
|
|
|
|
randSleep := uint64(float64(g.Interval) * (float64(uint32(g.ID())) / (1 << 32)))
|
|
|
|
sleepOffset := uint64(time.Now().UnixNano()) % uint64(g.Interval)
|
|
|
|
if randSleep < sleepOffset {
|
|
|
|
randSleep += uint64(g.Interval)
|
|
|
|
}
|
|
|
|
randSleep -= sleepOffset
|
|
|
|
sleepTimer := time.NewTimer(time.Duration(randSleep))
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
sleepTimer.Stop()
|
|
|
|
return
|
|
|
|
case <-g.doneCh:
|
|
|
|
sleepTimer.Stop()
|
|
|
|
return
|
|
|
|
case <-sleepTimer.C:
|
|
|
|
}
|
2020-09-02 21:58:54 +00:00
|
|
|
}
|
2020-09-02 22:00:55 +00:00
|
|
|
|
|
|
|
logger.Infof("group %q started; interval=%v; concurrency=%d", g.Name, g.Interval, g.Concurrency)
|
2021-04-28 20:41:15 +00:00
|
|
|
e := &executor{nts, rw}
|
2020-06-01 10:46:37 +00:00
|
|
|
t := time.NewTicker(g.Interval)
|
2020-05-10 16:58:17 +00:00
|
|
|
defer t.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
logger.Infof("group %q: context cancelled", g.Name)
|
|
|
|
return
|
2020-05-17 14:12:09 +00:00
|
|
|
case <-g.doneCh:
|
2020-05-10 16:58:17 +00:00
|
|
|
logger.Infof("group %q: received stop signal", g.Name)
|
|
|
|
return
|
2020-05-17 14:12:09 +00:00
|
|
|
case ng := <-g.updateCh:
|
2020-06-01 10:46:37 +00:00
|
|
|
g.mu.Lock()
|
|
|
|
err := g.updateWith(ng)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("group %q: failed to update: %s", g.Name, err)
|
|
|
|
g.mu.Unlock()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if g.Interval != ng.Interval {
|
|
|
|
g.Interval = ng.Interval
|
|
|
|
t.Stop()
|
|
|
|
t = time.NewTicker(g.Interval)
|
|
|
|
}
|
|
|
|
g.mu.Unlock()
|
2020-06-09 12:21:20 +00:00
|
|
|
logger.Infof("group %q re-started; interval=%v; concurrency=%d", g.Name, g.Interval, g.Concurrency)
|
2020-05-10 16:58:17 +00:00
|
|
|
case <-t.C:
|
app/vmalert: extend metrics set exported by `vmalert` #573 (#654)
* app/vmalert: extend metrics set exported by `vmalert` #573
New metrics were added to improve observability:
+ vmalert_alerts_pending{alertname, group} - number of pending alerts per group
per alert;
+ vmalert_alerts_acitve{alertname, group} - number of active alerts per group
per alert;
+ vmalert_alerts_error{alertname, group} - is 1 if alertname ended up with error
during prev execution, is 0 if no errors happened;
+ vmalert_recording_rules_error{recording, group} - is 1 if recording rule
ended up with error during prev execution, is 0 if no errors happened;
* vmalert_iteration_total{group, file} - now contains group and file name labels.
This should improve control over specific groups;
* vmalert_iteration_duration_seconds{group, file} - now contains group and file name labels. This should improve control over specific groups;
Some collisions for alerts and recording rules are possible, because neither
group name nor alert/recording rule name are unique for compatibility reasons.
Commit contains list of TODOs for Unregistering metrics since groups and rules
are ephemeral and could be removed without application restart. In order to
unlock Unregistering feature corresponding PR was filed - https://github.com/VictoriaMetrics/metrics/pull/13
* app/vmalert: extend metrics set exported by `vmalert` #573
The changes are following:
* add an ID label to rules metrics, since `name` collisions within one group is
a common case - see the k8s example alerts;
* supports metrics unregistering on rule updates. Consider the case when one rule
was added or removed from the group, or the whole group was added or removed.
The change depends on https://github.com/VictoriaMetrics/metrics/pull/16
where race condition for Unregister method was fixed.
2020-08-09 06:41:29 +00:00
|
|
|
g.metrics.iterationTotal.Inc()
|
2020-05-10 16:58:17 +00:00
|
|
|
iterationStart := time.Now()
|
|
|
|
|
2020-06-09 12:21:20 +00:00
|
|
|
errs := e.execConcurrently(ctx, g.Rules, g.Concurrency, g.Interval)
|
|
|
|
for err := range errs {
|
2020-05-10 16:58:17 +00:00
|
|
|
if err != nil {
|
2020-06-09 12:21:20 +00:00
|
|
|
logger.Errorf("group %q: %s", g.Name, err)
|
2020-05-10 16:58:17 +00:00
|
|
|
}
|
2020-06-09 12:21:20 +00:00
|
|
|
}
|
2020-05-10 16:58:17 +00:00
|
|
|
|
app/vmalert: extend metrics set exported by `vmalert` #573 (#654)
* app/vmalert: extend metrics set exported by `vmalert` #573
New metrics were added to improve observability:
+ vmalert_alerts_pending{alertname, group} - number of pending alerts per group
per alert;
+ vmalert_alerts_acitve{alertname, group} - number of active alerts per group
per alert;
+ vmalert_alerts_error{alertname, group} - is 1 if alertname ended up with error
during prev execution, is 0 if no errors happened;
+ vmalert_recording_rules_error{recording, group} - is 1 if recording rule
ended up with error during prev execution, is 0 if no errors happened;
* vmalert_iteration_total{group, file} - now contains group and file name labels.
This should improve control over specific groups;
* vmalert_iteration_duration_seconds{group, file} - now contains group and file name labels. This should improve control over specific groups;
Some collisions for alerts and recording rules are possible, because neither
group name nor alert/recording rule name are unique for compatibility reasons.
Commit contains list of TODOs for Unregistering metrics since groups and rules
are ephemeral and could be removed without application restart. In order to
unlock Unregistering feature corresponding PR was filed - https://github.com/VictoriaMetrics/metrics/pull/13
* app/vmalert: extend metrics set exported by `vmalert` #573
The changes are following:
* add an ID label to rules metrics, since `name` collisions within one group is
a common case - see the k8s example alerts;
* supports metrics unregistering on rule updates. Consider the case when one rule
was added or removed from the group, or the whole group was added or removed.
The change depends on https://github.com/VictoriaMetrics/metrics/pull/16
where race condition for Unregister method was fixed.
2020-08-09 06:41:29 +00:00
|
|
|
g.metrics.iterationDuration.UpdateDuration(iterationStart)
|
2020-06-09 12:21:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-01 10:46:37 +00:00
|
|
|
|
2020-06-09 12:21:20 +00:00
|
|
|
type executor struct {
|
2020-06-29 19:21:03 +00:00
|
|
|
notifiers []notifier.Notifier
|
|
|
|
rw *remotewrite.Client
|
2020-06-09 12:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *executor) execConcurrently(ctx context.Context, rules []Rule, concurrency int, interval time.Duration) chan error {
|
|
|
|
res := make(chan error, len(rules))
|
|
|
|
var returnSeries bool
|
|
|
|
if e.rw != nil {
|
|
|
|
returnSeries = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if concurrency == 1 {
|
|
|
|
// fast path
|
|
|
|
for _, rule := range rules {
|
|
|
|
res <- e.exec(ctx, rule, returnSeries, interval)
|
|
|
|
}
|
|
|
|
close(res)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
sem := make(chan struct{}, concurrency)
|
|
|
|
go func() {
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
for _, rule := range rules {
|
|
|
|
sem <- struct{}{}
|
|
|
|
wg.Add(1)
|
|
|
|
go func(r Rule) {
|
|
|
|
res <- e.exec(ctx, r, returnSeries, interval)
|
|
|
|
<-sem
|
|
|
|
wg.Done()
|
|
|
|
}(rule)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
close(res)
|
|
|
|
}()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
app/vmalert: extend metrics set exported by `vmalert` #573 (#654)
* app/vmalert: extend metrics set exported by `vmalert` #573
New metrics were added to improve observability:
+ vmalert_alerts_pending{alertname, group} - number of pending alerts per group
per alert;
+ vmalert_alerts_acitve{alertname, group} - number of active alerts per group
per alert;
+ vmalert_alerts_error{alertname, group} - is 1 if alertname ended up with error
during prev execution, is 0 if no errors happened;
+ vmalert_recording_rules_error{recording, group} - is 1 if recording rule
ended up with error during prev execution, is 0 if no errors happened;
* vmalert_iteration_total{group, file} - now contains group and file name labels.
This should improve control over specific groups;
* vmalert_iteration_duration_seconds{group, file} - now contains group and file name labels. This should improve control over specific groups;
Some collisions for alerts and recording rules are possible, because neither
group name nor alert/recording rule name are unique for compatibility reasons.
Commit contains list of TODOs for Unregistering metrics since groups and rules
are ephemeral and could be removed without application restart. In order to
unlock Unregistering feature corresponding PR was filed - https://github.com/VictoriaMetrics/metrics/pull/13
* app/vmalert: extend metrics set exported by `vmalert` #573
The changes are following:
* add an ID label to rules metrics, since `name` collisions within one group is
a common case - see the k8s example alerts;
* supports metrics unregistering on rule updates. Consider the case when one rule
was added or removed from the group, or the whole group was added or removed.
The change depends on https://github.com/VictoriaMetrics/metrics/pull/16
where race condition for Unregister method was fixed.
2020-08-09 06:41:29 +00:00
|
|
|
var (
|
|
|
|
execTotal = metrics.NewCounter(`vmalert_execution_total`)
|
|
|
|
execErrors = metrics.NewCounter(`vmalert_execution_errors_total`)
|
|
|
|
execDuration = metrics.NewSummary(`vmalert_execution_duration_seconds`)
|
|
|
|
|
|
|
|
remoteWriteErrors = metrics.NewCounter(`vmalert_remotewrite_errors_total`)
|
|
|
|
)
|
|
|
|
|
2020-06-09 12:21:20 +00:00
|
|
|
func (e *executor) exec(ctx context.Context, rule Rule, returnSeries bool, interval time.Duration) error {
|
|
|
|
execTotal.Inc()
|
|
|
|
execStart := time.Now()
|
|
|
|
defer func() {
|
|
|
|
execDuration.UpdateDuration(execStart)
|
|
|
|
}()
|
|
|
|
|
2021-04-28 20:41:15 +00:00
|
|
|
tss, err := rule.Exec(ctx, returnSeries)
|
2020-06-09 12:21:20 +00:00
|
|
|
if err != nil {
|
|
|
|
execErrors.Inc()
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("rule %q: failed to execute: %w", rule, err)
|
2020-06-09 12:21:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(tss) > 0 && e.rw != nil {
|
|
|
|
for _, ts := range tss {
|
|
|
|
if err := e.rw.Push(ts); err != nil {
|
|
|
|
remoteWriteErrors.Inc()
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("rule %q: remote write failure: %w", rule, err)
|
2020-05-10 16:58:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-09 12:21:20 +00:00
|
|
|
|
|
|
|
ar, ok := rule.(*AlertingRule)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var alerts []notifier.Alert
|
|
|
|
for _, a := range ar.alerts {
|
|
|
|
switch a.State {
|
|
|
|
case notifier.StateFiring:
|
|
|
|
// set End to execStart + 3 intervals
|
|
|
|
// so notifier can resolve it automatically if `vmalert`
|
|
|
|
// won't be able to send resolve for some reason
|
|
|
|
a.End = time.Now().Add(3 * interval)
|
|
|
|
alerts = append(alerts, *a)
|
|
|
|
case notifier.StateInactive:
|
|
|
|
// set End to execStart to notify
|
|
|
|
// that it was just resolved
|
|
|
|
a.End = time.Now()
|
|
|
|
alerts = append(alerts, *a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(alerts) < 1 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-29 19:21:03 +00:00
|
|
|
|
2020-06-09 12:21:20 +00:00
|
|
|
alertsSent.Add(len(alerts))
|
2020-06-29 19:21:03 +00:00
|
|
|
errGr := new(utils.ErrGroup)
|
|
|
|
for _, nt := range e.notifiers {
|
|
|
|
if err := nt.Send(ctx, alerts); err != nil {
|
|
|
|
alertsSendErrors.Inc()
|
2020-06-30 19:58:18 +00:00
|
|
|
errGr.Add(fmt.Errorf("rule %q: failed to send alerts: %w", rule, err))
|
2020-06-29 19:21:03 +00:00
|
|
|
}
|
2020-06-09 12:21:20 +00:00
|
|
|
}
|
2020-06-29 19:21:03 +00:00
|
|
|
return errGr.Err()
|
2020-05-10 16:58:17 +00:00
|
|
|
}
|