Skip to content

Commit

Permalink
Rename alertAlgorithm to alertMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Aug 6, 2019
1 parent 361243a commit d8b8a26
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 40 deletions.
1 change: 0 additions & 1 deletion algorithms/register.go

This file was deleted.

16 changes: 8 additions & 8 deletions algorithms/algorithm.go → methods/method.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package algoritms
package methods

import "github.com/prometheus/prometheus/pkg/rulefmt"

type AlertAlgorithm interface {
type AlertMethod interface {
AlertForError(serviceName string, availabilityTarget float64, annotations map[string]string) []rulefmt.Rule
AlertForLatency(serviceName string, targets []LatencyTarget, annotations map[string]string) []rulefmt.Rule
}

var algorithms = map[string]AlertAlgorithm{}
var methods = map[string]AlertMethod{}

func register(algorithm AlertAlgorithm, name string) AlertAlgorithm {
algorithms[name] = algorithm
return algorithm
func register(method AlertMethod, name string) AlertMethod {
methods[name] = method
return method
}

func Get(name string) AlertAlgorithm {
return algorithms[name]
func Get(name string) AlertMethod {
return methods[name]
}

type LatencyTarget struct {
Expand Down
2 changes: 1 addition & 1 deletion algorithms/multi-window.go → methods/multi-window.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package algoritms
package methods

import (
"fmt"
Expand Down
30 changes: 15 additions & 15 deletions slo/slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"
"strings"

algorithms "github.com/globocom/slo-generator/algorithms"
methods "github.com/globocom/slo-generator/methods"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/rulefmt"
)
Expand All @@ -14,8 +14,8 @@ type SLOSpec struct {
}

type ExprBlock struct {
AlertAlgorithm string `yaml:"alertAlgorithm"`
Expr string `yaml:"expr"`
AlertMethod string `yaml:"alertMethod"`
Expr string `yaml:"expr"`
}

func (block *ExprBlock) ComputeExpr(window, le string) string {
Expand All @@ -24,26 +24,26 @@ func (block *ExprBlock) ComputeExpr(window, le string) string {
}

type SLO struct {
Name string `yaml:"name"`
AvailabilityObjectivePercent float64 `yaml:"availabilityObjectivePercent"`
LatencyObjectiveBuckets []algorithms.LatencyTarget `yaml:"latencyObjectiveBuckets"`
ErrorRateRecord ExprBlock `yaml:"errorRateRecord"`
LatencyRecord ExprBlock `yaml:"latencyRecord"`
Annotations map[string]string `yaml:"annotations"`
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"`
}

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

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

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

Expand Down
22 changes: 11 additions & 11 deletions slo/slo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"
"time"

algorithms "github.com/globocom/slo-generator/algorithms"
methods "github.com/globocom/slo-generator/methods"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/rulefmt"
"github.com/stretchr/testify/assert"
Expand All @@ -14,7 +14,7 @@ func TestSLOGenerateGroupRules(t *testing.T) {
slo := &SLO{
Name: "my-team.my-service.payment",
AvailabilityObjectivePercent: 99.9,
LatencyObjectiveBuckets: []algorithms.LatencyTarget{
LatencyObjectiveBuckets: []methods.LatencyTarget{
{
LE: "0.1",
Target: 90,
Expand All @@ -25,12 +25,12 @@ func TestSLOGenerateGroupRules(t *testing.T) {
},
},
ErrorRateRecord: ExprBlock{
AlertAlgorithm: "multi-window",
Expr: "sum(rate(http_errors[$window]))/sum(rate(http_total[$window]))",
AlertMethod: "multi-window",
Expr: "sum(rate(http_errors[$window]))/sum(rate(http_total[$window]))",
},
LatencyRecord: ExprBlock{
AlertAlgorithm: "multi-window",
Expr: "sum(rate(http_bucket{le=\"$le\"}[$window]))/sum(rate(http_total[$window]))",
AlertMethod: "multi-window",
Expr: "sum(rate(http_bucket{le=\"$le\"}[$window]))/sum(rate(http_total[$window]))",
},
Annotations: map[string]string{
"message": "Service A has lower SLI",
Expand Down Expand Up @@ -240,7 +240,7 @@ func TestSLOGenerateAlertRules(t *testing.T) {
slo := &SLO{
Name: "my-team.my-service.payment",
AvailabilityObjectivePercent: 99.9,
LatencyObjectiveBuckets: []algorithms.LatencyTarget{
LatencyObjectiveBuckets: []methods.LatencyTarget{
{
LE: "0.1",
Target: 95,
Expand All @@ -251,12 +251,12 @@ func TestSLOGenerateAlertRules(t *testing.T) {
},
},
ErrorRateRecord: ExprBlock{
AlertAlgorithm: "multi-window",
Expr: "kk",
AlertMethod: "multi-window",
Expr: "kk",
},
LatencyRecord: ExprBlock{
AlertAlgorithm: "multi-window",
Expr: "kk",
AlertMethod: "multi-window",
Expr: "kk",
},
Annotations: map[string]string{
"message": "Service A has lower SLI",
Expand Down
8 changes: 4 additions & 4 deletions slo_example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ slos:
slack_channel: '_team_a'

errorRateRecord:
algorithm: multi-window
alertMethod: multi-window
expr: |
sum (rate(http_requests_total{job="service-a", status="5xx"}[$window])) /
sum (rate(http_requests_total{job="service-a"}[$window]))
latencyRecord:
algorithm: multi-window
alertMethod: multi-window
expr: |
sum (rate(http_request_duration_seconds_bucket{job="service-a", le="$le"}[$window])) /
sum (rate(http_requests_total{job="service-a"}[$window]))
Expand All @@ -41,13 +41,13 @@ slos:
slack_channel: '_team_b'

errorRateRecord:
algorithm: multi-window
alertMethod: multi-window
expr: |
sum (rate(http_requests_total{job="service-b", status="5xx"}[$window])) /
sum (rate(http_requests_total{job="service-b"}[$window]))
latencyRecord:
algorithm: multi-window
alertMethod: multi-window
expr: |
sum (rate(http_request_duration_seconds_bucket{job="service-b", le="$le"}[$window])) /
sum (rate(http_requests_total{job="service-b"}[$window]))

0 comments on commit d8b8a26

Please sign in to comment.