Skip to content

Commit

Permalink
Fix #18.2 alert string printing address
Browse files Browse the repository at this point in the history
- the call to errors.Wrapf was obscured in a function call and caused the linter to miss a
pointer that should have been dereferenced.

- the function that wraps errors now takes a string so the caller must call fmt.Sprintf
which catches any errors

- updated version to 0.4.3
  • Loading branch information
jeffwillette committed Oct 25, 2017
1 parent 33fa728 commit 8e7cd73
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
41 changes: 22 additions & 19 deletions cmd/alertd_container.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"strings"

"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -123,18 +124,20 @@ func (c *AlertdContainer) CheckExists(e error) {
switch {
case c.IsUnknown(e) && !c.ExistenceCheck.AlertActive:
// if the alert is not active I need to alert and make it active
c.Alert.Add(e, ErrExistCheckFail, "%s", c.Name)
c.Alert.Add(e, ErrExistCheckFail, fmt.Sprintf("%s", c.Name))
c.ExistenceCheck.ToggleAlertActive()

case c.IsUnknown(e) && c.ExistenceCheck.AlertActive:
// do nothing
case c.HasErrored(e):
// if there is some other error besides an existence check error
c.Alert.Add(e, ErrUnknown, "%s", c.Name)
c.Alert.Add(e, ErrUnknown, fmt.Sprintf("%s", c.Name))

case c.HasBecomeKnown(e):
c.Alert.Add(ErrExistCheckRecovered, nil, "%s", c.Name)
c.Alert.Add(ErrExistCheckRecovered, nil, fmt.Sprintf("%s", c.Name))
c.ExistenceCheck.ToggleAlertActive()
default:
return // nothing is wrong, just keep going
}
}

Expand All @@ -148,14 +151,14 @@ func (c *AlertdContainer) ShouldAlertRunning(j *types.ContainerJSON) bool {
func (c *AlertdContainer) CheckRunning(j *types.ContainerJSON) {
switch {
case c.ShouldAlertRunning(j) && !c.RunningCheck.AlertActive:
c.Alert.Add(ErrRunningCheckFail, nil, "%s: expected running state: %t, current running state: %t",
c.Name, c.RunningCheck.Expected, j.State.Running)
c.Alert.Add(ErrRunningCheckFail, nil, fmt.Sprintf("%s: expected running state: "+
"%t, current running state: %t", c.Name, *c.RunningCheck.Expected, j.State.Running))

c.RunningCheck.ToggleAlertActive()

case !c.ShouldAlertRunning(j) && c.RunningCheck.AlertActive:
c.Alert.Add(ErrRunningCheckRecovered, nil, "%s: expected running state: %t, current running state: %t",
c.Name, c.RunningCheck.Expected, j.State.Running)
c.Alert.Add(ErrRunningCheckRecovered, nil, fmt.Sprintf("%s: expected running state: "+
"%t, current running state: %t", c.Name, *c.RunningCheck.Expected, j.State.Running))

c.RunningCheck.ToggleAlertActive()
}
Expand Down Expand Up @@ -185,14 +188,14 @@ func (c *AlertdContainer) CheckCPUUsage(s *types.Stats) {

switch {
case a && !c.CPUCheck.AlertActive:
c.Alert.Add(ErrCPUCheckFail, nil, "%s: CPU limit: %d, current usage: %d",
c.Name, c.CPUCheck.Limit, u)
c.Alert.Add(ErrCPUCheckFail, nil, fmt.Sprintf("%s: CPU limit: %d, current usage: %d",
c.Name, c.CPUCheck.Limit, u))

c.CPUCheck.ToggleAlertActive()

case !a && c.CPUCheck.AlertActive:
c.Alert.Add(ErrCPUCheckRecovered, nil, "%s: CPU limit: %d, current usage %d",
c.Name, c.CPUCheck.Limit, u)
c.Alert.Add(ErrCPUCheckRecovered, nil, fmt.Sprintf("%s: CPU limit: %d, current usage %d",
c.Name, c.CPUCheck.Limit, u))

c.CPUCheck.ToggleAlertActive()
}
Expand All @@ -211,14 +214,14 @@ func (c *AlertdContainer) CheckMinPids(s *types.Stats) {
case c.PIDCheck.Limit == nil:
// do nothing because the check is disabled
case a && !c.PIDCheck.AlertActive:
c.Alert.Add(ErrMinPIDCheckFail, nil, "%s: minimum PIDs: %d, current PIDs: %d",
c.Name, c.PIDCheck.Limit, s.PidsStats.Current)
c.Alert.Add(ErrMinPIDCheckFail, nil, fmt.Sprintf("%s: minimum PIDs: %d, current PIDs: %d",
c.Name, c.PIDCheck.Limit, s.PidsStats.Current))

c.PIDCheck.ToggleAlertActive()

case !a && c.PIDCheck.AlertActive:
c.Alert.Add(ErrMinPIDCheckRecovered, nil, "%s: minimum PIDs: %d, current PIDs: %d",
c.Name, c.PIDCheck.Limit, s.PidsStats.Current)
c.Alert.Add(ErrMinPIDCheckRecovered, nil, fmt.Sprintf("%s: minimum PIDs: %d, current PIDs: %d",
c.Name, c.PIDCheck.Limit, s.PidsStats.Current))

c.PIDCheck.ToggleAlertActive()
}
Expand Down Expand Up @@ -247,14 +250,14 @@ func (c *AlertdContainer) CheckMemory(s *types.Stats) {
case c.MemCheck.Limit == nil:
// do nothing because the check is disabled
case a && !c.MemCheck.AlertActive:
c.Alert.Add(ErrMemCheckFail, nil, "%s: Memory limit: %d, current usage: %d",
c.Name, c.MemCheck.Limit, u)
c.Alert.Add(ErrMemCheckFail, nil, fmt.Sprintf("%s: Memory limit: %d, current usage: %d",
c.Name, c.MemCheck.Limit, u))

c.MemCheck.ToggleAlertActive()

case !a && c.MemCheck.AlertActive:
c.Alert.Add(ErrMemCheckRecovered, nil, "%s: Memory limit: %d, current usage: %d",
c.Name, c.MemCheck.Limit, u)
c.Alert.Add(ErrMemCheckRecovered, nil, fmt.Sprintf("%s: Memory limit: %d, current usage: %d",
c.Name, c.MemCheck.Limit, u))

c.MemCheck.ToggleAlertActive()
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ func (a *Alert) Len() int {
}

// Add should take in an error and wrap it
func (a *Alert) Add(e1, e2 error, fs string, args ...interface{}) {
func (a *Alert) Add(e1, e2 error, s string) {

e := e1
if e2 != nil {
e = errors.Wrap(e1, e2.Error())
}

err := errors.Wrapf(e, fs, args...)
err := errors.Wrap(e, s)
a.Messages = append(a.Messages, err)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
)

var version = "Version 0.4.2"
var version = "Version 0.4.3"

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Expand Down

0 comments on commit 8e7cd73

Please sign in to comment.