2013-01-07 14:24:26 -08:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// 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.
|
|
|
|
|
2014-05-28 10:44:54 -07:00
|
|
|
package manager
|
2013-01-07 14:24:26 -08:00
|
|
|
|
|
|
|
import (
|
2013-12-10 07:54:35 -08:00
|
|
|
"fmt"
|
2013-04-17 05:42:15 -07:00
|
|
|
"sync"
|
2013-01-07 14:24:26 -08:00
|
|
|
"time"
|
|
|
|
|
2013-08-12 08:18:02 -07:00
|
|
|
"github.com/golang/glog"
|
2014-06-18 10:43:15 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2013-08-12 08:18:02 -07:00
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/config"
|
2013-08-09 10:32:55 -07:00
|
|
|
"github.com/prometheus/prometheus/notification"
|
2014-05-28 10:44:54 -07:00
|
|
|
"github.com/prometheus/prometheus/rules"
|
2014-06-06 02:55:53 -07:00
|
|
|
"github.com/prometheus/prometheus/storage/local"
|
2014-05-28 10:44:54 -07:00
|
|
|
"github.com/prometheus/prometheus/templates"
|
2013-06-25 05:02:27 -07:00
|
|
|
)
|
2013-01-07 14:24:26 -08:00
|
|
|
|
2014-06-18 10:43:15 -07:00
|
|
|
// Constants for instrumentation.
|
|
|
|
const (
|
2014-07-23 10:55:33 -07:00
|
|
|
namespace = "prometheus"
|
|
|
|
|
2014-06-18 10:43:15 -07:00
|
|
|
ruleTypeLabel = "rule_type"
|
|
|
|
alertingRuleType = "alerting"
|
|
|
|
recordingRuleType = "recording"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
evalDuration = prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
2014-07-23 10:55:33 -07:00
|
|
|
Namespace: namespace,
|
|
|
|
Name: "rule_evaluation_duration_milliseconds",
|
|
|
|
Help: "The duration for a rule to execute.",
|
2014-06-18 10:43:15 -07:00
|
|
|
},
|
|
|
|
[]string{ruleTypeLabel},
|
|
|
|
)
|
2014-12-31 04:16:08 -08:00
|
|
|
evalFailures = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Name: "rule_evaluation_failures_total",
|
|
|
|
Help: "The total number of rule evaluation failures.",
|
|
|
|
},
|
|
|
|
)
|
2014-07-23 10:55:33 -07:00
|
|
|
iterationDuration = prometheus.NewSummary(prometheus.SummaryOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Name: "evaluator_duration_milliseconds",
|
|
|
|
Help: "The duration for all evaluations to execute.",
|
2015-01-21 06:42:25 -08: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 10:55:33 -07:00
|
|
|
})
|
2014-06-18 10:43:15 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(iterationDuration)
|
2014-12-31 04:16:08 -08:00
|
|
|
prometheus.MustRegister(evalFailures)
|
2014-06-18 10:43:15 -07:00
|
|
|
prometheus.MustRegister(evalDuration)
|
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// A RuleManager manages recording and alerting rules. Create instances with
|
|
|
|
// NewRuleManager.
|
2013-01-07 14:24:26 -08:00
|
|
|
type RuleManager interface {
|
2013-06-13 07:10:05 -07:00
|
|
|
// Load and add rules from rule files specified in the configuration.
|
2013-04-30 11:20:14 -07:00
|
|
|
AddRulesFromConfig(config config.Config) error
|
2013-06-13 07:10:05 -07:00
|
|
|
// Start the rule manager's periodic rule evaluation.
|
2013-06-05 04:56:56 -07:00
|
|
|
Run()
|
2013-06-13 07:10:05 -07:00
|
|
|
// Stop the rule manager's rule evaluation cycles.
|
|
|
|
Stop()
|
|
|
|
// Return all rules.
|
2014-05-28 10:44:54 -07:00
|
|
|
Rules() []rules.Rule
|
2013-06-13 07:10:05 -07:00
|
|
|
// Return all alerting rules.
|
2014-05-28 10:44:54 -07:00
|
|
|
AlertingRules() []*rules.AlertingRule
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type ruleManager struct {
|
2013-06-05 04:56:56 -07:00
|
|
|
// Protects the rules list.
|
|
|
|
sync.Mutex
|
2014-05-28 10:44:54 -07:00
|
|
|
rules []rules.Rule
|
2013-06-05 04:56:56 -07:00
|
|
|
|
2013-08-20 06:42:06 -07:00
|
|
|
done chan bool
|
|
|
|
|
|
|
|
interval time.Duration
|
2014-09-16 06:47:24 -07:00
|
|
|
storage local.Storage
|
2013-08-20 06:42:06 -07:00
|
|
|
|
2014-12-31 05:01:19 -08:00
|
|
|
results chan<- clientmodel.Samples
|
2014-10-10 05:19:02 -07:00
|
|
|
notificationHandler *notification.NotificationHandler
|
2013-08-20 06:42:06 -07:00
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
prometheusURL string
|
2013-08-20 06:42:06 -07:00
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// RuleManagerOptions bundles options for the RuleManager.
|
2013-08-20 06:42:06 -07:00
|
|
|
type RuleManagerOptions struct {
|
|
|
|
EvaluationInterval time.Duration
|
2014-09-16 06:47:24 -07:00
|
|
|
Storage local.Storage
|
2013-08-20 06:42:06 -07:00
|
|
|
|
2014-10-10 05:19:02 -07:00
|
|
|
NotificationHandler *notification.NotificationHandler
|
2014-12-31 05:01:19 -08:00
|
|
|
Results chan<- clientmodel.Samples
|
2013-08-20 06:42:06 -07:00
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
PrometheusURL string
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
2014-12-10 07:16:49 -08:00
|
|
|
// NewRuleManager returns an implementation of RuleManager, ready to be started
|
|
|
|
// by calling the Run method.
|
2013-08-20 06:42:06 -07:00
|
|
|
func NewRuleManager(o *RuleManagerOptions) RuleManager {
|
2013-01-07 14:24:26 -08:00
|
|
|
manager := &ruleManager{
|
2014-05-28 10:44:54 -07:00
|
|
|
rules: []rules.Rule{},
|
2013-08-20 06:42:06 -07:00
|
|
|
done: make(chan bool),
|
|
|
|
|
2014-10-10 05:19:02 -07:00
|
|
|
interval: o.EvaluationInterval,
|
|
|
|
storage: o.Storage,
|
|
|
|
results: o.Results,
|
|
|
|
notificationHandler: o.NotificationHandler,
|
2014-12-10 07:16:49 -08:00
|
|
|
prometheusURL: o.PrometheusURL,
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
return manager
|
|
|
|
}
|
|
|
|
|
2013-06-05 04:56:56 -07:00
|
|
|
func (m *ruleManager) Run() {
|
2013-05-23 12:29:27 -07:00
|
|
|
ticker := time.NewTicker(m.interval)
|
|
|
|
defer ticker.Stop()
|
2013-01-07 14:24:26 -08:00
|
|
|
|
|
|
|
for {
|
2014-12-10 07:16:49 -08:00
|
|
|
// TODO(beorn): This has the same problem as the scraper had
|
|
|
|
// before. If rule evaluation takes longer than the interval,
|
|
|
|
// there is a 50% chance per iteration that - after stopping the
|
|
|
|
// ruleManager - a new evaluation will be started rather than
|
|
|
|
// the ruleManager actually stopped. We need a similar
|
|
|
|
// contraption here as in the scraper.
|
2013-01-07 14:24:26 -08:00
|
|
|
select {
|
2013-05-23 12:29:27 -07:00
|
|
|
case <-ticker.C:
|
|
|
|
start := time.Now()
|
2013-06-05 04:56:56 -07:00
|
|
|
m.runIteration(m.results)
|
2014-07-23 10:55:33 -07:00
|
|
|
iterationDuration.Observe(float64(time.Since(start) / time.Millisecond))
|
2013-01-07 14:24:26 -08:00
|
|
|
case <-m.done:
|
2014-11-11 15:38:28 -08:00
|
|
|
glog.Info("Rule manager stopped.")
|
2013-12-11 06:30:27 -08:00
|
|
|
return
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *ruleManager) Stop() {
|
2014-11-11 15:38:28 -08:00
|
|
|
glog.Info("Stopping rule manager...")
|
2014-06-06 02:55:53 -07:00
|
|
|
m.done <- true
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
2014-05-28 10:44:54 -07:00
|
|
|
func (m *ruleManager) queueAlertNotifications(rule *rules.AlertingRule, timestamp clientmodel.Timestamp) {
|
2013-07-30 08:18:07 -07:00
|
|
|
activeAlerts := rule.ActiveAlerts()
|
|
|
|
if len(activeAlerts) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-09 10:32:55 -07:00
|
|
|
notifications := make(notification.NotificationReqs, 0, len(activeAlerts))
|
2013-07-30 08:18:07 -07:00
|
|
|
for _, aa := range activeAlerts {
|
2014-12-10 07:16:49 -08:00
|
|
|
if aa.State != rules.Firing {
|
2013-07-30 08:18:07 -07:00
|
|
|
// BUG: In the future, make AlertManager support pending alerts?
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-05-28 10:44:54 -07: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
|
|
|
|
Value clientmodel.SampleValue
|
|
|
|
}{
|
|
|
|
Labels: l,
|
|
|
|
Value: aa.Value,
|
|
|
|
}
|
|
|
|
// 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 {
|
2014-06-10 07:30:06 -07:00
|
|
|
template := templates.NewTemplateExpander(defs+text, "__alert_"+rule.Name(), tmplData, timestamp, m.storage)
|
|
|
|
result, err := template.Expand()
|
2014-05-28 10:44:54 -07:00
|
|
|
if err != nil {
|
|
|
|
result = err.Error()
|
|
|
|
glog.Warningf("Error expanding alert template %v with data '%v': %v", rule.Name(), tmplData, err)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2013-08-09 10:32:55 -07:00
|
|
|
notifications = append(notifications, ¬ification.NotificationReq{
|
2014-05-28 10:44:54 -07:00
|
|
|
Summary: expand(rule.Summary),
|
|
|
|
Description: expand(rule.Description),
|
2013-08-09 10:32:55 -07:00
|
|
|
Labels: aa.Labels.Merge(clientmodel.LabelSet{
|
2014-05-28 10:44:54 -07:00
|
|
|
rules.AlertNameLabel: clientmodel.LabelValue(rule.Name()),
|
2013-08-09 10:32:55 -07:00
|
|
|
}),
|
2013-08-20 06:42:06 -07: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 06:35:02 -07:00
|
|
|
ActiveSince: aa.ActiveSince.Time(),
|
2013-08-20 06:42:06 -07:00
|
|
|
RuleString: rule.String(),
|
2014-12-10 07:16:49 -08:00
|
|
|
GeneratorURL: m.prometheusURL + rules.GraphLinkForExpression(rule.Vector.String()),
|
2013-07-30 08:18:07 -07:00
|
|
|
})
|
|
|
|
}
|
2014-10-10 05:19:02 -07:00
|
|
|
m.notificationHandler.SubmitReqs(notifications)
|
2013-07-30 08:18:07 -07:00
|
|
|
}
|
|
|
|
|
2014-12-31 05:01:19 -08:00
|
|
|
func (m *ruleManager) runIteration(results chan<- clientmodel.Samples) {
|
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 06:35:02 -07:00
|
|
|
now := clientmodel.Now()
|
2013-04-17 05:42:15 -07:00
|
|
|
wg := sync.WaitGroup{}
|
2013-05-23 12:29:27 -07:00
|
|
|
|
2013-06-05 04:56:56 -07:00
|
|
|
m.Lock()
|
2014-05-28 10:44:54 -07:00
|
|
|
rulesSnapshot := make([]rules.Rule, len(m.rules))
|
|
|
|
copy(rulesSnapshot, m.rules)
|
2013-06-05 04:56:56 -07:00
|
|
|
m.Unlock()
|
|
|
|
|
2014-05-28 10:44:54 -07:00
|
|
|
for _, rule := range rulesSnapshot {
|
2013-04-17 05:42:15 -07:00
|
|
|
wg.Add(1)
|
2013-05-23 12:29:27 -07:00
|
|
|
// BUG(julius): Look at fixing thundering herd.
|
2014-05-28 10:44:54 -07:00
|
|
|
go func(rule rules.Rule) {
|
2013-05-23 12:29:27 -07:00
|
|
|
defer wg.Done()
|
2013-12-10 07:54:35 -08:00
|
|
|
|
|
|
|
start := time.Now()
|
2013-05-07 04:15:10 -07:00
|
|
|
vector, err := rule.Eval(now, m.storage)
|
2013-12-10 07:54:35 -08:00
|
|
|
duration := time.Since(start)
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
samples := make(clientmodel.Samples, len(vector))
|
2014-12-08 07:55:49 -08:00
|
|
|
for i, s := range vector {
|
|
|
|
samples[i] = &clientmodel.Sample{
|
|
|
|
Metric: s.Metric.Metric,
|
|
|
|
Value: s.Value,
|
|
|
|
Timestamp: s.Timestamp,
|
|
|
|
}
|
|
|
|
}
|
2014-12-31 04:16:08 -08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
evalFailures.Inc()
|
2015-01-08 07:57:25 -08:00
|
|
|
glog.Warningf("Error while evaluating rule %q: %s", rule, err)
|
2014-12-31 05:01:19 -08:00
|
|
|
} else {
|
|
|
|
m.results <- samples
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2013-07-30 08:18:07 -07:00
|
|
|
|
2013-12-10 07:54:35 -08:00
|
|
|
switch r := rule.(type) {
|
2014-05-28 10:44:54 -07:00
|
|
|
case *rules.AlertingRule:
|
|
|
|
m.queueAlertNotifications(r, now)
|
2014-06-18 10:43:15 -07:00
|
|
|
evalDuration.WithLabelValues(alertingRuleType).Observe(
|
|
|
|
float64(duration / time.Millisecond),
|
|
|
|
)
|
2014-05-28 10:44:54 -07:00
|
|
|
case *rules.RecordingRule:
|
2014-06-18 10:43:15 -07:00
|
|
|
evalDuration.WithLabelValues(recordingRuleType).Observe(
|
|
|
|
float64(duration / time.Millisecond),
|
|
|
|
)
|
2013-12-10 07:54:35 -08:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown rule type: %T", rule))
|
2013-07-30 08:18:07 -07:00
|
|
|
}
|
2013-04-17 05:42:15 -07:00
|
|
|
}(rule)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2013-05-23 12:29:27 -07:00
|
|
|
|
2013-04-17 05:42:15 -07:00
|
|
|
wg.Wait()
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
|
2013-04-30 11:20:14 -07:00
|
|
|
func (m *ruleManager) AddRulesFromConfig(config config.Config) error {
|
|
|
|
for _, ruleFile := range config.Global.RuleFile {
|
2014-05-28 10:44:54 -07:00
|
|
|
newRules, err := rules.LoadRulesFromFile(ruleFile)
|
2013-01-07 14:24:26 -08:00
|
|
|
if err != nil {
|
2013-12-09 08:01:51 -08:00
|
|
|
return fmt.Errorf("%s: %s", ruleFile, err)
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
2013-06-05 04:56:56 -07:00
|
|
|
m.Lock()
|
2013-01-07 14:24:26 -08:00
|
|
|
m.rules = append(m.rules, newRules...)
|
2013-06-05 04:56:56 -07:00
|
|
|
m.Unlock()
|
2013-01-07 14:24:26 -08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2013-06-11 02:00:55 -07:00
|
|
|
|
2014-05-28 10:44:54 -07:00
|
|
|
func (m *ruleManager) Rules() []rules.Rule {
|
2013-06-11 02:00:55 -07:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2014-05-28 10:44:54 -07:00
|
|
|
rules := make([]rules.Rule, len(m.rules))
|
2013-06-11 02:00:55 -07:00
|
|
|
copy(rules, m.rules)
|
|
|
|
return rules
|
|
|
|
}
|
2013-06-13 07:10:05 -07:00
|
|
|
|
2014-05-28 10:44:54 -07:00
|
|
|
func (m *ruleManager) AlertingRules() []*rules.AlertingRule {
|
2013-06-13 07:10:05 -07:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2014-05-28 10:44:54 -07:00
|
|
|
alerts := []*rules.AlertingRule{}
|
2013-06-13 07:10:05 -07:00
|
|
|
for _, rule := range m.rules {
|
2014-05-28 10:44:54 -07:00
|
|
|
if alertingRule, ok := rule.(*rules.AlertingRule); ok {
|
2013-06-13 07:10:05 -07:00
|
|
|
alerts = append(alerts, alertingRule)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return alerts
|
|
|
|
}
|