|
|
|
@ -25,6 +25,7 @@ import (
|
|
|
|
|
"github.com/prometheus/common/log" |
|
|
|
|
"github.com/prometheus/common/model" |
|
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/pkg/labels" |
|
|
|
|
"github.com/prometheus/prometheus/promql" |
|
|
|
|
"github.com/prometheus/prometheus/template" |
|
|
|
|
"github.com/prometheus/prometheus/util/strutil" |
|
|
|
@ -32,12 +33,12 @@ import (
|
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
|
// AlertMetricName is the metric name for synthetic alert timeseries.
|
|
|
|
|
alertMetricName model.LabelValue = "ALERTS" |
|
|
|
|
alertMetricName = "ALERTS" |
|
|
|
|
|
|
|
|
|
// AlertNameLabel is the label name indicating the name of an alert.
|
|
|
|
|
alertNameLabel model.LabelName = "alertname" |
|
|
|
|
alertNameLabel = "alertname" |
|
|
|
|
// AlertStateLabel is the label name indicating the state of an alert.
|
|
|
|
|
alertStateLabel model.LabelName = "alertstate" |
|
|
|
|
alertStateLabel = "alertstate" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// AlertState denotes the state of an active alert.
|
|
|
|
@ -68,11 +69,13 @@ func (s AlertState) String() string {
|
|
|
|
|
|
|
|
|
|
// Alert is the user-level representation of a single instance of an alerting rule.
|
|
|
|
|
type Alert struct { |
|
|
|
|
State AlertState |
|
|
|
|
Labels model.LabelSet |
|
|
|
|
Annotations model.LabelSet |
|
|
|
|
State AlertState |
|
|
|
|
|
|
|
|
|
Labels labels.Labels |
|
|
|
|
Annotations labels.Labels |
|
|
|
|
|
|
|
|
|
// The value at the last evaluation of the alerting expression.
|
|
|
|
|
Value model.SampleValue |
|
|
|
|
Value float64 |
|
|
|
|
// The interval during which the condition of this alert held true.
|
|
|
|
|
// ResolvedAt will be 0 to indicate a still active alert.
|
|
|
|
|
ActiveAt, ResolvedAt model.Time |
|
|
|
@ -88,26 +91,26 @@ type AlertingRule struct {
|
|
|
|
|
// output vector before an alert transitions from Pending to Firing state.
|
|
|
|
|
holdDuration time.Duration |
|
|
|
|
// Extra labels to attach to the resulting alert sample vectors.
|
|
|
|
|
labels model.LabelSet |
|
|
|
|
labels labels.Labels |
|
|
|
|
// Non-identifying key/value pairs.
|
|
|
|
|
annotations model.LabelSet |
|
|
|
|
annotations labels.Labels |
|
|
|
|
|
|
|
|
|
// Protects the below.
|
|
|
|
|
mtx sync.Mutex |
|
|
|
|
// A map of alerts which are currently active (Pending or Firing), keyed by
|
|
|
|
|
// the fingerprint of the labelset they correspond to.
|
|
|
|
|
active map[model.Fingerprint]*Alert |
|
|
|
|
active map[uint64]*Alert |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// NewAlertingRule constructs a new AlertingRule.
|
|
|
|
|
func NewAlertingRule(name string, vec promql.Expr, hold time.Duration, lbls, anns model.LabelSet) *AlertingRule { |
|
|
|
|
func NewAlertingRule(name string, vec promql.Expr, hold time.Duration, lbls, anns labels.Labels) *AlertingRule { |
|
|
|
|
return &AlertingRule{ |
|
|
|
|
name: name, |
|
|
|
|
vector: vec, |
|
|
|
|
holdDuration: hold, |
|
|
|
|
labels: lbls, |
|
|
|
|
annotations: anns, |
|
|
|
|
active: map[model.Fingerprint]*Alert{}, |
|
|
|
|
active: map[uint64]*Alert{}, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -117,27 +120,26 @@ func (r *AlertingRule) Name() string {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (r *AlertingRule) equal(o *AlertingRule) bool { |
|
|
|
|
return r.name == o.name && r.labels.Equal(o.labels) |
|
|
|
|
return r.name == o.name && labels.Equal(r.labels, o.labels) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (r *AlertingRule) sample(alert *Alert, ts model.Time, set bool) *model.Sample { |
|
|
|
|
metric := model.Metric(r.labels.Clone()) |
|
|
|
|
func (r *AlertingRule) sample(alert *Alert, ts model.Time, set bool) promql.Sample { |
|
|
|
|
lb := labels.NewBuilder(r.labels) |
|
|
|
|
|
|
|
|
|
for ln, lv := range alert.Labels { |
|
|
|
|
metric[ln] = lv |
|
|
|
|
for _, l := range alert.Labels { |
|
|
|
|
lb.Set(l.Name, l.Value) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
metric[model.MetricNameLabel] = alertMetricName |
|
|
|
|
metric[model.AlertNameLabel] = model.LabelValue(r.name) |
|
|
|
|
metric[alertStateLabel] = model.LabelValue(alert.State.String()) |
|
|
|
|
lb.Set(labels.MetricName, alertMetricName) |
|
|
|
|
lb.Set(labels.AlertName, r.name) |
|
|
|
|
lb.Set(alertStateLabel, alert.State.String()) |
|
|
|
|
|
|
|
|
|
s := &model.Sample{ |
|
|
|
|
Metric: metric, |
|
|
|
|
Timestamp: ts, |
|
|
|
|
Value: 0, |
|
|
|
|
s := promql.Sample{ |
|
|
|
|
Metric: lb.Labels(), |
|
|
|
|
Point: promql.Point{T: int64(ts), V: 0}, |
|
|
|
|
} |
|
|
|
|
if set { |
|
|
|
|
s.Value = 1 |
|
|
|
|
s.V = 1 |
|
|
|
|
} |
|
|
|
|
return s |
|
|
|
|
} |
|
|
|
@ -148,8 +150,8 @@ const resolvedRetention = 15 * time.Minute
|
|
|
|
|
|
|
|
|
|
// Eval evaluates the rule expression and then creates pending alerts and fires
|
|
|
|
|
// or removes previously pending alerts accordingly.
|
|
|
|
|
func (r *AlertingRule) Eval(ctx context.Context, ts model.Time, engine *promql.Engine, externalURLPath string) (model.Vector, error) { |
|
|
|
|
query, err := engine.NewInstantQuery(r.vector.String(), ts) |
|
|
|
|
func (r *AlertingRule) Eval(ctx context.Context, ts model.Time, engine *promql.Engine, externalURLPath string) (promql.Vector, error) { |
|
|
|
|
query, err := engine.NewInstantQuery(r.vector.String(), ts.Time()) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
@ -163,13 +165,13 @@ func (r *AlertingRule) Eval(ctx context.Context, ts model.Time, engine *promql.E
|
|
|
|
|
|
|
|
|
|
// Create pending alerts for any new vector elements in the alert expression
|
|
|
|
|
// or update the expression value for existing elements.
|
|
|
|
|
resultFPs := map[model.Fingerprint]struct{}{} |
|
|
|
|
resultFPs := map[uint64]struct{}{} |
|
|
|
|
|
|
|
|
|
for _, smpl := range res { |
|
|
|
|
// Provide the alert information to the template.
|
|
|
|
|
l := make(map[string]string, len(smpl.Metric)) |
|
|
|
|
for k, v := range smpl.Metric { |
|
|
|
|
l[string(k)] = string(v) |
|
|
|
|
for _, lbl := range smpl.Metric { |
|
|
|
|
l[lbl.Name] = lbl.Value |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
tmplData := struct { |
|
|
|
@ -177,13 +179,13 @@ func (r *AlertingRule) Eval(ctx context.Context, ts model.Time, engine *promql.E
|
|
|
|
|
Value float64 |
|
|
|
|
}{ |
|
|
|
|
Labels: l, |
|
|
|
|
Value: float64(smpl.Value), |
|
|
|
|
Value: smpl.V, |
|
|
|
|
} |
|
|
|
|
// Inject some convenience variables that are easier to remember for users
|
|
|
|
|
// who are not used to Go's templating system.
|
|
|
|
|
defs := "{{$labels := .Labels}}{{$value := .Value}}" |
|
|
|
|
|
|
|
|
|
expand := func(text model.LabelValue) model.LabelValue { |
|
|
|
|
expand := func(text string) string { |
|
|
|
|
tmpl := template.NewTemplateExpander( |
|
|
|
|
ctx, |
|
|
|
|
defs+string(text), |
|
|
|
@ -198,44 +200,42 @@ func (r *AlertingRule) Eval(ctx context.Context, ts model.Time, engine *promql.E
|
|
|
|
|
result = fmt.Sprintf("<error expanding template: %s>", err) |
|
|
|
|
log.Warnf("Error expanding alert template %v with data '%v': %s", r.Name(), tmplData, err) |
|
|
|
|
} |
|
|
|
|
return model.LabelValue(result) |
|
|
|
|
return result |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
delete(smpl.Metric, model.MetricNameLabel) |
|
|
|
|
labels := make(model.LabelSet, len(smpl.Metric)+len(r.labels)+1) |
|
|
|
|
for ln, lv := range smpl.Metric { |
|
|
|
|
labels[ln] = lv |
|
|
|
|
} |
|
|
|
|
for ln, lv := range r.labels { |
|
|
|
|
labels[ln] = expand(lv) |
|
|
|
|
lb := labels.NewBuilder(smpl.Metric).Del(labels.MetricName) |
|
|
|
|
|
|
|
|
|
for _, l := range r.labels { |
|
|
|
|
lb.Set(l.Name, expand(l.Value)) |
|
|
|
|
} |
|
|
|
|
labels[model.AlertNameLabel] = model.LabelValue(r.Name()) |
|
|
|
|
lb.Set(labels.AlertName, r.Name()) |
|
|
|
|
|
|
|
|
|
annotations := make(model.LabelSet, len(r.annotations)) |
|
|
|
|
for an, av := range r.annotations { |
|
|
|
|
annotations[an] = expand(av) |
|
|
|
|
annotations := make(labels.Labels, 0, len(r.annotations)) |
|
|
|
|
for _, a := range r.annotations { |
|
|
|
|
annotations = append(annotations, labels.Label{Name: a.Name, Value: expand(a.Value)}) |
|
|
|
|
} |
|
|
|
|
fp := smpl.Metric.Fingerprint() |
|
|
|
|
resultFPs[fp] = struct{}{} |
|
|
|
|
|
|
|
|
|
h := smpl.Metric.Hash() |
|
|
|
|
resultFPs[h] = struct{}{} |
|
|
|
|
|
|
|
|
|
// Check whether we already have alerting state for the identifying label set.
|
|
|
|
|
// Update the last value and annotations if so, create a new alert entry otherwise.
|
|
|
|
|
if alert, ok := r.active[fp]; ok && alert.State != StateInactive { |
|
|
|
|
alert.Value = smpl.Value |
|
|
|
|
if alert, ok := r.active[h]; ok && alert.State != StateInactive { |
|
|
|
|
alert.Value = smpl.V |
|
|
|
|
alert.Annotations = annotations |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
r.active[fp] = &Alert{ |
|
|
|
|
Labels: labels, |
|
|
|
|
r.active[h] = &Alert{ |
|
|
|
|
Labels: lb.Labels(), |
|
|
|
|
Annotations: annotations, |
|
|
|
|
ActiveAt: ts, |
|
|
|
|
State: StatePending, |
|
|
|
|
Value: smpl.Value, |
|
|
|
|
Value: smpl.V, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var vec model.Vector |
|
|
|
|
var vec promql.Vector |
|
|
|
|
// Check if any pending alerts should be removed or fire now. Write out alert timeseries.
|
|
|
|
|
for fp, a := range r.active { |
|
|
|
|
if _, ok := resultFPs[fp]; !ok { |
|
|
|
|