forked from LinTechSo/sloth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
199 lines (166 loc) 路 6.01 KB
/
prometheus.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
package generate
import (
"context"
"fmt"
"github.com/prometheus/prometheus/model/rulefmt"
"github.com/jonas27/sloth/internal/alert"
"github.com/jonas27/sloth/internal/info"
"github.com/jonas27/sloth/internal/log"
"github.com/jonas27/sloth/internal/prometheus"
)
// ServiceConfig is the application service configuration.
type ServiceConfig struct {
AlertGenerator AlertGenerator
SLIRecordingRulesGenerator SLIRecordingRulesGenerator
MetaRecordingRulesGenerator MetadataRecordingRulesGenerator
SLOAlertRulesGenerator SLOAlertRulesGenerator
Logger log.Logger
}
func (c *ServiceConfig) defaults() error {
if c.AlertGenerator == nil {
return fmt.Errorf("alert generator is required")
}
if c.SLIRecordingRulesGenerator == nil {
c.SLIRecordingRulesGenerator = prometheus.OptimizedSLIRecordingRulesGenerator
}
if c.MetaRecordingRulesGenerator == nil {
c.MetaRecordingRulesGenerator = prometheus.MetadataRecordingRulesGenerator
}
if c.SLOAlertRulesGenerator == nil {
c.SLOAlertRulesGenerator = prometheus.SLOAlertRulesGenerator
}
if c.Logger == nil {
c.Logger = log.Noop
}
c.Logger = c.Logger.WithValues(log.Kv{"svc": "generate.prometheus.Service"})
return nil
}
// AlertGenerator knows how to generate multiwindow multi-burn SLO alerts.
type AlertGenerator interface {
GenerateMWMBAlerts(ctx context.Context, slo alert.SLO) (*alert.MWMBAlertGroup, error)
}
// SLIRecordingRulesGenerator knows how to generate SLI recording rules.
type SLIRecordingRulesGenerator interface {
GenerateSLIRecordingRules(ctx context.Context, slo prometheus.SLO, alerts alert.MWMBAlertGroup) ([]rulefmt.Rule, error)
}
// MetadataRecordingRulesGenerator knows how to generate metadata recording rules.
type MetadataRecordingRulesGenerator interface {
GenerateMetadataRecordingRules(ctx context.Context, info info.Info, slo prometheus.SLO, alerts alert.MWMBAlertGroup) ([]rulefmt.Rule, error)
}
// SLOAlertRulesGenerator knows hot to generate SLO alert rules.
type SLOAlertRulesGenerator interface {
GenerateSLOAlertRules(ctx context.Context, slo prometheus.SLO, alerts alert.MWMBAlertGroup) ([]rulefmt.Rule, error)
}
// Service is the application service for the generation of SLO for Prometheus.
type Service struct {
alertGen AlertGenerator
sliRecordRuleGen SLIRecordingRulesGenerator
metaRecordRuleGen MetadataRecordingRulesGenerator
alertRuleGen SLOAlertRulesGenerator
logger log.Logger
}
// NewService returns a new Prometheus application service.
func NewService(config ServiceConfig) (*Service, error) {
err := config.defaults()
if err != nil {
return nil, fmt.Errorf("invalid configuration: %w", err)
}
return &Service{
alertGen: config.AlertGenerator,
sliRecordRuleGen: config.SLIRecordingRulesGenerator,
metaRecordRuleGen: config.MetaRecordingRulesGenerator,
alertRuleGen: config.SLOAlertRulesGenerator,
logger: config.Logger,
}, nil
}
type Request struct {
// Info about the application and execution, normally used as metadata.
Info info.Info
// ExtraLabels are the extra labels added to the SLOs on execution time.
ExtraLabels map[string]string
// disable prometheus promql support if needed
DisablePromExprValidation bool
// SLOGroup are the SLOs group that will be used to generate the SLO results and Prom rules.
SLOGroup prometheus.SLOGroup
}
type SLOResult struct {
SLO prometheus.SLO
Alerts alert.MWMBAlertGroup
SLORules prometheus.SLORules
}
type Response struct {
PrometheusSLOs []SLOResult
}
func (s Service) Generate(ctx context.Context, r Request) (*Response, error) {
if r.DisablePromExprValidation == false {
err := r.SLOGroup.Validate()
if err != nil {
return nil, fmt.Errorf("invalid SLO group: %w", err)
}
}
// Generate Prom rules.
results := make([]SLOResult, 0, len(r.SLOGroup.SLOs))
for _, slo := range r.SLOGroup.SLOs {
// Add extra labels.
slo.Labels = mergeLabels(slo.Labels, r.ExtraLabels)
// Generate SLO result.
result, err := s.generateSLO(ctx, r.Info, slo)
if err != nil {
return nil, fmt.Errorf("could not generate %q slo: %w", slo.ID, err)
}
results = append(results, *result)
}
return &Response{
PrometheusSLOs: results,
}, nil
}
func (s Service) generateSLO(ctx context.Context, info info.Info, slo prometheus.SLO) (*SLOResult, error) {
logger := s.logger.WithCtxValues(ctx).WithValues(log.Kv{"slo": slo.ID})
// Generate the MWMB alerts.
alertSLO := alert.SLO{
ID: slo.ID,
Objective: slo.Objective,
TimeWindow: slo.TimeWindow,
}
as, err := s.alertGen.GenerateMWMBAlerts(ctx, alertSLO)
if err != nil {
return nil, fmt.Errorf("could not generate SLO alerts: %w", err)
}
logger.Infof("Multiwindow-multiburn alerts generated")
// Generate SLI recording rules.
sliRecordingRules, err := s.sliRecordRuleGen.GenerateSLIRecordingRules(ctx, slo, *as)
if err != nil {
return nil, fmt.Errorf("could not generate Prometheus sli recording rules: %w", err)
}
logger.WithValues(log.Kv{"rules": len(sliRecordingRules)}).Infof("SLI recording rules generated")
// Generate Metadata recording rules.
metaRecordingRules, err := s.metaRecordRuleGen.GenerateMetadataRecordingRules(ctx, info, slo, *as)
if err != nil {
return nil, fmt.Errorf("could not generate Prometheus metadata recording rules: %w", err)
}
logger.WithValues(log.Kv{"rules": len(metaRecordingRules)}).Infof("Metadata recording rules generated")
// Generate Alert rules.
alertRules, err := s.alertRuleGen.GenerateSLOAlertRules(ctx, slo, *as)
if err != nil {
return nil, fmt.Errorf("could not generate Prometheus alert rules: %w", err)
}
logger.WithValues(log.Kv{"rules": len(alertRules)}).Infof("SLO alert rules generated")
return &SLOResult{
SLO: slo,
Alerts: *as,
SLORules: prometheus.SLORules{
SLIErrorRecRules: sliRecordingRules,
MetadataRecRules: metaRecordingRules,
AlertRules: alertRules,
},
}, nil
}
func mergeLabels(ms ...map[string]string) map[string]string {
res := map[string]string{}
for _, m := range ms {
for k, v := range m {
res[k] = v
}
}
return res
}