Skip to content

Commit

Permalink
Improve ipvs input error strings and logging (influxdata#6530)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson authored and idohalevi committed Sep 23, 2020
1 parent 7d00e66 commit 843d90d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
1 change: 1 addition & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions plugins/common/logrus/hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package logrus

import (
"io/ioutil"
"log"
"strings"
"sync"

"github.com/sirupsen/logrus"
)

var once sync.Once

type LogHook struct {
}

// Install a logging hook into the logrus standard logger, diverting all logs
// through the Telegraf logger at debug level. This is useful for libraries
// that directly log to the logrus system without providing an override method.
func InstallHook() {
once.Do(func() {
logrus.SetOutput(ioutil.Discard)
logrus.AddHook(&LogHook{})
})
}

func (h *LogHook) Fire(entry *logrus.Entry) error {
msg := strings.ReplaceAll(entry.Message, "\n", " ")
log.Print("D! [logrus] ", msg)
return nil
}

func (h *LogHook) Levels() []logrus.Level {
return logrus.AllLevels
}
13 changes: 8 additions & 5 deletions plugins/inputs/ipvs/ipvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
package ipvs

import (
"errors"
"fmt"
"math/bits"
"strconv"
"syscall"

"github.com/docker/libnetwork/ipvs"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/common/logrus"
"github.com/influxdata/telegraf/plugins/inputs"
)

Expand All @@ -35,7 +35,7 @@ func (i *IPVS) Gather(acc telegraf.Accumulator) error {
if i.handle == nil {
h, err := ipvs.New("") // TODO: make the namespace configurable
if err != nil {
return errors.New("Unable to open IPVS handle")
return fmt.Errorf("unable to open IPVS handle: %v", err)
}
i.handle = h
}
Expand All @@ -44,7 +44,7 @@ func (i *IPVS) Gather(acc telegraf.Accumulator) error {
if err != nil {
i.handle.Close()
i.handle = nil // trigger a reopen on next call to gather
return errors.New("Failed to list IPVS services")
return fmt.Errorf("failed to list IPVS services: %v", err)
}
for _, s := range services {
fields := map[string]interface{}{
Expand All @@ -61,7 +61,7 @@ func (i *IPVS) Gather(acc telegraf.Accumulator) error {

destinations, err := i.handle.GetDestinations(s)
if err != nil {
i.Log.Errorf("Failed to list destinations for a virtual server: %s", err.Error())
i.Log.Errorf("Failed to list destinations for a virtual server: %v", err)
continue // move on to the next virtual server
}

Expand Down Expand Up @@ -148,5 +148,8 @@ func addressFamilyToString(af uint16) string {
}

func init() {
inputs.Add("ipvs", func() telegraf.Input { return &IPVS{} })
inputs.Add("ipvs", func() telegraf.Input {
logrus.InstallHook()
return &IPVS{}
})
}

0 comments on commit 843d90d

Please sign in to comment.