Skip to content

Commit

Permalink
Improve style of objectives creating a new block
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Aug 6, 2019
1 parent d8b8a26 commit 50d8f1f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
23 changes: 14 additions & 9 deletions slo/slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,31 @@ func (block *ExprBlock) ComputeExpr(window, le string) string {
}

type SLO struct {
Name string `yaml:"name"`
AvailabilityObjectivePercent float64 `yaml:"availabilityObjectivePercent"`
LatencyObjectiveBuckets []methods.LatencyTarget `yaml:"latencyObjectiveBuckets"`
ErrorRateRecord ExprBlock `yaml:"errorRateRecord"`
LatencyRecord ExprBlock `yaml:"latencyRecord"`
Annotations map[string]string `yaml:"annotations"`
Name string `yaml:"name"`
Objectives Objectives

ErrorRateRecord ExprBlock `yaml:"errorRateRecord"`
LatencyRecord ExprBlock `yaml:"latencyRecord"`
Annotations map[string]string `yaml:"annotations"`
}

type Objectives struct {
Availability float64 `yaml:"availability"`
Latency []methods.LatencyTarget `yaml:"latency"`
}

func (slo SLO) GenerateAlertRules() []rulefmt.Rule {
alertRules := []rulefmt.Rule{}

errorMethod := methods.Get(slo.ErrorRateRecord.AlertMethod)
if errorMethod != nil {
errorRules := errorMethod.AlertForError(slo.Name, slo.AvailabilityObjectivePercent, slo.Annotations)
errorRules := errorMethod.AlertForError(slo.Name, slo.Objectives.Availability, slo.Annotations)
alertRules = append(alertRules, errorRules...)
}

latencyMethod := methods.Get(slo.LatencyRecord.AlertMethod)
if latencyMethod != nil {
latencyRules := latencyMethod.AlertForLatency(slo.Name, slo.LatencyObjectiveBuckets, slo.Annotations)
latencyRules := latencyMethod.AlertForLatency(slo.Name, slo.Objectives.Latency, slo.Annotations)
alertRules = append(alertRules, latencyRules...)
}

Expand Down Expand Up @@ -75,7 +80,7 @@ func (slo SLO) GenerateGroupRules() []rulefmt.RuleGroup {

ruleGroup.Rules = append(ruleGroup.Rules, errorRateRecord)

for _, latencyBucket := range slo.LatencyObjectiveBuckets {
for _, latencyBucket := range slo.Objectives.Latency {
latencyRateRecord := rulefmt.Rule{
Record: "slo:service_latency:ratio_rate_" + bucket,
Expr: slo.LatencyRecord.ComputeExpr(bucket, latencyBucket.LE),
Expand Down
40 changes: 22 additions & 18 deletions slo/slo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ import (
func TestSLOGenerateGroupRules(t *testing.T) {
slo := &SLO{
Name: "my-team.my-service.payment",
AvailabilityObjectivePercent: 99.9,
LatencyObjectiveBuckets: []methods.LatencyTarget{
{
LE: "0.1",
Target: 90,
},
{
LE: "0.5",
Target: 99,
Objectives: Objectives{
Availability: 99.9,
Latency: []methods.LatencyTarget{
{
LE: "0.1",
Target: 90,
},
{
LE: "0.5",
Target: 99,
},
},
},
ErrorRateRecord: ExprBlock{
Expand Down Expand Up @@ -239,15 +241,17 @@ func TestSLOGenerateGroupRules(t *testing.T) {
func TestSLOGenerateAlertRules(t *testing.T) {
slo := &SLO{
Name: "my-team.my-service.payment",
AvailabilityObjectivePercent: 99.9,
LatencyObjectiveBuckets: []methods.LatencyTarget{
{
LE: "0.1",
Target: 95,
},
{
LE: "0.5",
Target: 99,
Objectives: Objectives{
Availability: 99.9,
Latency: []methods.LatencyTarget{
{
LE: "0.1",
Target: 95,
},
{
LE: "0.5",
Target: 99,
},
},
},
ErrorRateRecord: ExprBlock{
Expand Down
10 changes: 6 additions & 4 deletions slo_example.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
slos:
- name: myteam-a.service-a
availabilityObjectivePercent: 99
latencyObjectiveBuckets:
objectives:
availability: 99
latency:
- le: 5.0 # 95% < 5s
target: 95

Expand All @@ -27,8 +28,9 @@ slos:
- name: myteam-b.service-b
availabilityObjectivePercent: 99.9
latencyObjectiveBuckets:
objectives:
availability: 99.9
latency:
- le: 0.05 # 90% < 50ms
target: 90

Expand Down

0 comments on commit 50d8f1f

Please sign in to comment.