forked from cortexproject/cortex
/
ruler.go
491 lines (442 loc) · 14.9 KB
/
ruler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package ruler
import (
native_ctx "context"
"crypto/md5"
"encoding/json"
"flag"
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"time"
gklog "github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
opentracing "github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
config_util "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/discovery"
sd_config "github.com/prometheus/prometheus/discovery/config"
"github.com/prometheus/prometheus/discovery/dns"
"github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/notifier"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/rules"
"github.com/prometheus/prometheus/util/strutil"
"golang.org/x/net/context"
"golang.org/x/net/context/ctxhttp"
"github.com/weaveworks/common/instrument"
"github.com/weaveworks/common/user"
"github.com/weaveworks/cortex/pkg/chunk"
"github.com/weaveworks/cortex/pkg/distributor"
"github.com/weaveworks/cortex/pkg/querier"
"github.com/weaveworks/cortex/pkg/util"
)
var (
evalDuration = instrument.NewHistogramCollectorFromOpts(prometheus.HistogramOpts{
Namespace: "cortex",
Name: "group_evaluation_duration_seconds",
Help: "The duration for a rule group to execute.",
Buckets: []float64{.1, .25, .5, 1, 2.5, 5, 10, 25},
})
rulesProcessed = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "cortex",
Name: "rules_processed_total",
Help: "How many rules have been processed.",
})
blockedWorkers = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "cortex",
Name: "blocked_workers",
Help: "How many workers are waiting on an item to be ready.",
})
workerIdleTime = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "cortex",
Name: "worker_idle_seconds_total",
Help: "How long workers have spent waiting for work.",
})
evalLatency = prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: "cortex",
Name: "group_evaluation_latency_seconds",
Help: "How far behind the target time each rule group executed.",
Buckets: []float64{.1, .25, .5, 1, 2.5, 5, 10, 25},
})
)
func init() {
evalDuration.Register()
prometheus.MustRegister(evalLatency)
prometheus.MustRegister(rulesProcessed)
prometheus.MustRegister(blockedWorkers)
prometheus.MustRegister(workerIdleTime)
}
// Config is the configuration for the recording rules server.
type Config struct {
// This is used for template expansion in alerts; must be a valid URL
ExternalURL util.URLValue
// How frequently to evaluate rules by default.
EvaluationInterval time.Duration
NumWorkers int
// URL of the Alertmanager to send notifications to.
AlertmanagerURL util.URLValue
// Whether to use DNS SRV records to discover alertmanagers.
AlertmanagerDiscovery bool
// How long to wait between refreshing the list of alertmanagers based on
// DNS service discovery.
AlertmanagerRefreshInterval time.Duration
// Capacity of the queue for notifications to be sent to the Alertmanager.
NotificationQueueCapacity int
// HTTP timeout duration when sending notifications to the Alertmanager.
NotificationTimeout time.Duration
// Timeout for rule group evaluation, including sending result to ingester
GroupTimeout time.Duration
}
// RegisterFlags adds the flags required to config this to the given FlagSet
func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
cfg.ExternalURL.URL, _ = url.Parse("") // Must be non-nil
f.Var(&cfg.ExternalURL, "ruler.external.url", "URL of alerts return path.")
f.DurationVar(&cfg.EvaluationInterval, "ruler.evaluation-interval", 15*time.Second, "How frequently to evaluate rules")
f.IntVar(&cfg.NumWorkers, "ruler.num-workers", 1, "Number of rule evaluator worker routines in this process")
f.Var(&cfg.AlertmanagerURL, "ruler.alertmanager-url", "URL of the Alertmanager to send notifications to.")
f.BoolVar(&cfg.AlertmanagerDiscovery, "ruler.alertmanager-discovery", false, "Use DNS SRV records to discover alertmanager hosts.")
f.DurationVar(&cfg.AlertmanagerRefreshInterval, "ruler.alertmanager-refresh-interval", 1*time.Minute, "How long to wait between refreshing alertmanager hosts.")
f.IntVar(&cfg.NotificationQueueCapacity, "ruler.notification-queue-capacity", 10000, "Capacity of the queue for notifications to be sent to the Alertmanager.")
f.DurationVar(&cfg.NotificationTimeout, "ruler.notification-timeout", 10*time.Second, "HTTP timeout duration when sending notifications to the Alertmanager.")
f.DurationVar(&cfg.GroupTimeout, "ruler.group-timeout", 10*time.Second, "Timeout for rule group evaluation, including sending result to ingester")
}
// Ruler evaluates rules.
type Ruler struct {
engine *promql.Engine
pusher Pusher
alertURL *url.URL
notifierCfg *config.Config
queueCapacity int
groupTimeout time.Duration
// Per-user notifiers with separate queues.
notifiersMtx sync.Mutex
notifiers map[string]*rulerNotifier
}
// rulerNotifier bundles a notifier.Manager together with an associated
// Alertmanager service discovery manager and handles the lifecycle
// of both actors.
type rulerNotifier struct {
notifier *notifier.Manager
sdCancel context.CancelFunc
sdManager *discovery.Manager
wg sync.WaitGroup
logger gklog.Logger
}
func newRulerNotifier(o *notifier.Options, l gklog.Logger) *rulerNotifier {
sdCtx, sdCancel := context.WithCancel(context.Background())
return &rulerNotifier{
notifier: notifier.NewManager(o, l),
sdCancel: sdCancel,
sdManager: discovery.NewManager(sdCtx, l),
logger: l,
}
}
func (rn *rulerNotifier) run() {
rn.wg.Add(2)
go func() {
if err := rn.sdManager.Run(); err != nil {
level.Error(rn.logger).Log("msg", "error starting notifier discovery manager", "err", err)
}
rn.wg.Done()
}()
go func() {
rn.notifier.Run(rn.sdManager.SyncCh())
rn.wg.Done()
}()
}
func (rn *rulerNotifier) applyConfig(cfg *config.Config) error {
if err := rn.notifier.ApplyConfig(cfg); err != nil {
return err
}
sdCfgs := make(map[string]sd_config.ServiceDiscoveryConfig)
for _, v := range cfg.AlertingConfig.AlertmanagerConfigs {
// AlertmanagerConfigs doesn't hold an unique identifier so we use the config hash as the identifier.
b, err := json.Marshal(v)
if err != nil {
return err
}
// This hash needs to be identical to the one computed in the notifier in
// https://github.com/prometheus/prometheus/blob/719c579f7b917b384c3d629752dea026513317dc/notifier/notifier.go#L265
// This kind of sucks, but it's done in Prometheus in main.go in the same way.
sdCfgs[fmt.Sprintf("%x", md5.Sum(b))] = v.ServiceDiscoveryConfig
}
return rn.sdManager.ApplyConfig(sdCfgs)
}
func (rn *rulerNotifier) stop() {
rn.sdCancel()
rn.notifier.Stop()
rn.wg.Wait()
}
// NewRuler creates a new ruler from a distributor and chunk store.
func NewRuler(cfg Config, d *distributor.Distributor, c *chunk.Store) (*Ruler, error) {
ncfg, err := buildNotifierConfig(&cfg)
if err != nil {
return nil, err
}
return &Ruler{
engine: querier.NewEngine(d, c),
pusher: d,
alertURL: cfg.ExternalURL.URL,
notifierCfg: ncfg,
queueCapacity: cfg.NotificationQueueCapacity,
notifiers: map[string]*rulerNotifier{},
groupTimeout: cfg.GroupTimeout,
}, nil
}
// Builds a Prometheus config.Config from a ruler.Config with just the required
// options to configure notifications to Alertmanager.
func buildNotifierConfig(rulerConfig *Config) (*config.Config, error) {
if rulerConfig.AlertmanagerURL.URL == nil {
return &config.Config{}, nil
}
u := rulerConfig.AlertmanagerURL
var sdConfig sd_config.ServiceDiscoveryConfig
if rulerConfig.AlertmanagerDiscovery {
if !strings.Contains(u.Host, "_tcp.") {
return nil, fmt.Errorf("When alertmanager-discovery is on, host name must be of the form _portname._tcp.service.fqdn (is %q)", u.Host)
}
dnsSDConfig := dns.SDConfig{
Names: []string{u.Host},
RefreshInterval: model.Duration(rulerConfig.AlertmanagerRefreshInterval),
Type: "SRV",
Port: 0, // Ignored, because of SRV.
}
sdConfig = sd_config.ServiceDiscoveryConfig{
DNSSDConfigs: []*dns.SDConfig{&dnsSDConfig},
}
} else {
sdConfig = sd_config.ServiceDiscoveryConfig{
StaticConfigs: []*targetgroup.Group{
{
Targets: []model.LabelSet{
{
model.AddressLabel: model.LabelValue(u.Host),
},
},
},
},
}
}
amConfig := &config.AlertmanagerConfig{
Scheme: u.Scheme,
PathPrefix: u.Path,
Timeout: rulerConfig.NotificationTimeout,
ServiceDiscoveryConfig: sdConfig,
}
promConfig := &config.Config{
AlertingConfig: config.AlertingConfig{
AlertmanagerConfigs: []*config.AlertmanagerConfig{amConfig},
},
}
if u.User != nil {
amConfig.HTTPClientConfig = config_util.HTTPClientConfig{
BasicAuth: &config_util.BasicAuth{
Username: u.User.Username(),
},
}
if password, isSet := u.User.Password(); isSet {
amConfig.HTTPClientConfig.BasicAuth.Password = config_util.Secret(password)
}
}
return promConfig, nil
}
func (r *Ruler) newGroup(ctx context.Context, userID string, item *workItem) (*rules.Group, error) {
appendable := &appendableAppender{pusher: r.pusher, ctx: ctx}
notifier, err := r.getOrCreateNotifier(userID)
if err != nil {
return nil, err
}
opts := &rules.ManagerOptions{
Appendable: appendable,
QueryFunc: rules.EngineQueryFunc(r.engine),
Context: ctx,
ExternalURL: r.alertURL,
NotifyFunc: sendAlerts(notifier, r.alertURL.String()),
Logger: gklog.NewNopLogger(),
Registerer: prometheus.DefaultRegisterer,
}
delay := 0 * time.Second // Unused, so 0 value is fine.
return rules.NewGroup(item.filename, "none", delay, item.rules, opts), nil
}
// sendAlerts implements a rules.NotifyFunc for a Notifier.
// It filters any non-firing alerts from the input.
//
// Copied from Prometheus's main.go.
func sendAlerts(n *notifier.Manager, externalURL string) rules.NotifyFunc {
return func(ctx native_ctx.Context, expr string, alerts ...*rules.Alert) error {
var res []*notifier.Alert
for _, alert := range alerts {
// Only send actually firing alerts.
if alert.State == rules.StatePending {
continue
}
a := ¬ifier.Alert{
StartsAt: alert.FiredAt,
Labels: alert.Labels,
Annotations: alert.Annotations,
GeneratorURL: externalURL + strutil.TableLinkForExpression(expr),
}
if !alert.ResolvedAt.IsZero() {
a.EndsAt = alert.ResolvedAt
}
res = append(res, a)
}
if len(alerts) > 0 {
n.Send(res...)
}
return nil
}
}
func (r *Ruler) getOrCreateNotifier(userID string) (*notifier.Manager, error) {
r.notifiersMtx.Lock()
defer r.notifiersMtx.Unlock()
n, ok := r.notifiers[userID]
if ok {
return n.notifier, nil
}
n = newRulerNotifier(¬ifier.Options{
QueueCapacity: r.queueCapacity,
Do: func(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
// Note: The passed-in context comes from the Prometheus rule group code
// and does *not* contain the userID. So it needs to be added to the context
// here before using the context to inject the userID into the HTTP request.
ctx = user.InjectOrgID(ctx, userID)
if err := user.InjectOrgIDIntoHTTPRequest(ctx, req); err != nil {
return nil, err
}
return ctxhttp.Do(ctx, client, req)
},
}, util.Logger)
go n.run()
// This should never fail, unless there's a programming mistake.
if err := n.applyConfig(r.notifierCfg); err != nil {
return nil, err
}
// TODO: Remove notifiers for stale users. Right now this is a slow leak.
r.notifiers[userID] = n
return n.notifier, nil
}
// Evaluate a list of rules in the given context.
func (r *Ruler) Evaluate(userID string, item *workItem) {
ctx := user.InjectOrgID(context.Background(), userID)
logger := util.WithContext(ctx, util.Logger)
level.Debug(logger).Log("msg", "evaluating rules...", "num_rules", len(item.rules))
ctx, cancelTimeout := context.WithTimeout(ctx, r.groupTimeout)
instrument.CollectedRequest(ctx, "Evaluate", evalDuration, nil, func(ctx native_ctx.Context) error {
if span := opentracing.SpanFromContext(ctx); span != nil {
span.SetTag("instance", userID)
}
g, err := r.newGroup(ctx, userID, item)
if err != nil {
level.Error(logger).Log("msg", "failed to create rule group", "err", err)
return err
}
g.Eval(ctx, time.Now())
return nil
})
if err := ctx.Err(); err == nil {
cancelTimeout() // release resources
} else {
level.Warn(util.Logger).Log("msg", "context error", "error", err)
}
rulesProcessed.Add(float64(len(item.rules)))
}
// Stop stops the Ruler.
func (r *Ruler) Stop() {
r.notifiersMtx.Lock()
defer r.notifiersMtx.Unlock()
for _, n := range r.notifiers {
n.stop()
}
}
// Server is a rules server.
type Server struct {
scheduler *scheduler
workers []worker
}
// NewServer makes a new rule processing server.
func NewServer(cfg Config, ruler *Ruler, rulesAPI RulesAPI) (*Server, error) {
// TODO: Separate configuration for polling interval.
s := newScheduler(rulesAPI, cfg.EvaluationInterval, cfg.EvaluationInterval)
if cfg.NumWorkers <= 0 {
return nil, fmt.Errorf("must have at least 1 worker, got %d", cfg.NumWorkers)
}
workers := make([]worker, cfg.NumWorkers)
for i := 0; i < cfg.NumWorkers; i++ {
workers[i] = newWorker(&s, ruler)
}
srv := Server{
scheduler: &s,
workers: workers,
}
go srv.run()
return &srv, nil
}
// Run the server.
func (s *Server) run() {
go s.scheduler.Run()
for _, w := range s.workers {
go w.Run()
}
level.Info(util.Logger).Log("msg", "ruler up and running")
}
// Stop the server.
func (s *Server) Stop() {
for _, w := range s.workers {
w.Stop()
}
s.scheduler.Stop()
}
// Worker does a thing until it's told to stop.
type Worker interface {
Run()
Stop()
}
type worker struct {
scheduler *scheduler
ruler *Ruler
done chan struct{}
terminated chan struct{}
}
func newWorker(scheduler *scheduler, ruler *Ruler) worker {
return worker{
scheduler: scheduler,
ruler: ruler,
done: make(chan struct{}),
terminated: make(chan struct{}),
}
}
func (w *worker) Run() {
defer close(w.terminated)
for {
select {
case <-w.done:
return
default:
}
waitStart := time.Now()
blockedWorkers.Inc()
level.Debug(util.Logger).Log("msg", "waiting for next work item")
item := w.scheduler.nextWorkItem()
blockedWorkers.Dec()
waitElapsed := time.Now().Sub(waitStart)
if item == nil {
level.Debug(util.Logger).Log("msg", "queue closed and empty; terminating worker")
return
}
evalLatency.Observe(time.Since(item.scheduled).Seconds())
workerIdleTime.Add(waitElapsed.Seconds())
level.Debug(util.Logger).Log("msg", "processing item", "item", item)
w.ruler.Evaluate(item.userID, item)
w.scheduler.workItemDone(*item)
level.Debug(util.Logger).Log("msg", "item handed back to queue", "item", item)
}
}
func (w *worker) Stop() {
close(w.done)
<-w.terminated
}