From 5fd68538fb193bbcaaf196a57296bd2f662ae00c Mon Sep 17 00:00:00 2001 From: Kubo Ryosuke Date: Sat, 30 Oct 2021 22:31:24 +0900 Subject: [PATCH] Update Alarm adapter --- README.md | 10 ++++----- adapters/report.go | 47 +++++++++++++++++++---------------------- adapters/report_test.go | 11 ++++++---- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 9ee86ea..71a85f1 100644 --- a/README.md +++ b/README.md @@ -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 */ }, ) diff --git a/adapters/report.go b/adapters/report.go index 722e310..9a3fb87 100644 --- a/adapters/report.go +++ b/adapters/report.go @@ -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 } } } diff --git a/adapters/report_test.go b/adapters/report_test.go index 4c34f87..7d2fc12 100644 --- a/adapters/report_test.go +++ b/adapters/report_test.go @@ -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}}) @@ -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}}) @@ -45,6 +45,9 @@ func TestOnReportFrequencyFilter(t *testing.T) { handler(core.Reports{{Name: "test", Severity: core.Info}}) require.Equal(t, 1, r) require.Equal(t, 1, a) + handler(core.Reports{{Name: "test", Severity: core.Info}}) + require.Equal(t, 1, r) + require.Equal(t, 1, a) } func TestReportLogger(t *testing.T) {