-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathmanager.go
427 lines (375 loc) · 12.7 KB
/
manager.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
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package rules
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/rulefmt"
"github.com/prometheus/prometheus/rules"
"gopkg.in/yaml.v3"
"github.com/thanos-io/thanos/pkg/errutil"
"github.com/thanos-io/thanos/pkg/extprom"
"github.com/thanos-io/thanos/pkg/rules/rulespb"
"github.com/thanos-io/thanos/pkg/store/labelpb"
"github.com/thanos-io/thanos/pkg/store/storepb"
)
const tmpRuleDir = ".tmp-rules"
type Group struct {
*rules.Group
OriginalFile string
PartialResponseStrategy storepb.PartialResponseStrategy
}
func (g Group) toProto() *rulespb.RuleGroup {
ret := &rulespb.RuleGroup{
Name: g.Name(),
File: g.OriginalFile,
Interval: g.Interval().Seconds(),
PartialResponseStrategy: g.PartialResponseStrategy,
// UTC needed due to https://github.com/gogo/protobuf/issues/519.
LastEvaluation: g.GetLastEvaluation().UTC(),
EvaluationDurationSeconds: g.GetEvaluationTime().Seconds(),
}
for _, r := range g.Rules() {
lastError := ""
if r.LastError() != nil {
lastError = r.LastError().Error()
}
switch rule := r.(type) {
case *rules.AlertingRule:
ret.Rules = append(ret.Rules, &rulespb.Rule{
Result: &rulespb.Rule_Alert{Alert: &rulespb.Alert{
State: rulespb.AlertState(rule.State()),
Name: rule.Name(),
Query: rule.Query().String(),
DurationSeconds: rule.HoldDuration().Seconds(),
Labels: labelpb.ZLabelSet{Labels: labelpb.ZLabelsFromPromLabels(rule.Labels())},
Annotations: labelpb.ZLabelSet{Labels: labelpb.ZLabelsFromPromLabels(rule.Annotations())},
Alerts: ActiveAlertsToProto(g.PartialResponseStrategy, rule),
Health: string(rule.Health()),
LastError: lastError,
EvaluationDurationSeconds: rule.GetEvaluationDuration().Seconds(),
// UTC needed due to https://github.com/gogo/protobuf/issues/519.
LastEvaluation: rule.GetEvaluationTimestamp().UTC(),
}}})
case *rules.RecordingRule:
ret.Rules = append(ret.Rules, &rulespb.Rule{
Result: &rulespb.Rule_Recording{Recording: &rulespb.RecordingRule{
Name: rule.Name(),
Query: rule.Query().String(),
Labels: labelpb.ZLabelSet{Labels: labelpb.ZLabelsFromPromLabels(rule.Labels())},
Health: string(rule.Health()),
LastError: lastError,
EvaluationDurationSeconds: rule.GetEvaluationDuration().Seconds(),
// UTC needed due to https://github.com/gogo/protobuf/issues/519.
LastEvaluation: rule.GetEvaluationTimestamp().UTC(),
}}})
default:
// We cannot do much, let's panic, API will recover.
panic(fmt.Sprintf("rule %q: unsupported type %T", r.Name(), rule))
}
}
return ret
}
func ActiveAlertsToProto(s storepb.PartialResponseStrategy, a *rules.AlertingRule) []*rulespb.AlertInstance {
active := a.ActiveAlerts()
ret := make([]*rulespb.AlertInstance, len(active))
for i, ruleAlert := range active {
ret[i] = &rulespb.AlertInstance{
PartialResponseStrategy: s,
Labels: labelpb.ZLabelSet{Labels: labelpb.ZLabelsFromPromLabels(ruleAlert.Labels)},
Annotations: labelpb.ZLabelSet{Labels: labelpb.ZLabelsFromPromLabels(ruleAlert.Annotations)},
State: rulespb.AlertState(ruleAlert.State),
ActiveAt: &ruleAlert.ActiveAt, //nolint:exportloopref
Value: strconv.FormatFloat(ruleAlert.Value, 'e', -1, 64),
}
}
return ret
}
// Manager is a partial response strategy and proto compatible Manager.
// Manager also implements rulespb.Rules gRPC service.
type Manager struct {
workDir string
mgrs map[storepb.PartialResponseStrategy]*rules.Manager
extLset labels.Labels
mtx sync.RWMutex
ruleFiles map[string]string
}
// NewManager creates new Manager.
// QueryFunc from baseOpts will be rewritten.
func NewManager(
ctx context.Context,
reg prometheus.Registerer,
dataDir string,
baseOpts rules.ManagerOptions,
queryFuncCreator func(partialResponseStrategy storepb.PartialResponseStrategy) rules.QueryFunc,
extLset labels.Labels,
) *Manager {
m := &Manager{
workDir: filepath.Join(dataDir, tmpRuleDir),
mgrs: make(map[storepb.PartialResponseStrategy]*rules.Manager),
extLset: extLset,
ruleFiles: make(map[string]string),
}
for _, strategy := range storepb.PartialResponseStrategy_value {
s := storepb.PartialResponseStrategy(strategy)
opts := baseOpts
opts.Registerer = extprom.WrapRegistererWith(prometheus.Labels{"strategy": strings.ToLower(s.String())}, reg)
opts.Context = ctx
opts.QueryFunc = queryFuncCreator(s)
m.mgrs[s] = rules.NewManager(&opts)
}
return m
}
// Run is non blocking, in opposite to TSDB manager, which is blocking.
func (m *Manager) Run() {
for _, mgr := range m.mgrs {
go mgr.Run()
}
}
func (m *Manager) Stop() {
for _, mgr := range m.mgrs {
mgr.Stop()
}
}
func (m *Manager) protoRuleGroups() []*rulespb.RuleGroup {
rg := m.RuleGroups()
res := make([]*rulespb.RuleGroup, 0, len(rg))
for _, g := range rg {
res = append(res, g.toProto())
}
return res
}
func (m *Manager) RuleGroups() []Group {
m.mtx.RLock()
defer m.mtx.RUnlock()
var res []Group
for s, r := range m.mgrs {
for _, group := range r.RuleGroups() {
res = append(res, Group{
Group: group,
OriginalFile: m.ruleFiles[group.File()],
PartialResponseStrategy: s,
})
}
}
return res
}
func (m *Manager) Active() []*rulespb.AlertInstance {
var res []*rulespb.AlertInstance
for s, r := range m.mgrs {
for _, r := range r.AlertingRules() {
res = append(res, ActiveAlertsToProto(s, r)...)
}
}
return res
}
type configRuleAdapter struct {
PartialResponseStrategy *storepb.PartialResponseStrategy
group rulefmt.RuleGroup
nativeRuleGroup map[string]interface{}
}
func (g *configRuleAdapter) UnmarshalYAML(unmarshal func(interface{}) error) error {
rs := struct {
RuleGroup rulefmt.RuleGroup `yaml:",inline"`
Strategy string `yaml:"partial_response_strategy"`
}{}
if err := unmarshal(&rs); err != nil {
return err
}
g.PartialResponseStrategy = new(storepb.PartialResponseStrategy)
// Same as YAMl. Quote as JSON unmarshal expects raw JSON field.
if err := g.PartialResponseStrategy.UnmarshalJSON([]byte("\"" + rs.Strategy + "\"")); err != nil {
return err
}
g.group = rs.RuleGroup
var native map[string]interface{}
if err := unmarshal(&native); err != nil {
return errors.Wrap(err, "failed to unmarshal rulefmt.configRuleAdapter")
}
delete(native, "partial_response_strategy")
g.nativeRuleGroup = native
return nil
}
func (g configRuleAdapter) MarshalYAML() (interface{}, error) {
return struct {
RuleGroup map[string]interface{} `yaml:",inline"`
}{
RuleGroup: g.nativeRuleGroup,
}, nil
}
// TODO(bwplotka): Replace this with upstream implementation after https://github.com/prometheus/prometheus/issues/7128 is fixed.
func (g configRuleAdapter) validate() (errs []error) {
set := map[string]struct{}{}
if g.group.Name == "" {
errs = append(errs, errors.New("Groupname should not be empty"))
}
if _, ok := set[g.group.Name]; ok {
errs = append(
errs,
fmt.Errorf("groupname: %q is repeated in the same file", g.group.Name),
)
}
set[g.group.Name] = struct{}{}
for i, r := range g.group.Rules {
for _, node := range r.Validate() {
var ruleName string
if r.Alert.Value != "" {
ruleName = r.Alert.Value
} else {
ruleName = r.Record.Value
}
errs = append(errs, &rulefmt.Error{
Group: g.group.Name,
Rule: i,
RuleName: ruleName,
Err: node,
})
}
}
return errs
}
// ValidateAndCount validates all rules in the rule groups and return overal number of rules in all groups.
// TODO(bwplotka): Replace this with upstream implementation after https://github.com/prometheus/prometheus/issues/7128 is fixed.
func ValidateAndCount(group io.Reader) (numRules int, errs errutil.MultiError) {
var rgs configGroups
d := yaml.NewDecoder(group)
d.KnownFields(true)
if err := d.Decode(&rgs); err != nil {
errs.Add(err)
return 0, errs
}
for _, g := range rgs.Groups {
if err := g.validate(); err != nil {
for _, e := range err {
errs.Add(e)
}
return 0, errs
}
}
for _, rg := range rgs.Groups {
numRules += len(rg.group.Rules)
}
return numRules, errs
}
type configGroups struct {
Groups []configRuleAdapter `yaml:"groups"`
}
// Update updates rules from given files to all managers we hold. We decide which groups should go where, based on
// special field in configGroups.configRuleAdapter struct.
func (m *Manager) Update(evalInterval time.Duration, files []string) error {
var (
errs errutil.MultiError
filesByStrategy = map[storepb.PartialResponseStrategy][]string{}
ruleFiles = map[string]string{}
)
// Initialize filesByStrategy for existing managers' strategies to make
// sure that managers are updated when they have no rules configured.
for strategy := range m.mgrs {
filesByStrategy[strategy] = make([]string, 0)
}
if err := os.RemoveAll(m.workDir); err != nil {
return errors.Wrapf(err, "remove %s", m.workDir)
}
if err := os.MkdirAll(m.workDir, os.ModePerm); err != nil {
return errors.Wrapf(err, "create %s", m.workDir)
}
for _, fn := range files {
b, err := ioutil.ReadFile(fn)
if err != nil {
errs.Add(err)
continue
}
var rg configGroups
if err := yaml.Unmarshal(b, &rg); err != nil {
errs.Add(errors.Wrap(err, fn))
continue
}
// NOTE: This is very ugly, but we need to write those yaml into tmp dir without the partial partial response field
// which is not supported, to be able to reuse rules.Manager. The problem is that it uses yaml.UnmarshalStrict.
groupsByStrategy := map[storepb.PartialResponseStrategy][]configRuleAdapter{}
for _, rg := range rg.Groups {
groupsByStrategy[*rg.PartialResponseStrategy] = append(groupsByStrategy[*rg.PartialResponseStrategy], rg)
}
for s, rg := range groupsByStrategy {
b, err := yaml.Marshal(configGroups{Groups: rg})
if err != nil {
errs = append(errs, errors.Wrapf(err, "%s: failed to marshal rule groups", fn))
continue
}
// Use full file name appending to work dir, so we can differentiate between different dirs and same filenames(!).
// This will be also used as key for file group name.
newFn := filepath.Join(m.workDir, s.String(), strings.TrimLeft(fn, m.workDir))
if err := os.MkdirAll(filepath.Dir(newFn), os.ModePerm); err != nil {
errs.Add(errors.Wrapf(err, "create %s", filepath.Dir(newFn)))
continue
}
if err := ioutil.WriteFile(newFn, b, os.ModePerm); err != nil {
errs.Add(errors.Wrapf(err, "write file %v", newFn))
continue
}
filesByStrategy[s] = append(filesByStrategy[s], newFn)
ruleFiles[newFn] = fn
}
}
m.mtx.Lock()
for s, fs := range filesByStrategy {
mgr, ok := m.mgrs[s]
if !ok {
errs.Add(errors.Errorf("no manager found for %v", s))
continue
}
// We add external labels in `pkg/alert.Queue`.
if err := mgr.Update(evalInterval, fs, nil); err != nil {
// TODO(bwplotka): Prometheus logs all error details. Fix it upstream to have consistent error handling.
errs.Add(errors.Wrapf(err, "strategy %s, update rules", s))
continue
}
}
m.ruleFiles = ruleFiles
m.mtx.Unlock()
return errs.Err()
}
// Rules returns specified rules from manager. This is used by gRPC and locally for HTTP and UI purposes.
func (m *Manager) Rules(r *rulespb.RulesRequest, s rulespb.Rules_RulesServer) error {
groups := m.protoRuleGroups()
pgs := make([]*rulespb.RuleGroup, 0, len(groups))
for _, g := range groups {
// https://github.com/gogo/protobuf/issues/519
g.LastEvaluation = g.LastEvaluation.UTC()
if r.Type == rulespb.RulesRequest_ALL {
pgs = append(pgs, g)
continue
}
filtered := proto.Clone(g).(*rulespb.RuleGroup)
filtered.Rules = nil
for _, rule := range g.Rules {
if rule.GetAlert() != nil && r.Type == rulespb.RulesRequest_ALERT {
filtered.Rules = append(filtered.Rules, rule)
continue
}
if rule.GetRecording() != nil && r.Type == rulespb.RulesRequest_RECORD {
filtered.Rules = append(filtered.Rules, rule)
}
}
pgs = append(pgs, filtered)
}
enrichRulesWithExtLabels(pgs, m.extLset)
for _, pg := range pgs {
if err := s.Send(&rulespb.RulesResponse{Result: &rulespb.RulesResponse_Group{Group: pg}}); err != nil {
return err
}
}
return nil
}