Skip to content

Commit

Permalink
Update Alarm adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfish-shogi committed Oct 30, 2021
1 parent 86cce20 commit 4b68216
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ And `adapters` package has some useful handlers.
config.OnReport = core.MergeOnReportHandlers(
adapters.ReportLogger(&adapters.ReportLogConfig{JSON: true}, os.Stdout),
adapters.Alarm(&adapters.AlarmConfig{
OnAlarm : func(reports core.Reports) { /* start alarm */ },
OnRecover : func(reports core.Reports) { /* stop alarm */ },
Window : 10,
AlarmIfErrorGreaterThanEqual: 2,
RecoverIfErrorLessThanEqual : 0,
OnAlarm : func(reports core.Reports) { /* start alarm */ },
OnRecover : func(reports core.Reports) { /* stop alarm */ },
Window : 10,
AlarmIfErrorGreaterThanEqual : 2,
RecoverIfInfoGreaterThanEqual: 10,
}),
func(reports core.Reports) { /* send metrics */ },
)
Expand Down
47 changes: 22 additions & 25 deletions adapters/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,41 @@ import (
)

type AlarmConfig struct {
OnAlarm core.OnReportHandler
OnRecover core.OnReportHandler
Window int
AlarmIfErrorGreaterThanEqual int
RecoverIfErrorLessThanEqual int
OnAlarm core.OnReportHandler
OnRecover core.OnReportHandler
Window int
AlarmIfErrorGreaterThanEqual int
RecoverIfInfoGreaterThanEqual int
}

func Alarm(config *AlarmConfig) core.OnReportHandler {
history := make([]bool, 0)
var upper bool
lower := true
history := make([]core.Severity, 0)
var alarm bool
return func(reports core.Reports) {
if reports.WorstSeverity() == core.Error {
history = append(history, true)
} else {
history = append(history, false)
}
history = append(history, reports.WorstSeverity())
if len(history) > config.Window {
history = history[1:]
}
var cnt int
for _, val := range history {
if val {
cnt++
var infoCnt int
var errCnt int
for _, hist := range history {
switch hist {
case core.Info:
infoCnt++
case core.Error:
errCnt++
}
}
if cnt >= config.AlarmIfErrorGreaterThanEqual {
if !upper {
if errCnt >= config.AlarmIfErrorGreaterThanEqual {
if !alarm {
config.OnAlarm(reports)
alarm = true
}
upper = true
lower = false
} else if cnt <= config.RecoverIfErrorLessThanEqual {
if !lower {
} else if infoCnt >= config.RecoverIfInfoGreaterThanEqual {
if alarm {
config.OnRecover(reports)
alarm = false
}
upper = false
lower = true
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions adapters/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func TestOnReportFrequencyFilter(t *testing.T) {
OnRecover: func(reports core.Reports) {
r++
},
Window: 5,
AlarmIfErrorGreaterThanEqual: 3,
RecoverIfErrorLessThanEqual: 1,
Window: 5,
AlarmIfErrorGreaterThanEqual: 3,
RecoverIfInfoGreaterThanEqual: 4,
})
handler(core.Reports{{Name: "test", Severity: core.Error}})
handler(core.Reports{{Name: "test", Severity: core.Info}})
Expand All @@ -36,7 +36,7 @@ func TestOnReportFrequencyFilter(t *testing.T) {
require.Equal(t, 0, a)
handler(core.Reports{{Name: "test", Severity: core.Error}})
require.Equal(t, 1, a)
handler(core.Reports{{Name: "test", Severity: core.Error}})
handler(core.Reports{{Name: "test", Severity: core.Warn}})
handler(core.Reports{{Name: "test", Severity: core.Info}})
handler(core.Reports{{Name: "test", Severity: core.Info}})
handler(core.Reports{{Name: "test", Severity: core.Info}})
Expand Down

0 comments on commit 4b68216

Please sign in to comment.