2020-06-01 12:46:37 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"hash/fnv"
|
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/config"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/datasource"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
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 08:41:29 +02:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
2020-06-01 12:46:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// RecordingRule is a Rule that supposed
|
|
|
|
// to evaluate configured Expression and
|
|
|
|
// return TimeSeries as result.
|
|
|
|
type RecordingRule struct {
|
2021-02-01 14:02:44 +01:00
|
|
|
Type datasource.Type
|
2020-06-15 21:15:47 +02:00
|
|
|
RuleID uint64
|
2020-06-01 12:46:37 +02:00
|
|
|
Name string
|
|
|
|
Expr string
|
|
|
|
Labels map[string]string
|
|
|
|
GroupID uint64
|
|
|
|
|
2021-04-28 22:41:15 +02:00
|
|
|
q datasource.Querier
|
|
|
|
|
2020-06-01 12:46:37 +02:00
|
|
|
// guard status fields
|
|
|
|
mu sync.RWMutex
|
|
|
|
// stores last moment of time Exec was called
|
|
|
|
lastExecTime time.Time
|
|
|
|
// stores last error that happened in Exec func
|
|
|
|
// resets on every successful Exec
|
|
|
|
// may be used as Health state
|
|
|
|
lastExecError error
|
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 08:41:29 +02:00
|
|
|
|
|
|
|
metrics *recordingRuleMetrics
|
|
|
|
}
|
|
|
|
|
|
|
|
type recordingRuleMetrics struct {
|
|
|
|
errors *gauge
|
2020-06-01 12:46:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// String implements Stringer interface
|
|
|
|
func (rr *RecordingRule) String() string {
|
|
|
|
return rr.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns unique Rule ID
|
|
|
|
// within the parent Group.
|
|
|
|
func (rr *RecordingRule) ID() uint64 {
|
2020-06-15 21:15:47 +02:00
|
|
|
return rr.RuleID
|
2020-06-01 12:46:37 +02:00
|
|
|
}
|
|
|
|
|
2021-04-28 22:41:15 +02:00
|
|
|
func newRecordingRule(qb datasource.QuerierBuilder, group *Group, cfg config.Rule) *RecordingRule {
|
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 08:41:29 +02:00
|
|
|
rr := &RecordingRule{
|
2021-02-01 14:02:44 +01:00
|
|
|
Type: cfg.Type,
|
2020-06-15 21:15:47 +02:00
|
|
|
RuleID: cfg.ID,
|
2020-06-01 12:46:37 +02:00
|
|
|
Name: cfg.Record,
|
|
|
|
Expr: cfg.Expr,
|
|
|
|
Labels: cfg.Labels,
|
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 08:41:29 +02:00
|
|
|
GroupID: group.ID(),
|
|
|
|
metrics: &recordingRuleMetrics{},
|
2021-04-30 08:46:03 +02:00
|
|
|
q: qb.BuildWithParams(datasource.QuerierParams{
|
|
|
|
DataSourceType: &cfg.Type,
|
|
|
|
EvaluationInterval: group.Interval,
|
|
|
|
}),
|
2020-06-01 12:46:37 +02:00
|
|
|
}
|
2021-02-01 14:02:44 +01: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 08:41:29 +02:00
|
|
|
labels := fmt.Sprintf(`recording=%q, group=%q, id="%d"`, rr.Name, group.Name, rr.ID())
|
|
|
|
rr.metrics.errors = getOrCreateGauge(fmt.Sprintf(`vmalert_recording_rules_error{%s}`, labels),
|
|
|
|
func() float64 {
|
|
|
|
rr.mu.Lock()
|
|
|
|
defer rr.mu.Unlock()
|
|
|
|
if rr.lastExecError == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
})
|
|
|
|
return rr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close unregisters rule metrics
|
|
|
|
func (rr *RecordingRule) Close() {
|
|
|
|
metrics.UnregisterMetric(rr.metrics.errors.name)
|
2020-06-01 12:46:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exec executes RecordingRule expression via the given Querier.
|
2021-04-28 22:41:15 +02:00
|
|
|
func (rr *RecordingRule) Exec(ctx context.Context, series bool) ([]prompbmarshal.TimeSeries, error) {
|
2020-06-01 12:46:37 +02:00
|
|
|
if !series {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-04-28 22:41:15 +02:00
|
|
|
qMetrics, err := rr.q.Query(ctx, rr.Expr)
|
2020-06-01 12:46:37 +02:00
|
|
|
rr.mu.Lock()
|
|
|
|
defer rr.mu.Unlock()
|
|
|
|
|
|
|
|
rr.lastExecTime = time.Now()
|
|
|
|
rr.lastExecError = err
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return nil, fmt.Errorf("failed to execute query %q: %w", rr.Expr, err)
|
2020-06-01 12:46:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
duplicates := make(map[uint64]prompbmarshal.TimeSeries, len(qMetrics))
|
|
|
|
var tss []prompbmarshal.TimeSeries
|
|
|
|
for _, r := range qMetrics {
|
2021-04-26 00:03:22 +02:00
|
|
|
ts := rr.toTimeSeries(r, time.Unix(r.Timestamp, 0))
|
2020-06-01 12:46:37 +02:00
|
|
|
h := hashTimeSeries(ts)
|
|
|
|
if _, ok := duplicates[h]; ok {
|
|
|
|
rr.lastExecError = errDuplicate
|
|
|
|
return nil, errDuplicate
|
|
|
|
}
|
|
|
|
duplicates[h] = ts
|
|
|
|
tss = append(tss, ts)
|
|
|
|
}
|
|
|
|
return tss, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hashTimeSeries(ts prompbmarshal.TimeSeries) uint64 {
|
|
|
|
hash := fnv.New64a()
|
|
|
|
labels := ts.Labels
|
|
|
|
sort.Slice(labels, func(i, j int) bool {
|
|
|
|
return labels[i].Name < labels[j].Name
|
|
|
|
})
|
|
|
|
for _, l := range labels {
|
|
|
|
hash.Write([]byte(l.Name))
|
|
|
|
hash.Write([]byte(l.Value))
|
|
|
|
hash.Write([]byte("\xff"))
|
|
|
|
}
|
|
|
|
return hash.Sum64()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rr *RecordingRule) toTimeSeries(m datasource.Metric, timestamp time.Time) prompbmarshal.TimeSeries {
|
|
|
|
labels := make(map[string]string)
|
|
|
|
for _, l := range m.Labels {
|
|
|
|
labels[l.Name] = l.Value
|
|
|
|
}
|
|
|
|
labels["__name__"] = rr.Name
|
|
|
|
// override existing labels with configured ones
|
|
|
|
for k, v := range rr.Labels {
|
|
|
|
labels[k] = v
|
|
|
|
}
|
|
|
|
return newTimeSeries(m.Value, labels, timestamp)
|
|
|
|
}
|
|
|
|
|
2020-06-01 13:34:58 +02:00
|
|
|
// UpdateWith copies all significant fields.
|
2020-06-01 12:46:37 +02:00
|
|
|
// alerts state isn't copied since
|
|
|
|
// it should be updated in next 2 Execs
|
|
|
|
func (rr *RecordingRule) UpdateWith(r Rule) error {
|
|
|
|
nr, ok := r.(*RecordingRule)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("BUG: attempt to update recroding rule with wrong type %#v", r)
|
|
|
|
}
|
|
|
|
rr.Expr = nr.Expr
|
|
|
|
rr.Labels = nr.Labels
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RuleAPI returns Rule representation in form
|
|
|
|
// of APIRecordingRule
|
|
|
|
func (rr *RecordingRule) RuleAPI() APIRecordingRule {
|
|
|
|
var lastErr string
|
|
|
|
if rr.lastExecError != nil {
|
|
|
|
lastErr = rr.lastExecError.Error()
|
|
|
|
}
|
|
|
|
return APIRecordingRule{
|
|
|
|
// encode as strings to avoid rounding
|
|
|
|
ID: fmt.Sprintf("%d", rr.ID()),
|
|
|
|
GroupID: fmt.Sprintf("%d", rr.GroupID),
|
|
|
|
Name: rr.Name,
|
2021-02-01 14:02:44 +01:00
|
|
|
Type: rr.Type.String(),
|
2020-06-01 12:46:37 +02:00
|
|
|
Expression: rr.Expr,
|
|
|
|
LastError: lastErr,
|
|
|
|
LastExec: rr.lastExecTime,
|
|
|
|
Labels: rr.Labels,
|
|
|
|
}
|
|
|
|
}
|