Skip to content

Commit 13085dd

Browse files
authored
Improve ipvs input error strings and logging (influxdata#6530)
1 parent 0b31e35 commit 13085dd

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

Gopkg.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/common/logrus/hook.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package logrus
2+
3+
import (
4+
"io/ioutil"
5+
"log"
6+
"strings"
7+
"sync"
8+
9+
"github.com/sirupsen/logrus"
10+
)
11+
12+
var once sync.Once
13+
14+
type LogHook struct {
15+
}
16+
17+
// Install a logging hook into the logrus standard logger, diverting all logs
18+
// through the Telegraf logger at debug level. This is useful for libraries
19+
// that directly log to the logrus system without providing an override method.
20+
func InstallHook() {
21+
once.Do(func() {
22+
logrus.SetOutput(ioutil.Discard)
23+
logrus.AddHook(&LogHook{})
24+
})
25+
}
26+
27+
func (h *LogHook) Fire(entry *logrus.Entry) error {
28+
msg := strings.ReplaceAll(entry.Message, "\n", " ")
29+
log.Print("D! [logrus] ", msg)
30+
return nil
31+
}
32+
33+
func (h *LogHook) Levels() []logrus.Level {
34+
return logrus.AllLevels
35+
}

plugins/inputs/ipvs/ipvs.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
package ipvs
44

55
import (
6-
"errors"
76
"fmt"
87
"math/bits"
98
"strconv"
109
"syscall"
1110

1211
"github.com/docker/libnetwork/ipvs"
1312
"github.com/influxdata/telegraf"
13+
"github.com/influxdata/telegraf/plugins/common/logrus"
1414
"github.com/influxdata/telegraf/plugins/inputs"
1515
)
1616

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

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

@@ -148,5 +148,8 @@ func addressFamilyToString(af uint16) string {
148148
}
149149

150150
func init() {
151-
inputs.Add("ipvs", func() telegraf.Input { return &IPVS{} })
151+
inputs.Add("ipvs", func() telegraf.Input {
152+
logrus.InstallHook()
153+
return &IPVS{}
154+
})
152155
}

0 commit comments

Comments
 (0)