shim logger improvements (#7865)
This commit is contained in:
parent
6e5c72f743
commit
569584d7df
|
|
@ -79,7 +79,7 @@ func logName(pluginType, name, alias string) string {
|
||||||
return pluginType + "." + name + "::" + alias
|
return pluginType + "." + name + "::" + alias
|
||||||
}
|
}
|
||||||
|
|
||||||
func setLogIfExist(i interface{}, log telegraf.Logger) {
|
func setLoggerOnPlugin(i interface{}, log telegraf.Logger) {
|
||||||
valI := reflect.ValueOf(i)
|
valI := reflect.ValueOf(i)
|
||||||
|
|
||||||
if valI.Type().Kind() != reflect.Ptr {
|
if valI.Type().Kind() != reflect.Ptr {
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ func NewRunningAggregator(aggregator telegraf.Aggregator, config *AggregatorConf
|
||||||
aggErrorsRegister.Incr(1)
|
aggErrorsRegister.Incr(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
setLogIfExist(aggregator, logger)
|
setLoggerOnPlugin(aggregator, logger)
|
||||||
|
|
||||||
return &RunningAggregator{
|
return &RunningAggregator{
|
||||||
Aggregator: aggregator,
|
Aggregator: aggregator,
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ func NewRunningInput(input telegraf.Input, config *InputConfig) *RunningInput {
|
||||||
inputErrorsRegister.Incr(1)
|
inputErrorsRegister.Incr(1)
|
||||||
GlobalGatherErrors.Incr(1)
|
GlobalGatherErrors.Incr(1)
|
||||||
})
|
})
|
||||||
setLogIfExist(input, logger)
|
setLoggerOnPlugin(input, logger)
|
||||||
|
|
||||||
return &RunningInput{
|
return &RunningInput{
|
||||||
Input: input,
|
Input: input,
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,7 @@ func NewRunningOutput(
|
||||||
logger.OnErr(func() {
|
logger.OnErr(func() {
|
||||||
writeErrorsRegister.Incr(1)
|
writeErrorsRegister.Incr(1)
|
||||||
})
|
})
|
||||||
setLogIfExist(output, logger)
|
setLoggerOnPlugin(output, logger)
|
||||||
|
|
||||||
if config.MetricBufferLimit > 0 {
|
if config.MetricBufferLimit > 0 {
|
||||||
bufferLimit = config.MetricBufferLimit
|
bufferLimit = config.MetricBufferLimit
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ func NewRunningProcessor(processor telegraf.StreamingProcessor, config *Processo
|
||||||
logger.OnErr(func() {
|
logger.OnErr(func() {
|
||||||
processErrorsRegister.Incr(1)
|
processErrorsRegister.Incr(1)
|
||||||
})
|
})
|
||||||
setLogIfExist(processor, logger)
|
setLoggerOnPlugin(processor, logger)
|
||||||
|
|
||||||
return &RunningProcessor{
|
return &RunningProcessor{
|
||||||
Processor: processor,
|
Processor: processor,
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
|
|
@ -152,14 +153,17 @@ func DefaultImportedPlugins() (config, error) {
|
||||||
Outputs: map[string][]toml.Primitive{},
|
Outputs: map[string][]toml.Primitive{},
|
||||||
}
|
}
|
||||||
for name := range inputs.Inputs {
|
for name := range inputs.Inputs {
|
||||||
|
log.Println("No config found. Loading default config for plugin", name)
|
||||||
conf.Inputs[name] = []toml.Primitive{}
|
conf.Inputs[name] = []toml.Primitive{}
|
||||||
return conf, nil
|
return conf, nil
|
||||||
}
|
}
|
||||||
for name := range processors.Processors {
|
for name := range processors.Processors {
|
||||||
|
log.Println("No config found. Loading default config for plugin", name)
|
||||||
conf.Processors[name] = []toml.Primitive{}
|
conf.Processors[name] = []toml.Primitive{}
|
||||||
return conf, nil
|
return conf, nil
|
||||||
}
|
}
|
||||||
for name := range outputs.Outputs {
|
for name := range outputs.Outputs {
|
||||||
|
log.Println("No config found. Loading default config for plugin", name)
|
||||||
conf.Outputs[name] = []toml.Primitive{}
|
conf.Outputs[name] = []toml.Primitive{}
|
||||||
return conf, nil
|
return conf, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ import (
|
||||||
|
|
||||||
// AddInput adds the input to the shim. Later calls to Run() will run this input.
|
// AddInput adds the input to the shim. Later calls to Run() will run this input.
|
||||||
func (s *Shim) AddInput(input telegraf.Input) error {
|
func (s *Shim) AddInput(input telegraf.Input) error {
|
||||||
|
setLoggerOnPlugin(input, NewLogger())
|
||||||
if p, ok := input.(telegraf.Initializer); ok {
|
if p, ok := input.(telegraf.Initializer); ok {
|
||||||
err := p.Init()
|
err := p.Init()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -57,13 +58,16 @@ func (s *Shim) RunInput(pollInterval time.Duration) error {
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
scanner := bufio.NewScanner(s.stdin)
|
go func() {
|
||||||
for scanner.Scan() {
|
scanner := bufio.NewScanner(s.stdin)
|
||||||
// push a non-blocking message to trigger metric collection.
|
for scanner.Scan() {
|
||||||
s.pushCollectMetricsRequest()
|
// push a non-blocking message to trigger metric collection.
|
||||||
}
|
s.pushCollectMetricsRequest()
|
||||||
|
}
|
||||||
|
|
||||||
|
cancel() // cancel gracefully stops gathering
|
||||||
|
}()
|
||||||
|
|
||||||
cancel() // cancel gracefully stops gathering
|
|
||||||
wg.Wait() // wait for writing to stdout to finish
|
wg.Wait() // wait for writing to stdout to finish
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,89 @@
|
||||||
|
package shim
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/influxdata/telegraf"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
log.SetOutput(os.Stderr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Logger defines a logging structure for plugins.
|
||||||
|
// external plugins can only ever write to stderr and writing to stdout
|
||||||
|
// would interfere with input/processor writing out of metrics.
|
||||||
|
type Logger struct{}
|
||||||
|
|
||||||
|
// NewLogger creates a new logger instance
|
||||||
|
func NewLogger() *Logger {
|
||||||
|
return &Logger{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Errorf logs an error message, patterned after log.Printf.
|
||||||
|
func (l *Logger) Errorf(format string, args ...interface{}) {
|
||||||
|
log.Printf("E! "+format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error logs an error message, patterned after log.Print.
|
||||||
|
func (l *Logger) Error(args ...interface{}) {
|
||||||
|
log.Print("E! ", fmt.Sprint(args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debugf logs a debug message, patterned after log.Printf.
|
||||||
|
func (l *Logger) Debugf(format string, args ...interface{}) {
|
||||||
|
log.Printf("D! "+format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debug logs a debug message, patterned after log.Print.
|
||||||
|
func (l *Logger) Debug(args ...interface{}) {
|
||||||
|
log.Print("D! ", fmt.Sprint(args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warnf logs a warning message, patterned after log.Printf.
|
||||||
|
func (l *Logger) Warnf(format string, args ...interface{}) {
|
||||||
|
log.Printf("W! "+format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warn logs a warning message, patterned after log.Print.
|
||||||
|
func (l *Logger) Warn(args ...interface{}) {
|
||||||
|
log.Print("W! ", fmt.Sprint(args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Infof logs an information message, patterned after log.Printf.
|
||||||
|
func (l *Logger) Infof(format string, args ...interface{}) {
|
||||||
|
log.Printf("I! "+format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info logs an information message, patterned after log.Print.
|
||||||
|
func (l *Logger) Info(args ...interface{}) {
|
||||||
|
log.Print("I! ", fmt.Sprint(args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// setLoggerOnPlugin injects the logger into the plugin,
|
||||||
|
// if it defines Log telegraf.Logger. This is sort of like SetLogger but using
|
||||||
|
// reflection instead of forcing the plugin author to define the function for it
|
||||||
|
func setLoggerOnPlugin(i interface{}, log telegraf.Logger) {
|
||||||
|
valI := reflect.ValueOf(i)
|
||||||
|
|
||||||
|
if valI.Type().Kind() != reflect.Ptr {
|
||||||
|
valI = reflect.New(reflect.TypeOf(i))
|
||||||
|
}
|
||||||
|
|
||||||
|
field := valI.Elem().FieldByName("Log")
|
||||||
|
if !field.IsValid() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch field.Type().String() {
|
||||||
|
case "telegraf.Logger":
|
||||||
|
if field.CanSet() {
|
||||||
|
field.Set(reflect.ValueOf(log))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
@ -10,6 +10,7 @@ import (
|
||||||
|
|
||||||
// AddOutput adds the input to the shim. Later calls to Run() will run this.
|
// AddOutput adds the input to the shim. Later calls to Run() will run this.
|
||||||
func (s *Shim) AddOutput(output telegraf.Output) error {
|
func (s *Shim) AddOutput(output telegraf.Output) error {
|
||||||
|
setLoggerOnPlugin(output, NewLogger())
|
||||||
if p, ok := output.(telegraf.Initializer); ok {
|
if p, ok := output.(telegraf.Initializer); ok {
|
||||||
err := p.Init()
|
err := p.Init()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -14,12 +14,14 @@ import (
|
||||||
|
|
||||||
// AddProcessor adds the processor to the shim. Later calls to Run() will run this.
|
// AddProcessor adds the processor to the shim. Later calls to Run() will run this.
|
||||||
func (s *Shim) AddProcessor(processor telegraf.Processor) error {
|
func (s *Shim) AddProcessor(processor telegraf.Processor) error {
|
||||||
|
setLoggerOnPlugin(processor, NewLogger())
|
||||||
p := processors.NewStreamingProcessorFromProcessor(processor)
|
p := processors.NewStreamingProcessorFromProcessor(processor)
|
||||||
return s.AddStreamingProcessor(p)
|
return s.AddStreamingProcessor(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddStreamingProcessor adds the processor to the shim. Later calls to Run() will run this.
|
// AddStreamingProcessor adds the processor to the shim. Later calls to Run() will run this.
|
||||||
func (s *Shim) AddStreamingProcessor(processor telegraf.StreamingProcessor) error {
|
func (s *Shim) AddStreamingProcessor(processor telegraf.StreamingProcessor) error {
|
||||||
|
setLoggerOnPlugin(processor, NewLogger())
|
||||||
if p, ok := processor.(telegraf.Initializer); ok {
|
if p, ok := processor.(telegraf.Initializer); ok {
|
||||||
err := p.Init()
|
err := p.Init()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -49,8 +49,15 @@ type Shim struct {
|
||||||
stderr io.Writer
|
stderr io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
oldpkg = "github.com/influxdata/telegraf/plugins/inputs/execd/shim"
|
||||||
|
newpkg = "github.com/influxdata/telegraf/plugins/common/shim"
|
||||||
|
)
|
||||||
|
|
||||||
// New creates a new shim interface
|
// New creates a new shim interface
|
||||||
func New() *Shim {
|
func New() *Shim {
|
||||||
|
fmt.Fprintf(os.Stderr, "%s is deprecated; please change your import to %s\n",
|
||||||
|
oldpkg, newpkg)
|
||||||
return &Shim{
|
return &Shim{
|
||||||
stdin: os.Stdin,
|
stdin: os.Stdin,
|
||||||
stdout: os.Stdout,
|
stdout: os.Stdout,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue