2015-01-21 19:07:45 +00:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-04-24 09:51:40 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
2013-04-26 14:02:52 +00:00
|
|
|
"fmt"
|
2013-06-13 14:10:05 +00:00
|
|
|
"html/template"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
2015-03-30 17:43:19 +00:00
|
|
|
"github.com/prometheus/prometheus/promql"
|
2015-05-29 11:30:30 +00:00
|
|
|
"github.com/prometheus/prometheus/util/strutil"
|
2013-04-24 09:51:40 +00:00
|
|
|
)
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
const (
|
2014-12-10 15:16:49 +00:00
|
|
|
// AlertMetricName is the metric name for synthetic alert timeseries.
|
2015-05-25 19:16:32 +00:00
|
|
|
alertMetricName clientmodel.LabelValue = "ALERTS"
|
2013-06-25 12:02:27 +00:00
|
|
|
|
2014-12-10 15:16:49 +00:00
|
|
|
// AlertNameLabel is the label name indicating the name of an alert.
|
2015-05-25 19:16:32 +00:00
|
|
|
alertNameLabel clientmodel.LabelName = "alertname"
|
2014-12-10 15:16:49 +00:00
|
|
|
// AlertStateLabel is the label name indicating the state of an alert.
|
2015-05-25 19:16:32 +00:00
|
|
|
alertStateLabel clientmodel.LabelName = "alertstate"
|
2013-06-25 12:02:27 +00:00
|
|
|
)
|
|
|
|
|
2014-12-10 15:16:49 +00:00
|
|
|
// AlertState denotes the state of an active alert.
|
2013-06-13 14:10:05 +00:00
|
|
|
type AlertState int
|
2013-04-24 09:51:40 +00:00
|
|
|
|
2013-06-13 14:10:05 +00:00
|
|
|
func (s AlertState) String() string {
|
2013-04-24 09:51:40 +00:00
|
|
|
switch s {
|
2015-05-25 19:16:32 +00:00
|
|
|
case StateInactive:
|
2013-06-13 14:10:05 +00:00
|
|
|
return "inactive"
|
2015-05-25 19:16:32 +00:00
|
|
|
case StatePending:
|
2013-05-16 05:38:31 +00:00
|
|
|
return "pending"
|
2015-05-25 19:16:32 +00:00
|
|
|
case StateFiring:
|
2013-05-16 05:38:31 +00:00
|
|
|
return "firing"
|
|
|
|
default:
|
|
|
|
panic("undefined")
|
2013-04-24 09:51:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2014-12-10 15:16:49 +00:00
|
|
|
// Inactive alerts are neither firing nor pending.
|
2015-05-25 19:16:32 +00:00
|
|
|
StateInactive AlertState = iota
|
2014-12-10 15:16:49 +00:00
|
|
|
// Pending alerts have been active for less than the configured
|
|
|
|
// threshold duration.
|
2015-05-25 19:16:32 +00:00
|
|
|
StatePending
|
2014-12-10 15:16:49 +00:00
|
|
|
// Firing alerts have been active for longer than the configured
|
|
|
|
// threshold duration.
|
2015-05-25 19:16:32 +00:00
|
|
|
StateFiring
|
2013-04-24 09:51:40 +00:00
|
|
|
)
|
|
|
|
|
2013-06-13 14:10:05 +00:00
|
|
|
// Alert is used to track active (pending/firing) alerts over time.
|
|
|
|
type Alert struct {
|
2013-04-24 09:51:40 +00:00
|
|
|
// The name of the alert.
|
2013-06-13 14:10:05 +00:00
|
|
|
Name string
|
2013-04-24 09:51:40 +00:00
|
|
|
// The vector element labelset triggering this alert.
|
2013-06-25 12:02:27 +00:00
|
|
|
Labels clientmodel.LabelSet
|
2014-12-10 15:16:49 +00:00
|
|
|
// The state of the alert (Pending or Firing).
|
2013-06-13 14:10:05 +00:00
|
|
|
State AlertState
|
2014-12-10 15:16:49 +00:00
|
|
|
// The time when the alert first transitioned into Pending state.
|
Use custom timestamp type for sample timestamps and related code.
So far we've been using Go's native time.Time for anything related to sample
timestamps. Since the range of time.Time is much bigger than what we need, this
has created two problems:
- there could be time.Time values which were out of the range/precision of the
time type that we persist to disk, therefore causing incorrectly ordered keys.
One bug caused by this was:
https://github.com/prometheus/prometheus/issues/367
It would be good to use a timestamp type that's more closely aligned with
what the underlying storage supports.
- sizeof(time.Time) is 192, while Prometheus should be ok with a single 64-bit
Unix timestamp (possibly even a 32-bit one). Since we store samples in large
numbers, this seriously affects memory usage. Furthermore, copying/working
with the data will be faster if it's smaller.
*MEMORY USAGE RESULTS*
Initial memory usage comparisons for a running Prometheus with 1 timeseries and
100,000 samples show roughly a 13% decrease in total (VIRT) memory usage. In my
tests, this advantage for some reason decreased a bit the more samples the
timeseries had (to 5-7% for millions of samples). This I can't fully explain,
but perhaps garbage collection issues were involved.
*WHEN TO USE THE NEW TIMESTAMP TYPE*
The new clientmodel.Timestamp type should be used whenever time
calculations are either directly or indirectly related to sample
timestamps.
For example:
- the timestamp of a sample itself
- all kinds of watermarks
- anything that may become or is compared to a sample timestamp (like the timestamp
passed into Target.Scrape()).
When to still use time.Time:
- for measuring durations/times not related to sample timestamps, like duration
telemetry exporting, timers that indicate how frequently to execute some
action, etc.
*NOTE ON OPERATOR OPTIMIZATION TESTS*
We don't use operator optimization code anymore, but it still lives in
the code as dead code. It still has tests, but I couldn't get all of them to
pass with the new timestamp format. I commented out the failing cases for now,
but we should probably remove the dead code soon. I just didn't want to do that
in the same change as this.
Change-Id: I821787414b0debe85c9fffaeb57abd453727af0f
2013-10-28 13:35:02 +00:00
|
|
|
ActiveSince clientmodel.Timestamp
|
2013-06-13 14:10:05 +00:00
|
|
|
// The value of the alert expression for this vector element.
|
2013-06-25 12:02:27 +00:00
|
|
|
Value clientmodel.SampleValue
|
2013-04-24 09:51:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sample returns a Sample suitable for recording the alert.
|
2015-03-30 17:43:19 +00:00
|
|
|
func (a Alert) sample(timestamp clientmodel.Timestamp, value clientmodel.SampleValue) *promql.Sample {
|
2013-06-25 12:02:27 +00:00
|
|
|
recordedMetric := clientmodel.Metric{}
|
2013-06-13 14:10:05 +00:00
|
|
|
for label, value := range a.Labels {
|
2013-04-24 09:51:40 +00:00
|
|
|
recordedMetric[label] = value
|
|
|
|
}
|
|
|
|
|
2015-05-25 19:16:32 +00:00
|
|
|
recordedMetric[clientmodel.MetricNameLabel] = alertMetricName
|
|
|
|
recordedMetric[alertNameLabel] = clientmodel.LabelValue(a.Name)
|
|
|
|
recordedMetric[alertStateLabel] = clientmodel.LabelValue(a.State.String())
|
2013-04-24 09:51:40 +00:00
|
|
|
|
2015-03-30 17:43:19 +00:00
|
|
|
return &promql.Sample{
|
2014-12-08 15:55:49 +00:00
|
|
|
Metric: clientmodel.COWMetric{
|
|
|
|
Metric: recordedMetric,
|
|
|
|
Copied: true,
|
|
|
|
},
|
2013-04-24 09:51:40 +00:00
|
|
|
Value: value,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:16:49 +00:00
|
|
|
// An AlertingRule generates alerts from its vector expression.
|
2013-04-24 09:51:40 +00:00
|
|
|
type AlertingRule struct {
|
|
|
|
// The name of the alert.
|
2013-04-05 16:03:45 +00:00
|
|
|
name string
|
|
|
|
// The vector expression from which to generate alerts.
|
2015-05-25 19:16:32 +00:00
|
|
|
vector promql.Expr
|
2013-04-24 09:51:40 +00:00
|
|
|
// The duration for which a labelset needs to persist in the expression
|
2014-12-10 15:16:49 +00:00
|
|
|
// output vector before an alert transitions from Pending to Firing state.
|
2013-04-24 09:51:40 +00:00
|
|
|
holdDuration time.Duration
|
|
|
|
// Extra labels to attach to the resulting alert sample vectors.
|
2015-05-25 19:16:32 +00:00
|
|
|
labels clientmodel.LabelSet
|
2013-07-30 15:18:07 +00:00
|
|
|
// Short alert summary, suitable for email subjects.
|
2015-05-25 19:16:32 +00:00
|
|
|
summary string
|
2013-07-30 15:18:07 +00:00
|
|
|
// More detailed alert description.
|
2015-05-25 19:16:32 +00:00
|
|
|
description string
|
2013-06-13 14:10:05 +00:00
|
|
|
|
|
|
|
// Protects the below.
|
|
|
|
mutex sync.Mutex
|
2014-12-10 15:16:49 +00:00
|
|
|
// A map of alerts which are currently active (Pending or Firing), keyed by
|
2013-04-24 09:51:40 +00:00
|
|
|
// the fingerprint of the labelset they correspond to.
|
2013-06-25 12:02:27 +00:00
|
|
|
activeAlerts map[clientmodel.Fingerprint]*Alert
|
2013-04-24 09:51:40 +00:00
|
|
|
}
|
|
|
|
|
2015-05-25 19:16:32 +00:00
|
|
|
// NewAlertingRule constructs a new AlertingRule.
|
|
|
|
func NewAlertingRule(
|
|
|
|
name string,
|
|
|
|
vector promql.Expr,
|
|
|
|
holdDuration time.Duration,
|
|
|
|
labels clientmodel.LabelSet,
|
|
|
|
summary string,
|
|
|
|
description string,
|
|
|
|
) *AlertingRule {
|
|
|
|
return &AlertingRule{
|
|
|
|
name: name,
|
|
|
|
vector: vector,
|
|
|
|
holdDuration: holdDuration,
|
|
|
|
labels: labels,
|
|
|
|
summary: summary,
|
|
|
|
description: description,
|
|
|
|
|
|
|
|
activeAlerts: map[clientmodel.Fingerprint]*Alert{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:16:49 +00:00
|
|
|
// Name returns the name of the alert.
|
2013-07-30 15:18:07 +00:00
|
|
|
func (rule *AlertingRule) Name() string {
|
|
|
|
return rule.name
|
|
|
|
}
|
2013-04-24 09:51:40 +00:00
|
|
|
|
2015-05-25 19:16:32 +00:00
|
|
|
// eval evaluates the rule expression and then creates pending alerts and fires
|
2015-05-25 18:43:24 +00:00
|
|
|
// or removes previously pending alerts accordingly.
|
2015-05-25 19:16:32 +00:00
|
|
|
func (rule *AlertingRule) eval(timestamp clientmodel.Timestamp, engine *promql.Engine) (promql.Vector, error) {
|
|
|
|
query, err := engine.NewInstantQuery(rule.vector.String(), timestamp)
|
2015-03-30 17:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-25 18:43:24 +00:00
|
|
|
exprResult, err := query.Exec().Vector()
|
2013-04-24 09:51:40 +00:00
|
|
|
if err != nil {
|
2013-05-16 05:38:31 +00:00
|
|
|
return nil, err
|
2013-04-24 09:51:40 +00:00
|
|
|
}
|
|
|
|
|
2013-06-13 14:10:05 +00:00
|
|
|
rule.mutex.Lock()
|
|
|
|
defer rule.mutex.Unlock()
|
|
|
|
|
2013-06-14 11:03:19 +00:00
|
|
|
// Create pending alerts for any new vector elements in the alert expression
|
|
|
|
// or update the expression value for existing elements.
|
2015-05-28 19:51:44 +00:00
|
|
|
resultFPs := map[clientmodel.Fingerprint]struct{}{}
|
2013-04-24 09:51:40 +00:00
|
|
|
for _, sample := range exprResult {
|
2014-12-08 15:55:49 +00:00
|
|
|
fp := sample.Metric.Metric.Fingerprint()
|
2015-05-28 19:51:44 +00:00
|
|
|
resultFPs[fp] = struct{}{}
|
2013-06-25 12:02:27 +00:00
|
|
|
|
2014-12-08 15:55:49 +00:00
|
|
|
if alert, ok := rule.activeAlerts[fp]; !ok {
|
2013-06-25 12:02:27 +00:00
|
|
|
labels := clientmodel.LabelSet{}
|
2014-12-08 15:55:49 +00:00
|
|
|
labels.MergeFromMetric(sample.Metric.Metric)
|
2015-05-25 19:16:32 +00:00
|
|
|
labels = labels.Merge(rule.labels)
|
2013-06-25 12:02:27 +00:00
|
|
|
if _, ok := labels[clientmodel.MetricNameLabel]; ok {
|
|
|
|
delete(labels, clientmodel.MetricNameLabel)
|
2013-06-13 14:10:05 +00:00
|
|
|
}
|
2014-12-08 15:55:49 +00:00
|
|
|
rule.activeAlerts[fp] = &Alert{
|
2013-06-13 14:10:05 +00:00
|
|
|
Name: rule.name,
|
2013-06-14 11:03:19 +00:00
|
|
|
Labels: labels,
|
2015-05-25 19:16:32 +00:00
|
|
|
State: StatePending,
|
2013-06-13 14:10:05 +00:00
|
|
|
ActiveSince: timestamp,
|
|
|
|
Value: sample.Value,
|
2013-04-24 09:51:40 +00:00
|
|
|
}
|
2013-06-13 14:10:05 +00:00
|
|
|
} else {
|
|
|
|
alert.Value = sample.Value
|
2013-04-24 09:51:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 17:43:19 +00:00
|
|
|
vector := promql.Vector{}
|
2013-05-16 05:38:31 +00:00
|
|
|
|
2013-04-24 09:51:40 +00:00
|
|
|
// Check if any pending alerts should be removed or fire now. Write out alert timeseries.
|
|
|
|
for fp, activeAlert := range rule.activeAlerts {
|
2015-05-28 19:51:44 +00:00
|
|
|
if _, ok := resultFPs[fp]; !ok {
|
2013-04-24 09:51:40 +00:00
|
|
|
vector = append(vector, activeAlert.sample(timestamp, 0))
|
|
|
|
delete(rule.activeAlerts, fp)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-05-25 19:16:32 +00:00
|
|
|
if activeAlert.State == StatePending && timestamp.Sub(activeAlert.ActiveSince) >= rule.holdDuration {
|
2013-04-24 09:51:40 +00:00
|
|
|
vector = append(vector, activeAlert.sample(timestamp, 0))
|
2015-05-25 19:16:32 +00:00
|
|
|
activeAlert.State = StateFiring
|
2013-04-24 09:51:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vector = append(vector, activeAlert.sample(timestamp, 1))
|
|
|
|
}
|
2013-05-16 05:38:31 +00:00
|
|
|
|
|
|
|
return vector, nil
|
2013-04-24 09:51:40 +00:00
|
|
|
}
|
|
|
|
|
2013-06-13 14:10:05 +00:00
|
|
|
func (rule *AlertingRule) String() string {
|
2015-06-23 15:46:57 +00:00
|
|
|
s := fmt.Sprintf("ALERT %s", rule.name)
|
|
|
|
s += fmt.Sprintf("\n\tIF %s", rule.vector)
|
|
|
|
if rule.holdDuration > 0 {
|
|
|
|
s += fmt.Sprintf("\n\tFOR %s", strutil.DurationToString(rule.holdDuration))
|
|
|
|
}
|
|
|
|
if len(rule.labels) > 0 {
|
|
|
|
s += fmt.Sprintf("\n\tWITH %s", rule.labels)
|
|
|
|
}
|
|
|
|
s += fmt.Sprintf("\n\tSUMMARY %q", rule.summary)
|
|
|
|
s += fmt.Sprintf("\n\tDESCRIPTION %q", rule.description)
|
|
|
|
return s
|
2013-06-13 14:10:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 15:46:57 +00:00
|
|
|
// HTMLSnippet returns an HTML snippet representing this alerting rule. The
|
|
|
|
// resulting snippet is expected to be presented in a <pre> element, so that
|
|
|
|
// line breaks and other returned whitespace is respected.
|
2015-05-18 17:44:40 +00:00
|
|
|
func (rule *AlertingRule) HTMLSnippet(pathPrefix string) template.HTML {
|
2013-06-25 12:02:27 +00:00
|
|
|
alertMetric := clientmodel.Metric{
|
2015-05-25 19:16:32 +00:00
|
|
|
clientmodel.MetricNameLabel: alertMetricName,
|
|
|
|
alertNameLabel: clientmodel.LabelValue(rule.name),
|
2013-06-13 14:10:05 +00:00
|
|
|
}
|
2015-06-23 15:46:57 +00:00
|
|
|
s := fmt.Sprintf("ALERT <a href=%q>%s</a>", pathPrefix+strutil.GraphLinkForExpression(alertMetric.String()), rule.name)
|
|
|
|
s += fmt.Sprintf("\n IF <a href=%q>%s</a>", pathPrefix+strutil.GraphLinkForExpression(rule.vector.String()), rule.vector)
|
|
|
|
if rule.holdDuration > 0 {
|
|
|
|
s += fmt.Sprintf("\n FOR %s", strutil.DurationToString(rule.holdDuration))
|
|
|
|
}
|
|
|
|
if len(rule.labels) > 0 {
|
|
|
|
s += fmt.Sprintf("\n WITH %s", rule.labels)
|
|
|
|
}
|
|
|
|
s += fmt.Sprintf("\n SUMMARY %q", rule.summary)
|
|
|
|
s += fmt.Sprintf("\n DESCRIPTION %q", rule.description)
|
|
|
|
return template.HTML(s)
|
2013-06-13 14:10:05 +00:00
|
|
|
}
|
|
|
|
|
2014-12-10 15:16:49 +00:00
|
|
|
// State returns the "maximum" state: firing > pending > inactive.
|
2013-06-13 14:10:05 +00:00
|
|
|
func (rule *AlertingRule) State() AlertState {
|
|
|
|
rule.mutex.Lock()
|
|
|
|
defer rule.mutex.Unlock()
|
|
|
|
|
2015-05-25 19:16:32 +00:00
|
|
|
maxState := StateInactive
|
2013-06-13 14:10:05 +00:00
|
|
|
for _, activeAlert := range rule.activeAlerts {
|
|
|
|
if activeAlert.State > maxState {
|
|
|
|
maxState = activeAlert.State
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return maxState
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:16:49 +00:00
|
|
|
// ActiveAlerts returns a slice of active alerts.
|
2013-06-13 14:10:05 +00:00
|
|
|
func (rule *AlertingRule) ActiveAlerts() []Alert {
|
|
|
|
rule.mutex.Lock()
|
|
|
|
defer rule.mutex.Unlock()
|
|
|
|
|
|
|
|
alerts := make([]Alert, 0, len(rule.activeAlerts))
|
|
|
|
for _, alert := range rule.activeAlerts {
|
|
|
|
alerts = append(alerts, *alert)
|
|
|
|
}
|
|
|
|
return alerts
|
2013-06-06 13:12:37 +00:00
|
|
|
}
|