2015-01-21 19:07:45 +00:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-01-07 22:24:26 +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.
|
|
|
|
|
2015-03-30 17:43:19 +00:00
|
|
|
package rules
|
2013-01-07 22:24:26 +00:00
|
|
|
|
|
|
|
import (
|
2013-12-10 15:54:35 +00:00
|
|
|
"fmt"
|
2015-04-29 09:08:56 +00:00
|
|
|
"io/ioutil"
|
2015-06-30 12:38:01 +00:00
|
|
|
"net/url"
|
2015-05-28 05:36:21 +00:00
|
|
|
"path/filepath"
|
2013-04-17 12:42:15 +00:00
|
|
|
"sync"
|
2013-01-07 22:24:26 +00:00
|
|
|
"time"
|
|
|
|
|
2015-06-30 09:51:05 +00:00
|
|
|
html_template "html/template"
|
|
|
|
|
2014-06-18 17:43:15 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-10-03 08:21:43 +00:00
|
|
|
"github.com/prometheus/common/log"
|
2015-08-20 15:18:46 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2013-06-25 12:02:27 +00:00
|
|
|
|
2015-05-12 14:52:56 +00:00
|
|
|
"github.com/prometheus/prometheus/config"
|
2013-08-09 17:32:55 +00:00
|
|
|
"github.com/prometheus/prometheus/notification"
|
2015-03-30 17:43:19 +00:00
|
|
|
"github.com/prometheus/prometheus/promql"
|
2015-03-15 02:36:15 +00:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2015-05-28 19:22:08 +00:00
|
|
|
"github.com/prometheus/prometheus/template"
|
2015-05-29 11:30:30 +00:00
|
|
|
"github.com/prometheus/prometheus/util/strutil"
|
2013-06-25 12:02:27 +00:00
|
|
|
)
|
2013-01-07 22:24:26 +00:00
|
|
|
|
2014-06-18 17:43:15 +00:00
|
|
|
// Constants for instrumentation.
|
|
|
|
const (
|
2014-07-23 17:55:33 +00:00
|
|
|
namespace = "prometheus"
|
|
|
|
|
2014-06-18 17:43:15 +00:00
|
|
|
ruleTypeLabel = "rule_type"
|
2015-05-25 19:16:32 +00:00
|
|
|
ruleTypeAlerting = "alerting"
|
|
|
|
ruleTypeRecording = "recording"
|
2014-06-18 17:43:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
evalDuration = prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
2014-07-23 17:55:33 +00:00
|
|
|
Namespace: namespace,
|
|
|
|
Name: "rule_evaluation_duration_milliseconds",
|
|
|
|
Help: "The duration for a rule to execute.",
|
2014-06-18 17:43:15 +00:00
|
|
|
},
|
|
|
|
[]string{ruleTypeLabel},
|
|
|
|
)
|
2014-12-31 12:16:08 +00:00
|
|
|
evalFailures = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Name: "rule_evaluation_failures_total",
|
|
|
|
Help: "The total number of rule evaluation failures.",
|
|
|
|
},
|
|
|
|
)
|
2014-07-23 17:55:33 +00:00
|
|
|
iterationDuration = prometheus.NewSummary(prometheus.SummaryOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Name: "evaluator_duration_milliseconds",
|
|
|
|
Help: "The duration for all evaluations to execute.",
|
2015-01-21 14:42:25 +00:00
|
|
|
Objectives: map[float64]float64{0.01: 0.001, 0.05: 0.005, 0.5: 0.05, 0.90: 0.01, 0.99: 0.001},
|
2014-07-23 17:55:33 +00:00
|
|
|
})
|
2014-06-18 17:43:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(iterationDuration)
|
2014-12-31 12:16:08 +00:00
|
|
|
prometheus.MustRegister(evalFailures)
|
2014-06-18 17:43:15 +00:00
|
|
|
prometheus.MustRegister(evalDuration)
|
|
|
|
}
|
|
|
|
|
2015-06-30 09:18:07 +00:00
|
|
|
// A Rule encapsulates a vector expression which is evaluated at a specified
|
|
|
|
// interval and acted upon (currently either recorded or used for alerting).
|
|
|
|
type Rule interface {
|
|
|
|
// Name returns the name of the rule.
|
|
|
|
Name() string
|
|
|
|
// Eval evaluates the rule, including any associated recording or alerting actions.
|
2015-08-24 16:04:41 +00:00
|
|
|
eval(model.Time, *promql.Engine) (model.Vector, error)
|
2015-06-30 09:18:07 +00:00
|
|
|
// String returns a human-readable string representation of the rule.
|
|
|
|
String() string
|
|
|
|
// HTMLSnippet returns a human-readable string representation of the rule,
|
|
|
|
// decorated with HTML elements for use the web frontend.
|
2015-06-30 09:51:05 +00:00
|
|
|
HTMLSnippet(pathPrefix string) html_template.HTML
|
2015-06-30 09:18:07 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 08:26:49 +00:00
|
|
|
// The Manager manages recording and alerting rules.
|
|
|
|
type Manager struct {
|
2013-06-05 11:56:56 +00:00
|
|
|
// Protects the rules list.
|
|
|
|
sync.Mutex
|
2015-03-30 17:43:19 +00:00
|
|
|
rules []Rule
|
2013-06-05 11:56:56 +00:00
|
|
|
|
2013-08-20 13:42:06 +00:00
|
|
|
done chan bool
|
|
|
|
|
2015-03-30 17:43:19 +00:00
|
|
|
interval time.Duration
|
|
|
|
queryEngine *promql.Engine
|
2013-08-20 13:42:06 +00:00
|
|
|
|
2015-03-15 02:36:15 +00:00
|
|
|
sampleAppender storage.SampleAppender
|
2014-10-10 12:19:02 +00:00
|
|
|
notificationHandler *notification.NotificationHandler
|
2013-08-20 13:42:06 +00:00
|
|
|
|
2015-06-30 12:38:01 +00:00
|
|
|
externalURL *url.URL
|
2013-08-20 13:42:06 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 08:26:49 +00:00
|
|
|
// ManagerOptions bundles options for the Manager.
|
|
|
|
type ManagerOptions struct {
|
2013-08-20 13:42:06 +00:00
|
|
|
EvaluationInterval time.Duration
|
2015-03-30 17:43:19 +00:00
|
|
|
QueryEngine *promql.Engine
|
2013-08-20 13:42:06 +00:00
|
|
|
|
2014-10-10 12:19:02 +00:00
|
|
|
NotificationHandler *notification.NotificationHandler
|
2015-03-15 02:36:15 +00:00
|
|
|
SampleAppender storage.SampleAppender
|
2013-08-20 13:42:06 +00:00
|
|
|
|
2015-06-30 12:38:01 +00:00
|
|
|
ExternalURL *url.URL
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 08:26:49 +00:00
|
|
|
// NewManager returns an implementation of Manager, ready to be started
|
2014-12-10 15:16:49 +00:00
|
|
|
// by calling the Run method.
|
2015-04-29 08:26:49 +00:00
|
|
|
func NewManager(o *ManagerOptions) *Manager {
|
|
|
|
manager := &Manager{
|
2015-03-30 17:43:19 +00:00
|
|
|
rules: []Rule{},
|
2013-08-20 13:42:06 +00:00
|
|
|
done: make(chan bool),
|
|
|
|
|
2014-10-10 12:19:02 +00:00
|
|
|
interval: o.EvaluationInterval,
|
2015-03-15 02:36:15 +00:00
|
|
|
sampleAppender: o.SampleAppender,
|
2015-03-30 17:43:19 +00:00
|
|
|
queryEngine: o.QueryEngine,
|
2014-10-10 12:19:02 +00:00
|
|
|
notificationHandler: o.NotificationHandler,
|
2015-06-30 12:38:01 +00:00
|
|
|
externalURL: o.ExternalURL,
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
return manager
|
|
|
|
}
|
|
|
|
|
2015-04-29 08:26:49 +00:00
|
|
|
// Run the rule manager's periodic rule evaluation.
|
|
|
|
func (m *Manager) Run() {
|
2015-05-20 16:10:29 +00:00
|
|
|
defer log.Info("Rule manager stopped.")
|
2015-01-29 14:05:10 +00:00
|
|
|
|
2015-05-12 14:52:56 +00:00
|
|
|
m.Lock()
|
|
|
|
lastInterval := m.interval
|
|
|
|
m.Unlock()
|
|
|
|
|
|
|
|
ticker := time.NewTicker(lastInterval)
|
2013-05-23 19:29:27 +00:00
|
|
|
defer ticker.Stop()
|
2013-01-07 22:24:26 +00:00
|
|
|
|
|
|
|
for {
|
2015-01-29 14:05:10 +00:00
|
|
|
// The outer select clause makes sure that m.done is looked at
|
|
|
|
// first. Otherwise, if m.runIteration takes longer than
|
|
|
|
// m.interval, there is only a 50% chance that m.done will be
|
|
|
|
// looked at before the next m.runIteration call happens.
|
2013-01-07 22:24:26 +00:00
|
|
|
select {
|
|
|
|
case <-m.done:
|
2013-12-11 14:30:27 +00:00
|
|
|
return
|
2015-01-29 14:05:10 +00:00
|
|
|
default:
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
start := time.Now()
|
2015-03-15 02:36:15 +00:00
|
|
|
m.runIteration()
|
2015-01-29 14:05:10 +00:00
|
|
|
iterationDuration.Observe(float64(time.Since(start) / time.Millisecond))
|
2015-05-12 14:52:56 +00:00
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
if lastInterval != m.interval {
|
|
|
|
ticker.Stop()
|
|
|
|
ticker = time.NewTicker(m.interval)
|
|
|
|
lastInterval = m.interval
|
|
|
|
}
|
|
|
|
m.Unlock()
|
2015-01-29 14:05:10 +00:00
|
|
|
case <-m.done:
|
|
|
|
return
|
|
|
|
}
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 08:26:49 +00:00
|
|
|
// Stop the rule manager's rule evaluation cycles.
|
|
|
|
func (m *Manager) Stop() {
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Info("Stopping rule manager...")
|
2014-06-06 09:55:53 +00:00
|
|
|
m.done <- true
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 15:18:46 +00:00
|
|
|
func (m *Manager) queueAlertNotifications(rule *AlertingRule, timestamp model.Time) {
|
2013-07-30 15:18:07 +00:00
|
|
|
activeAlerts := rule.ActiveAlerts()
|
|
|
|
if len(activeAlerts) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-09 17:32:55 +00:00
|
|
|
notifications := make(notification.NotificationReqs, 0, len(activeAlerts))
|
2013-07-30 15:18:07 +00:00
|
|
|
for _, aa := range activeAlerts {
|
2015-05-25 19:16:32 +00:00
|
|
|
if aa.State != StateFiring {
|
2013-07-30 15:18:07 +00:00
|
|
|
// BUG: In the future, make AlertManager support pending alerts?
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-05-28 17:44:54 +00:00
|
|
|
// Provide the alert information to the template.
|
|
|
|
l := map[string]string{}
|
|
|
|
for k, v := range aa.Labels {
|
|
|
|
l[string(k)] = string(v)
|
|
|
|
}
|
|
|
|
tmplData := struct {
|
|
|
|
Labels map[string]string
|
2015-08-21 15:45:56 +00:00
|
|
|
Value float64
|
2014-05-28 17:44:54 +00:00
|
|
|
}{
|
|
|
|
Labels: l,
|
2015-08-21 15:45:56 +00:00
|
|
|
Value: float64(aa.Value),
|
2014-05-28 17:44:54 +00:00
|
|
|
}
|
|
|
|
// 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 string) string {
|
2015-06-30 12:38:01 +00:00
|
|
|
tmpl := template.NewTemplateExpander(defs+text, "__alert_"+rule.Name(), tmplData, timestamp, m.queryEngine, m.externalURL.Path)
|
2015-05-28 19:22:08 +00:00
|
|
|
result, err := tmpl.Expand()
|
2014-05-28 17:44:54 +00:00
|
|
|
if err != nil {
|
|
|
|
result = err.Error()
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Warnf("Error expanding alert template %v with data '%v': %v", rule.Name(), tmplData, err)
|
2014-05-28 17:44:54 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2013-08-09 17:32:55 +00:00
|
|
|
notifications = append(notifications, ¬ification.NotificationReq{
|
2015-05-25 19:16:32 +00:00
|
|
|
Summary: expand(rule.summary),
|
|
|
|
Description: expand(rule.description),
|
2015-06-24 17:27:09 +00:00
|
|
|
Runbook: rule.runbook,
|
2015-08-20 15:18:46 +00:00
|
|
|
Labels: aa.Labels.Merge(model.LabelSet{
|
|
|
|
alertNameLabel: model.LabelValue(rule.Name()),
|
2013-08-09 17:32:55 +00:00
|
|
|
}),
|
2013-08-20 13:42:06 +00:00
|
|
|
Value: aa.Value,
|
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: aa.ActiveSince.Time(),
|
2013-08-20 13:42:06 +00:00
|
|
|
RuleString: rule.String(),
|
2015-06-30 12:38:01 +00:00
|
|
|
GeneratorURL: m.externalURL.String() + strutil.GraphLinkForExpression(rule.vector.String()),
|
2013-07-30 15:18:07 +00:00
|
|
|
})
|
|
|
|
}
|
2014-10-10 12:19:02 +00:00
|
|
|
m.notificationHandler.SubmitReqs(notifications)
|
2013-07-30 15:18:07 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 08:26:49 +00:00
|
|
|
func (m *Manager) runIteration() {
|
2015-08-20 15:18:46 +00:00
|
|
|
now := model.Now()
|
2013-04-17 12:42:15 +00:00
|
|
|
wg := sync.WaitGroup{}
|
2013-05-23 19:29:27 +00:00
|
|
|
|
2013-06-05 11:56:56 +00:00
|
|
|
m.Lock()
|
2015-03-30 17:43:19 +00:00
|
|
|
rulesSnapshot := make([]Rule, len(m.rules))
|
2014-05-28 17:44:54 +00:00
|
|
|
copy(rulesSnapshot, m.rules)
|
2013-06-05 11:56:56 +00:00
|
|
|
m.Unlock()
|
|
|
|
|
2014-05-28 17:44:54 +00:00
|
|
|
for _, rule := range rulesSnapshot {
|
2013-04-17 12:42:15 +00:00
|
|
|
wg.Add(1)
|
2013-05-23 19:29:27 +00:00
|
|
|
// BUG(julius): Look at fixing thundering herd.
|
2015-03-30 17:43:19 +00:00
|
|
|
go func(rule Rule) {
|
2013-05-23 19:29:27 +00:00
|
|
|
defer wg.Done()
|
2013-12-10 15:54:35 +00:00
|
|
|
|
|
|
|
start := time.Now()
|
2015-05-25 19:16:32 +00:00
|
|
|
vector, err := rule.eval(now, m.queryEngine)
|
2013-12-10 15:54:35 +00:00
|
|
|
duration := time.Since(start)
|
|
|
|
|
2014-12-31 12:16:08 +00:00
|
|
|
if err != nil {
|
|
|
|
evalFailures.Inc()
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Warnf("Error while evaluating rule %q: %s", rule, err)
|
2015-03-15 02:36:15 +00:00
|
|
|
return
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
2013-07-30 15:18:07 +00:00
|
|
|
|
2013-12-10 15:54:35 +00:00
|
|
|
switch r := rule.(type) {
|
2015-03-30 17:43:19 +00:00
|
|
|
case *AlertingRule:
|
2014-05-28 17:44:54 +00:00
|
|
|
m.queueAlertNotifications(r, now)
|
2015-05-25 19:16:32 +00:00
|
|
|
evalDuration.WithLabelValues(ruleTypeAlerting).Observe(
|
2014-06-18 17:43:15 +00:00
|
|
|
float64(duration / time.Millisecond),
|
|
|
|
)
|
2015-03-30 17:43:19 +00:00
|
|
|
case *RecordingRule:
|
2015-05-25 19:16:32 +00:00
|
|
|
evalDuration.WithLabelValues(ruleTypeRecording).Observe(
|
2014-06-18 17:43:15 +00:00
|
|
|
float64(duration / time.Millisecond),
|
|
|
|
)
|
2013-12-10 15:54:35 +00:00
|
|
|
default:
|
2015-08-24 13:07:27 +00:00
|
|
|
panic(fmt.Errorf("unknown rule type: %T", rule))
|
2013-07-30 15:18:07 +00:00
|
|
|
}
|
2015-03-15 02:36:15 +00:00
|
|
|
|
|
|
|
for _, s := range vector {
|
2015-08-24 16:04:41 +00:00
|
|
|
m.sampleAppender.Append(s)
|
2015-03-15 02:36:15 +00:00
|
|
|
}
|
2013-04-17 12:42:15 +00:00
|
|
|
}(rule)
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
2013-04-17 12:42:15 +00:00
|
|
|
wg.Wait()
|
2013-01-07 22:24:26 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 09:51:05 +00:00
|
|
|
// transferAlertState makes a copy of the state of alerting rules and returns a function
|
|
|
|
// that restores them in the current state.
|
|
|
|
func (m *Manager) transferAlertState() func() {
|
|
|
|
|
|
|
|
alertingRules := map[string]*AlertingRule{}
|
|
|
|
for _, r := range m.rules {
|
|
|
|
if ar, ok := r.(*AlertingRule); ok {
|
|
|
|
alertingRules[ar.name] = ar
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
// Restore alerting rule state.
|
|
|
|
for _, r := range m.rules {
|
|
|
|
ar, ok := r.(*AlertingRule)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if old, ok := alertingRules[ar.name]; ok {
|
|
|
|
ar.activeAlerts = old.activeAlerts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 14:52:56 +00:00
|
|
|
// ApplyConfig updates the rule manager's state as the config requires. If
|
2015-06-02 16:44:41 +00:00
|
|
|
// loading the new rules failed the old rule set is restored. Returns true on success.
|
|
|
|
func (m *Manager) ApplyConfig(conf *config.Config) bool {
|
2015-03-30 17:43:19 +00:00
|
|
|
m.Lock()
|
2015-04-29 09:08:56 +00:00
|
|
|
defer m.Unlock()
|
2015-03-30 17:43:19 +00:00
|
|
|
|
2015-06-30 09:51:05 +00:00
|
|
|
defer m.transferAlertState()()
|
|
|
|
|
2015-06-02 16:44:41 +00:00
|
|
|
success := true
|
2015-05-12 14:52:56 +00:00
|
|
|
m.interval = time.Duration(conf.GlobalConfig.EvaluationInterval)
|
|
|
|
|
|
|
|
rulesSnapshot := make([]Rule, len(m.rules))
|
|
|
|
copy(rulesSnapshot, m.rules)
|
|
|
|
m.rules = m.rules[:0]
|
|
|
|
|
2015-05-28 05:36:21 +00:00
|
|
|
var files []string
|
|
|
|
for _, pat := range conf.RuleFiles {
|
|
|
|
fs, err := filepath.Glob(pat)
|
|
|
|
if err != nil {
|
|
|
|
// The only error can be a bad pattern.
|
|
|
|
log.Errorf("Error retrieving rule files for %s: %s", pat, err)
|
2015-06-02 16:44:41 +00:00
|
|
|
success = false
|
2015-05-28 05:36:21 +00:00
|
|
|
}
|
|
|
|
files = append(files, fs...)
|
|
|
|
}
|
|
|
|
if err := m.loadRuleFiles(files...); err != nil {
|
2015-05-12 14:52:56 +00:00
|
|
|
// If loading the new rules failed, restore the old rule set.
|
|
|
|
m.rules = rulesSnapshot
|
2015-05-20 16:10:29 +00:00
|
|
|
log.Errorf("Error loading rules, previous rule set restored: %s", err)
|
2015-06-02 16:44:41 +00:00
|
|
|
success = false
|
2015-05-12 14:52:56 +00:00
|
|
|
}
|
2015-06-23 10:07:53 +00:00
|
|
|
|
2015-06-02 16:44:41 +00:00
|
|
|
return success
|
2015-05-12 14:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// loadRuleFiles loads alerting and recording rules from the given files.
|
|
|
|
func (m *Manager) loadRuleFiles(filenames ...string) error {
|
2015-04-29 09:08:56 +00:00
|
|
|
for _, fn := range filenames {
|
|
|
|
content, err := ioutil.ReadFile(fn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stmts, err := promql.ParseStmts(string(content))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing %s: %s", fn, err)
|
|
|
|
}
|
2015-07-03 12:48:22 +00:00
|
|
|
|
2015-04-29 09:08:56 +00:00
|
|
|
for _, stmt := range stmts {
|
|
|
|
switch r := stmt.(type) {
|
|
|
|
case *promql.AlertStmt:
|
2015-06-24 17:27:09 +00:00
|
|
|
rule := NewAlertingRule(r.Name, r.Expr, r.Duration, r.Labels, r.Summary, r.Description, r.Runbook)
|
2015-04-29 09:08:56 +00:00
|
|
|
m.rules = append(m.rules, rule)
|
|
|
|
case *promql.RecordStmt:
|
2015-05-25 19:16:32 +00:00
|
|
|
rule := NewRecordingRule(r.Name, r.Expr, r.Labels)
|
2015-04-29 09:08:56 +00:00
|
|
|
m.rules = append(m.rules, rule)
|
|
|
|
default:
|
|
|
|
panic("retrieval.Manager.LoadRuleFiles: unknown statement type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-07 22:24:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
2013-06-11 09:00:55 +00:00
|
|
|
|
2015-04-29 09:08:56 +00:00
|
|
|
// Rules returns the list of the manager's rules.
|
2015-04-29 08:26:49 +00:00
|
|
|
func (m *Manager) Rules() []Rule {
|
2013-06-11 09:00:55 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2015-03-30 17:43:19 +00:00
|
|
|
rules := make([]Rule, len(m.rules))
|
2013-06-11 09:00:55 +00:00
|
|
|
copy(rules, m.rules)
|
|
|
|
return rules
|
|
|
|
}
|
2013-06-13 14:10:05 +00:00
|
|
|
|
2015-04-29 09:08:56 +00:00
|
|
|
// AlertingRules returns the list of the manager's alerting rules.
|
2015-04-29 08:26:49 +00:00
|
|
|
func (m *Manager) AlertingRules() []*AlertingRule {
|
2013-06-13 14:10:05 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2015-03-30 17:43:19 +00:00
|
|
|
alerts := []*AlertingRule{}
|
2013-06-13 14:10:05 +00:00
|
|
|
for _, rule := range m.rules {
|
2015-03-30 17:43:19 +00:00
|
|
|
if alertingRule, ok := rule.(*AlertingRule); ok {
|
2013-06-13 14:10:05 +00:00
|
|
|
alerts = append(alerts, alertingRule)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return alerts
|
|
|
|
}
|