Improve ipvs input error strings and logging (#6530)

This commit is contained in:
Daniel Nelson 2019-10-21 21:27:05 -07:00 committed by GitHub
parent 82ba2cd52a
commit 17c4e0b06f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 5 deletions

1
Gopkg.lock generated
View File

@ -1774,6 +1774,7 @@
"github.com/shirou/gopsutil/mem",
"github.com/shirou/gopsutil/net",
"github.com/shirou/gopsutil/process",
"github.com/sirupsen/logrus",
"github.com/soniah/gosnmp",
"github.com/streadway/amqp",
"github.com/stretchr/testify/assert",

View File

@ -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
}

View File

@ -3,7 +3,6 @@
package ipvs
import (
"errors"
"fmt"
"math/bits"
"strconv"
@ -11,6 +10,7 @@ import (
"github.com/docker/libnetwork/ipvs"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/common/logrus"
"github.com/influxdata/telegraf/plugins/inputs"
)
@ -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
}
@ -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{}{
@ -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
}
@ -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{}
})
}