Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ipvs input error strings #6530

Merged
merged 3 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{}
})
}