Skip to content

Commit

Permalink
Add option to disable ticket support and save prometheus load
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Feb 4, 2020
1 parent 307b12d commit c6774ba
Show file tree
Hide file tree
Showing 4 changed files with 316 additions and 14 deletions.
12 changes: 7 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import (

func main() {
var (
sloPath = ""
classesPath = ""
ruleOutput = ""
sloPath = ""
classesPath = ""
ruleOutput = ""
disableTicket = false
)
flag.StringVar(&sloPath, "slo.path", "", "A YML file describing SLOs")
flag.StringVar(&classesPath, "classes.path", "", "A YML file describing SLOs classes (optional)")
flag.StringVar(&ruleOutput, "rule.output", "", "Output to describe a prometheus rules")
flag.BoolVar(&disableTicket, "disable.ticket", false, "Disable generation of alerts of kind ticket")

flag.Parse()

Expand Down Expand Up @@ -57,10 +59,10 @@ func main() {
log.Fatalf("Could not compile SLO: %q, err: %q", slo.Name, err.Error())
}

ruleGroups.Groups = append(ruleGroups.Groups, slo.GenerateGroupRules(sloClass)...)
ruleGroups.Groups = append(ruleGroups.Groups, slo.GenerateGroupRules(sloClass, disableTicket)...)
ruleGroups.Groups = append(ruleGroups.Groups, rulefmt.RuleGroup{
Name: "slo:" + slo.Name + ":alert",
Rules: slo.GenerateAlertRules(sloClass),
Rules: slo.GenerateAlertRules(sloClass, disableTicket),
})
}

Expand Down
12 changes: 12 additions & 0 deletions slo/samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,15 @@ var defaultSamples = []sample{
Buckets: []string{"1d", "3d"},
},
}

var disabletBucketsForTickets = []string{"3d", "1d", "2h"}

func isTicketSample(sample string) bool {
for _, bucketSample := range disabletBucketsForTickets {
if bucketSample == sample {
return true
}
}

return false
}
21 changes: 19 additions & 2 deletions slo/slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (o *Objectives) LatencyBuckets() []string {
return latencyBuckets
}

func (slo *SLO) GenerateAlertRules(sloClass *Class) []rulefmt.Rule {
func (slo *SLO) GenerateAlertRules(sloClass *Class, disableTicket bool) []rulefmt.Rule {
objectives := slo.Objectives
if sloClass != nil {
objectives = sloClass.Objectives
Expand All @@ -104,6 +104,18 @@ func (slo *SLO) GenerateAlertRules(sloClass *Class) []rulefmt.Rule {
slo.fillMetadata(&rule)
}

if disableTicket {
alertRulesWithoutTicket := []rulefmt.Rule{}

for _, rule := range alertRules {
if rule.Labels["severity"] != "ticket" {
alertRulesWithoutTicket = append(alertRulesWithoutTicket, rule)
}
}

return alertRulesWithoutTicket
}

return alertRules
}

Expand All @@ -117,7 +129,7 @@ func (slo *SLO) fillMetadata(rule *rulefmt.Rule) {
}
}

func (slo *SLO) GenerateGroupRules(sloClass *Class) []rulefmt.RuleGroup {
func (slo *SLO) GenerateGroupRules(sloClass *Class, disableTicket bool) []rulefmt.RuleGroup {
rules := []rulefmt.RuleGroup{}

objectives := slo.Objectives
Expand All @@ -130,6 +142,7 @@ func (slo *SLO) GenerateGroupRules(sloClass *Class) []rulefmt.RuleGroup {
}

for _, sample := range defaultSamples {

interval, err := model.ParseDuration(sample.Interval)
if err != nil {
log.Fatal(err)
Expand All @@ -141,6 +154,10 @@ func (slo *SLO) GenerateGroupRules(sloClass *Class) []rulefmt.RuleGroup {
}

for _, bucket := range sample.Buckets {
if disableTicket && isTicketSample(bucket) {
continue
}

ruleGroup.Rules = append(ruleGroup.Rules, slo.generateRules(bucket, latencyBuckets)...)
}

Expand Down

0 comments on commit c6774ba

Please sign in to comment.