From 92e10fb824e42616f4d0a6188ac252a7c71feb26 Mon Sep 17 00:00:00 2001 From: Ashwanth Kumar Date: Tue, 6 Sep 2016 21:19:34 +0530 Subject: [PATCH] Extracting CheckStatusToString out and improving the log message Also removed verbose not enabled log message for now, need to find a better way to do it --- alert-manager.go | 5 ++--- checks/checks.go | 16 ++++++++++++++++ checks/checks_test.go | 15 +++++++++++++++ notifiers/slack-notifier.go | 18 +----------------- notifiers/slack-notifier_test.go | 9 --------- 5 files changed, 34 insertions(+), 29 deletions(-) create mode 100644 checks/checks_test.go diff --git a/alert-manager.go b/alert-manager.go index e4cc912..0144b0f 100644 --- a/alert-manager.go +++ b/alert-manager.go @@ -122,9 +122,8 @@ func (a *AlertManager) processCheck(check checks.AppCheck) { keyPrefix := a.keyPrefix(check) delete(a.AlertCount, keyPrefix) } - } else { - fmt.Printf("Monitoring disabled for %s via alerts.enabled label in app config\n", check.App) } + // TODO - Add a log message that runs only once per every new app / if app state has changed } func (a *AlertManager) checkForRouteWithCheckLevel(level checks.CheckStatus, allRoutes []routes.Route) bool { @@ -138,7 +137,7 @@ func (a *AlertManager) checkForRouteWithCheckLevel(level checks.CheckStatus, all } func (a *AlertManager) notifyCheck(check checks.AppCheck, allRoutes []routes.Route) { - fmt.Printf("[NotifyCheck] Check: %s, Reason: %s ", check.CheckName, check.Message) + fmt.Printf("[NotifyCheck] App: %s, Result: %s, Check: %s, Reason: %s \n", check.App, checks.CheckStatusToString(check.Result), check.CheckName, check.Message) for _, route := range allRoutes { if route.Match(check) { for _, notifier := range a.Notifiers { diff --git a/checks/checks.go b/checks/checks.go index 8b65e41..c2b0fd6 100644 --- a/checks/checks.go +++ b/checks/checks.go @@ -31,3 +31,19 @@ const ( ) var CheckLevels = [...]CheckStatus{Warning, Critical} + +func CheckStatusToString(result CheckStatus) string { + value := "Unknown" + switch result { + case Pass: + value = "Passed" + case Resolved: + value = "Resolved" + case Warning: + value = "Warning" + case Critical: + value = "Critical" + } + + return value +} diff --git a/checks/checks_test.go b/checks/checks_test.go new file mode 100644 index 0000000..b54aadb --- /dev/null +++ b/checks/checks_test.go @@ -0,0 +1,15 @@ +package checks + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestResultToString(t *testing.T) { + assert.Equal(t, "Passed", CheckStatusToString(Pass)) + assert.Equal(t, "Warning", CheckStatusToString(Warning)) + assert.Equal(t, "Critical", CheckStatusToString(Critical)) + assert.Equal(t, "Resolved", CheckStatusToString(Resolved)) + assert.Equal(t, "Unknown", CheckStatusToString(127)) +} diff --git a/notifiers/slack-notifier.go b/notifiers/slack-notifier.go index 283e1cc..c064565 100644 --- a/notifiers/slack-notifier.go +++ b/notifiers/slack-notifier.go @@ -27,7 +27,7 @@ func (s *Slack) Notify(check checks.AppCheck) { attachment. AddField(slack.Field{Title: "App", Value: check.App, Short: true}). AddField(slack.Field{Title: "Check", Value: check.CheckName, Short: true}). - AddField(slack.Field{Title: "Result", Value: s.resultToString(check.Result), Short: true}). + AddField(slack.Field{Title: "Result", Value: checks.CheckStatusToString(check.Result), Short: true}). AddField(slack.Field{Title: "Times", Value: fmt.Sprintf("%d", check.Times), Short: true}) destination := maps.GetString(check.Labels, "alerts.slack.channel", s.Channel) @@ -78,22 +78,6 @@ func (s *Slack) resultToColor(result checks.CheckStatus) *string { return &color } -func (s *Slack) resultToString(result checks.CheckStatus) string { - value := "Unknown" - switch result { - case checks.Pass: - value = "Passed" - case checks.Resolved: - value = "Resolved" - case checks.Warning: - value = "Warning" - case checks.Critical: - value = "Critical" - } - - return value -} - func (s *Slack) parseOwners(owners []string) string { parsedOwners := []string{} for _, owner := range owners { diff --git a/notifiers/slack-notifier_test.go b/notifiers/slack-notifier_test.go index d531fe4..aa701f2 100644 --- a/notifiers/slack-notifier_test.go +++ b/notifiers/slack-notifier_test.go @@ -15,12 +15,3 @@ func TestResultToColor(t *testing.T) { assert.Equal(t, "danger", *slack.resultToColor(checks.Critical)) assert.Equal(t, "black", *slack.resultToColor(127)) } - -func TestResultToString(t *testing.T) { - slack := Slack{} - assert.Equal(t, "Passed", slack.resultToString(checks.Pass)) - assert.Equal(t, "Warning", slack.resultToString(checks.Warning)) - assert.Equal(t, "Critical", slack.resultToString(checks.Critical)) - assert.Equal(t, "Resolved", slack.resultToString(checks.Resolved)) - assert.Equal(t, "Unknown", slack.resultToString(127)) -}