From 9a43728ae0c986502570efd805b42f598715c4ec Mon Sep 17 00:00:00 2001 From: greg linton Date: Wed, 24 Jul 2019 14:06:44 -0600 Subject: [PATCH 01/15] Add ability to label inputs for logging --- agent/accumulator.go | 3 ++- agent/agent.go | 6 +++--- internal/config/config.go | 9 +++++++++ internal/models/running_aggregator.go | 8 ++++++-- internal/models/running_input.go | 8 ++++++++ 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/agent/accumulator.go b/agent/accumulator.go index 9e0bb11ca0cb8..929a904426a20 100644 --- a/agent/accumulator.go +++ b/agent/accumulator.go @@ -15,6 +15,7 @@ var ( type MetricMaker interface { Name() string + LogName() string MakeMetric(metric telegraf.Metric) telegraf.Metric } @@ -111,7 +112,7 @@ func (ac *accumulator) AddError(err error) { return } NErrors.Incr(1) - log.Printf("E! [%s]: Error in plugin: %v", ac.maker.Name(), err) + log.Printf("E! [%s]: Error in plugin: %v", ac.maker.LogName(), err) } func (ac *accumulator) SetPrecision(precision time.Duration) { diff --git a/agent/agent.go b/agent/agent.go index 5421543881097..54d9018344347 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -334,7 +334,7 @@ func (a *Agent) gatherOnce( return err case <-ticker.C: log.Printf("W! [agent] input %q did not complete within its interval", - input.Name()) + input.LogName()) } } } @@ -690,7 +690,7 @@ func (a *Agent) startServiceInputs( err := si.Start(acc) if err != nil { log.Printf("E! [agent] Service for input %s failed to start: %v", - input.Name(), err) + input.LogName(), err) for _, si := range started { si.Stop() @@ -742,7 +742,7 @@ func panicRecover(input *models.RunningInput) { trace := make([]byte, 2048) runtime.Stack(trace, true) log.Printf("E! FATAL: Input [%s] panicked: %s, Stack:\n%s\n", - input.Name(), err, trace) + input.LogName(), err, trace) log.Println("E! PLEASE REPORT THIS PANIC ON GITHUB with " + "stack trace, configuration, and OS information: " + "https://github.com/influxdata/telegraf/issues/new/choose") diff --git a/internal/config/config.go b/internal/config/config.go index a5315b9b6b5bc..33eb891506f47 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1319,6 +1319,14 @@ func buildInput(name string, tbl *ast.Table) (*models.InputConfig, error) { } } + if node, ok := tbl.Fields["log_id"]; ok { + if kv, ok := node.(*ast.KeyValue); ok { + if str, ok := kv.Value.(*ast.String); ok { + cp.LogID = str.Value + } + } + } + cp.Tags = make(map[string]string) if node, ok := tbl.Fields["tags"]; ok { if subtbl, ok := node.(*ast.Table); ok { @@ -1331,6 +1339,7 @@ func buildInput(name string, tbl *ast.Table) (*models.InputConfig, error) { delete(tbl.Fields, "name_prefix") delete(tbl.Fields, "name_suffix") delete(tbl.Fields, "name_override") + delete(tbl.Fields, "log_id") delete(tbl.Fields, "interval") delete(tbl.Fields, "tags") var err error diff --git a/internal/models/running_aggregator.go b/internal/models/running_aggregator.go index 8bd983eefe2dc..cff1d262a0f51 100644 --- a/internal/models/running_aggregator.go +++ b/internal/models/running_aggregator.go @@ -71,6 +71,10 @@ func (r *RunningAggregator) Name() string { return "aggregators." + r.Config.Name } +func (r *RunningAggregator) LogName() string { + return r.Name() +} + func (r *RunningAggregator) Init() error { if p, ok := r.Aggregator.(telegraf.Initializer); ok { err := p.Init() @@ -92,7 +96,7 @@ func (r *RunningAggregator) EndPeriod() time.Time { func (r *RunningAggregator) UpdateWindow(start, until time.Time) { r.periodStart = start r.periodEnd = until - log.Printf("D! [%s] Updated aggregation range [%s, %s]", r.Name(), start, until) + log.Printf("D! [%s] Updated aggregation range [%s, %s]", r.LogName(), start, until) } func (r *RunningAggregator) MakeMetric(metric telegraf.Metric) telegraf.Metric { @@ -137,7 +141,7 @@ func (r *RunningAggregator) Add(m telegraf.Metric) bool { if m.Time().Before(r.periodStart) || m.Time().After(r.periodEnd.Add(r.Config.Delay)) { log.Printf("D! [%s] metric is outside aggregation window; discarding. %s: m: %s e: %s", - r.Name(), m.Time(), r.periodStart, r.periodEnd) + r.LogName(), m.Time(), r.periodStart, r.periodEnd) r.MetricsDropped.Incr(1) return r.Config.DropOriginal } diff --git a/internal/models/running_input.go b/internal/models/running_input.go index 73c14fc0fa9e1..bbd928f54cfd7 100644 --- a/internal/models/running_input.go +++ b/internal/models/running_input.go @@ -39,6 +39,7 @@ func NewRunningInput(input telegraf.Input, config *InputConfig) *RunningInput { // InputConfig is the common config for all inputs. type InputConfig struct { Name string + LogID string Interval time.Duration NameOverride string @@ -52,6 +53,13 @@ func (r *RunningInput) Name() string { return "inputs." + r.Config.Name } +func (r *RunningInput) LogName() string { + if r.Config.LogID == "" { + return r.Name() + } + return r.Name() + "::" + r.Config.LogID +} + func (r *RunningInput) metricFiltered(metric telegraf.Metric) { metric.Drop() } From 6c3bd72a58aebcc5fef237f6f9114102d16fe35a Mon Sep 17 00:00:00 2001 From: greg linton Date: Thu, 25 Jul 2019 12:26:04 -0600 Subject: [PATCH 02/15] Enhance log alias support --- agent/agent.go | 20 +++++++------- internal/config/config.go | 38 ++++++++++++++++++++++----- internal/models/running_aggregator.go | 6 ++++- internal/models/running_input.go | 6 ++--- internal/models/running_output.go | 29 +++++++++++++------- internal/models/running_processor.go | 14 ++++++++-- 6 files changed, 82 insertions(+), 31 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 54d9018344347..dce08eccaf57e 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -333,7 +333,7 @@ func (a *Agent) gatherOnce( case err := <-done: return err case <-ticker.C: - log.Printf("W! [agent] input %q did not complete within its interval", + log.Printf("W! [agent] [%s] did not complete within its interval", input.LogName()) } } @@ -547,7 +547,7 @@ func (a *Agent) flush( logError := func(err error) { if err != nil { - log.Printf("E! [agent] Error writing to output [%s]: %v", output.Name, err) + log.Printf("E! [agent] Error writing to [%s]: %v", output.LogName(), err) } } @@ -599,8 +599,8 @@ func (a *Agent) flushOnce( output.LogBufferStatus() return err case <-ticker.C: - log.Printf("W! [agent] output %q did not complete within its flush interval", - output.Name) + log.Printf("W! [agent] [%q] did not complete within its flush interval", + output.LogName()) output.LogBufferStatus() } } @@ -643,11 +643,11 @@ func (a *Agent) initPlugins() error { // connectOutputs connects to all outputs. func (a *Agent) connectOutputs(ctx context.Context) error { for _, output := range a.Config.Outputs { - log.Printf("D! [agent] Attempting connection to output: %s\n", output.Name) + log.Printf("D! [agent] Attempting connection to [%s]", output.LogName()) err := output.Output.Connect() if err != nil { - log.Printf("E! [agent] Failed to connect to output %s, retrying in 15s, "+ - "error was '%s' \n", output.Name, err) + log.Printf("E! [agent] Failed to connect to [%s], retrying in 15s, "+ + "error was '%s'", output.LogName(), err) err := internal.SleepContext(ctx, 15*time.Second) if err != nil { @@ -659,7 +659,7 @@ func (a *Agent) connectOutputs(ctx context.Context) error { return err } } - log.Printf("D! [agent] Successfully connected to output: %s\n", output.Name) + log.Printf("D! [agent] Successfully connected to %s", output.LogName()) } return nil } @@ -689,7 +689,7 @@ func (a *Agent) startServiceInputs( err := si.Start(acc) if err != nil { - log.Printf("E! [agent] Service for input %s failed to start: %v", + log.Printf("E! [agent] Service for [%s] failed to start: %v", input.LogName(), err) for _, si := range started { @@ -741,7 +741,7 @@ func panicRecover(input *models.RunningInput) { if err := recover(); err != nil { trace := make([]byte, 2048) runtime.Stack(trace, true) - log.Printf("E! FATAL: Input [%s] panicked: %s, Stack:\n%s\n", + log.Printf("E! FATAL: [%s] panicked: %s, Stack:\n%s", input.LogName(), err, trace) log.Println("E! PLEASE REPORT THIS PANIC ON GITHUB with " + "stack trace, configuration, and OS information: " + diff --git a/internal/config/config.go b/internal/config/config.go index 33eb891506f47..dcf8c8753aef5 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -187,7 +187,7 @@ func (c *Config) AggregatorNames() []string { func (c *Config) ProcessorNames() []string { var name []string for _, processor := range c.Processors { - name = append(name, processor.Name) + name = append(name, processor.Config.Name) } return name } @@ -196,7 +196,7 @@ func (c *Config) ProcessorNames() []string { func (c *Config) OutputNames() []string { var name []string for _, output := range c.Outputs { - name = append(name, output.Name) + name = append(name, output.Config.Name) } return name } @@ -920,7 +920,6 @@ func (c *Config) addProcessor(name string, table *ast.Table) error { } rf := &models.RunningProcessor{ - Name: name, Processor: processor, Config: processorConfig, } @@ -1089,6 +1088,14 @@ func buildAggregator(name string, tbl *ast.Table) (*models.AggregatorConfig, err } } + if node, ok := tbl.Fields["alias"]; ok { + if kv, ok := node.(*ast.KeyValue); ok { + if str, ok := kv.Value.(*ast.String); ok { + conf.Alias = str.Value + } + } + } + conf.Tags = make(map[string]string) if node, ok := tbl.Fields["tags"]; ok { if subtbl, ok := node.(*ast.Table); ok { @@ -1104,6 +1111,7 @@ func buildAggregator(name string, tbl *ast.Table) (*models.AggregatorConfig, err delete(tbl.Fields, "name_prefix") delete(tbl.Fields, "name_suffix") delete(tbl.Fields, "name_override") + delete(tbl.Fields, "alias") delete(tbl.Fields, "tags") var err error conf.Filter, err = buildFilter(tbl) @@ -1131,6 +1139,15 @@ func buildProcessor(name string, tbl *ast.Table) (*models.ProcessorConfig, error } } + if node, ok := tbl.Fields["alias"]; ok { + if kv, ok := node.(*ast.KeyValue); ok { + if str, ok := kv.Value.(*ast.String); ok { + conf.Alias = str.Value + } + } + } + + delete(tbl.Fields, "alias") delete(tbl.Fields, "order") var err error conf.Filter, err = buildFilter(tbl) @@ -1319,10 +1336,10 @@ func buildInput(name string, tbl *ast.Table) (*models.InputConfig, error) { } } - if node, ok := tbl.Fields["log_id"]; ok { + if node, ok := tbl.Fields["alias"]; ok { if kv, ok := node.(*ast.KeyValue); ok { if str, ok := kv.Value.(*ast.String); ok { - cp.LogID = str.Value + cp.Alias = str.Value } } } @@ -1339,7 +1356,7 @@ func buildInput(name string, tbl *ast.Table) (*models.InputConfig, error) { delete(tbl.Fields, "name_prefix") delete(tbl.Fields, "name_suffix") delete(tbl.Fields, "name_override") - delete(tbl.Fields, "log_id") + delete(tbl.Fields, "alias") delete(tbl.Fields, "interval") delete(tbl.Fields, "tags") var err error @@ -2001,9 +2018,18 @@ func buildOutput(name string, tbl *ast.Table) (*models.OutputConfig, error) { } } + if node, ok := tbl.Fields["alias"]; ok { + if kv, ok := node.(*ast.KeyValue); ok { + if str, ok := kv.Value.(*ast.String); ok { + oc.Alias = str.Value + } + } + } + delete(tbl.Fields, "flush_interval") delete(tbl.Fields, "metric_buffer_limit") delete(tbl.Fields, "metric_batch_size") + delete(tbl.Fields, "alias") return oc, nil } diff --git a/internal/models/running_aggregator.go b/internal/models/running_aggregator.go index cff1d262a0f51..9e5ee591e0e6c 100644 --- a/internal/models/running_aggregator.go +++ b/internal/models/running_aggregator.go @@ -56,6 +56,7 @@ func NewRunningAggregator( // AggregatorConfig is the common config for all aggregators. type AggregatorConfig struct { Name string + Alias string DropOriginal bool Period time.Duration Delay time.Duration @@ -72,7 +73,10 @@ func (r *RunningAggregator) Name() string { } func (r *RunningAggregator) LogName() string { - return r.Name() + if r.Config.Alias == "" { + return r.Name() + } + return r.Name() + "::" + r.Config.Alias } func (r *RunningAggregator) Init() error { diff --git a/internal/models/running_input.go b/internal/models/running_input.go index bbd928f54cfd7..58f029932dd8d 100644 --- a/internal/models/running_input.go +++ b/internal/models/running_input.go @@ -39,7 +39,7 @@ func NewRunningInput(input telegraf.Input, config *InputConfig) *RunningInput { // InputConfig is the common config for all inputs. type InputConfig struct { Name string - LogID string + Alias string Interval time.Duration NameOverride string @@ -54,10 +54,10 @@ func (r *RunningInput) Name() string { } func (r *RunningInput) LogName() string { - if r.Config.LogID == "" { + if r.Config.Alias == "" { return r.Name() } - return r.Name() + "::" + r.Config.LogID + return r.Name() + "::" + r.Config.Alias } func (r *RunningInput) metricFiltered(metric telegraf.Metric) { diff --git a/internal/models/running_output.go b/internal/models/running_output.go index 438ecd480478a..120036a172ea6 100644 --- a/internal/models/running_output.go +++ b/internal/models/running_output.go @@ -21,6 +21,7 @@ const ( // OutputConfig containing name and filter type OutputConfig struct { Name string + Alias string Filter Filter FlushInterval time.Duration @@ -34,7 +35,6 @@ type RunningOutput struct { newMetricsCount int64 droppedMetrics int64 - Name string Output telegraf.Output Config *OutputConfig MetricBufferLimit int @@ -69,8 +69,8 @@ func NewRunningOutput( if batchSize == 0 { batchSize = DEFAULT_METRIC_BATCH_SIZE } + ro := &RunningOutput{ - Name: name, buffer: NewBuffer(name, bufferLimit), BatchReady: make(chan time.Time, 1), Output: output, @@ -92,6 +92,17 @@ func NewRunningOutput( return ro } +func (ro *RunningOutput) Name() string { + return "outputs." + ro.Config.Name +} + +func (ro *RunningOutput) LogName() string { + if ro.Config.Alias == "" { + return ro.Name() + } + return ro.Name() + "::" + ro.Config.Alias +} + func (ro *RunningOutput) metricFiltered(metric telegraf.Metric) { ro.MetricsFiltered.Incr(1) metric.Drop() @@ -195,15 +206,15 @@ func (ro *RunningOutput) WriteBatch() error { func (ro *RunningOutput) Close() { err := ro.Output.Close() if err != nil { - log.Printf("E! [outputs.%s] Error closing output: %v", ro.Name, err) + log.Printf("E! [%s] Error closing output: %v", ro.LogName(), err) } } func (ro *RunningOutput) write(metrics []telegraf.Metric) error { dropped := atomic.LoadInt64(&ro.droppedMetrics) if dropped > 0 { - log.Printf("W! [outputs.%s] Metric buffer overflow; %d metrics have been dropped", - ro.Name, dropped) + log.Printf("W! [%s] Metric buffer overflow; %d metrics have been dropped", + ro.LogName(), dropped) atomic.StoreInt64(&ro.droppedMetrics, 0) } @@ -213,14 +224,14 @@ func (ro *RunningOutput) write(metrics []telegraf.Metric) error { ro.WriteTime.Incr(elapsed.Nanoseconds()) if err == nil { - log.Printf("D! [outputs.%s] wrote batch of %d metrics in %s\n", - ro.Name, len(metrics), elapsed) + log.Printf("D! [%s] wrote batch of %d metrics in %s", + ro.LogName(), len(metrics), elapsed) } return err } func (ro *RunningOutput) LogBufferStatus() { nBuffer := ro.buffer.Len() - log.Printf("D! [outputs.%s] buffer fullness: %d / %d metrics. ", - ro.Name, nBuffer, ro.MetricBufferLimit) + log.Printf("D! [%s] buffer fullness: %d / %d metrics", + ro.LogName(), nBuffer, ro.MetricBufferLimit) } diff --git a/internal/models/running_processor.go b/internal/models/running_processor.go index 90d32fde5bcfe..b5b4704e85c61 100644 --- a/internal/models/running_processor.go +++ b/internal/models/running_processor.go @@ -7,8 +7,6 @@ import ( ) type RunningProcessor struct { - Name string - sync.Mutex Processor telegraf.Processor Config *ProcessorConfig @@ -23,10 +21,22 @@ func (rp RunningProcessors) Less(i, j int) bool { return rp[i].Config.Order < rp // FilterConfig containing a name and filter type ProcessorConfig struct { Name string + Alias string Order int64 Filter Filter } +func (rp *RunningProcessor) Name() string { + return "processors." + rp.Config.Name +} + +func (rp *RunningProcessor) LogName() string { + if rp.Config.Alias == "" { + return rp.Name() + } + return rp.Name() + "::" + rp.Config.Alias +} + func (rp *RunningProcessor) metricFiltered(metric telegraf.Metric) { metric.Drop() } From 30590b801c8472c33da58a2e10b1a76523d928cc Mon Sep 17 00:00:00 2001 From: greg linton Date: Wed, 31 Jul 2019 15:12:41 -0600 Subject: [PATCH 03/15] Update testMetricMakers --- agent/accumulator_test.go | 4 ++++ plugins/inputs/cloud_pubsub_push/pubsub_push_test.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/agent/accumulator_test.go b/agent/accumulator_test.go index 933821701c5e5..c84948ba97f9e 100644 --- a/agent/accumulator_test.go +++ b/agent/accumulator_test.go @@ -147,6 +147,10 @@ func (tm *TestMetricMaker) Name() string { return "TestPlugin" } +func (tm *TestMetricMaker) LogName() string { + return tm.Name() +} + func (tm *TestMetricMaker) MakeMetric(metric telegraf.Metric) telegraf.Metric { return metric } diff --git a/plugins/inputs/cloud_pubsub_push/pubsub_push_test.go b/plugins/inputs/cloud_pubsub_push/pubsub_push_test.go index 57734c70554f3..45a304e602e24 100644 --- a/plugins/inputs/cloud_pubsub_push/pubsub_push_test.go +++ b/plugins/inputs/cloud_pubsub_push/pubsub_push_test.go @@ -183,6 +183,10 @@ func (tm *testMetricMaker) Name() string { return "TestPlugin" } +func (tm *testMetricMaker) LogName() string { + return tm.Name() +} + func (tm *testMetricMaker) MakeMetric(metric telegraf.Metric) telegraf.Metric { return metric } From a87dbefddf52a0b8e2f8df9b74de058e816b80ba Mon Sep 17 00:00:00 2001 From: greg linton Date: Thu, 1 Aug 2019 18:54:30 -0600 Subject: [PATCH 04/15] Begin configuring per plugin logger --- docs/AGGREGATORS.md | 2 +- docs/INPUTS.md | 2 +- docs/OUTPUTS.md | 2 +- docs/PROCESSORS.md | 2 +- input.go | 55 +++++++++++++++++++- internal/models/running_aggregator.go | 2 +- internal/models/running_input.go | 2 +- internal/models/running_output.go | 2 +- internal/models/running_processor.go | 2 +- plugins/inputs/activemq/activemq.go | 2 +- plugins/inputs/activemq/activemq_test.go | 9 ++-- plugins/inputs/docker_log/docker_log.go | 2 +- plugins/inputs/docker_log/docker_log_test.go | 2 +- plugins/inputs/exec/exec.go | 11 +++- plugins/inputs/fireboard/fireboard.go | 2 +- plugins/inputs/http/http.go | 2 +- plugins/inputs/http/http_test.go | 11 ++-- plugins/inputs/ping/ping.go | 2 +- plugins/inputs/x509_cert/x509_cert.go | 2 +- plugins/inputs/x509_cert/x509_cert_test.go | 10 ++-- plugins/outputs/influxdb/http.go | 15 ++++-- plugins/outputs/influxdb/influxdb.go | 19 +++++-- plugins/outputs/influxdb/influxdb_test.go | 6 +++ plugins/outputs/influxdb/udp.go | 9 +++- 24 files changed, 132 insertions(+), 43 deletions(-) diff --git a/docs/AGGREGATORS.md b/docs/AGGREGATORS.md index a5930a3e0df6d..4af69c27384a2 100644 --- a/docs/AGGREGATORS.md +++ b/docs/AGGREGATORS.md @@ -52,7 +52,7 @@ var sampleConfig = ` drop_original = false ` -func (m *Min) Init() error { +func (m *Min) Init(telegraf.PluginConfig) error { return nil } diff --git a/docs/INPUTS.md b/docs/INPUTS.md index f8e906f318fee..0ce768020a37f 100644 --- a/docs/INPUTS.md +++ b/docs/INPUTS.md @@ -52,7 +52,7 @@ func (s *Simple) SampleConfig() string { ` } -func (s *Simple) Init() error { +func (s *Simple) Init(telegraf.PluginConfig) error { return nil } diff --git a/docs/OUTPUTS.md b/docs/OUTPUTS.md index 9d89491cc39d7..ab3577870a43e 100644 --- a/docs/OUTPUTS.md +++ b/docs/OUTPUTS.md @@ -43,7 +43,7 @@ func (s *Simple) SampleConfig() string { ` } -func (s *Simple) Init() error { +func (s *Simple) Init(telegraf.PluginConfig) error { return nil } diff --git a/docs/PROCESSORS.md b/docs/PROCESSORS.md index 6ea82fdae3309..30e99fd11cdeb 100644 --- a/docs/PROCESSORS.md +++ b/docs/PROCESSORS.md @@ -46,7 +46,7 @@ func (p *Printer) Description() string { return "Print all metrics that pass through this filter." } -func (p *Printer) Init() error { +func (p *Printer) Init(telegraf.PluginConfig) error { return nil } diff --git a/input.go b/input.go index ee47bc3470ad9..0de0c844b80a0 100644 --- a/input.go +++ b/input.go @@ -1,12 +1,14 @@ package telegraf +import "log" + // Initializer is an interface that all plugin types: Inputs, Outputs, // Processors, and Aggregators can optionally implement to initialize the // plugin. type Initializer interface { // Init performs one time setup of the plugin and returns an error if the // configuration is invalid. - Init() error + Init(PluginConfig) error } type Input interface { @@ -31,3 +33,54 @@ type ServiceInput interface { // Stop stops the services and closes any necessary channels and connections Stop() } + +// PluginConfig contains individualized plugin configuration. +type PluginConfig struct { + Logger Logger +} + +// Logger defines a logging structure for plugins. +type Logger struct { + Name string // Name is the plugin name, will be printed in the `[]`. +} + +// Errorf logs an error message, patterned after log.Printf. +func (l Logger) Errorf(format string, args ...interface{}) { + // todo: keep tally of errors from plugins + log.Printf("E! ["+l.Name+"] "+format, args...) +} + +// Error logs an error message, patterned after log.Print. +func (l Logger) Error(args ...interface{}) { + log.Print(append([]interface{}{"E! [" + l.Name + "] "}, args...)...) +} + +// Debugf logs a debug message, patterned after log.Printf. +func (l Logger) Debugf(format string, args ...interface{}) { + log.Printf("D! ["+l.Name+"] "+format, args...) +} + +// Debug logs a debug message, patterned after log.Print. +func (l Logger) Debug(args ...interface{}) { + log.Print(append([]interface{}{"D! [" + l.Name + "] "}, args...)...) +} + +// Warnf logs a warning message, patterned after log.Printf. +func (l Logger) Warnf(format string, args ...interface{}) { + log.Printf("W! ["+l.Name+"] "+format, args...) +} + +// Warn logs a warning message, patterned after log.Print. +func (l Logger) Warn(args ...interface{}) { + log.Print(append([]interface{}{"W! [" + l.Name + "] "}, args...)...) +} + +// Infof logs an information message, patterned after log.Printf. +func (l Logger) Infof(format string, args ...interface{}) { + log.Printf("I! ["+l.Name+"] "+format, args...) +} + +// Info logs an information message, patterned after log.Print. +func (l Logger) Info(args ...interface{}) { + log.Print(append([]interface{}{"I! [" + l.Name + "] "}, args...)...) +} diff --git a/internal/models/running_aggregator.go b/internal/models/running_aggregator.go index 9e5ee591e0e6c..e9d68258f1065 100644 --- a/internal/models/running_aggregator.go +++ b/internal/models/running_aggregator.go @@ -81,7 +81,7 @@ func (r *RunningAggregator) LogName() string { func (r *RunningAggregator) Init() error { if p, ok := r.Aggregator.(telegraf.Initializer); ok { - err := p.Init() + err := p.Init(telegraf.PluginConfig{Logger: telegraf.Logger{Name: r.LogName()}}) if err != nil { return err } diff --git a/internal/models/running_input.go b/internal/models/running_input.go index 58f029932dd8d..3de64e001db67 100644 --- a/internal/models/running_input.go +++ b/internal/models/running_input.go @@ -66,7 +66,7 @@ func (r *RunningInput) metricFiltered(metric telegraf.Metric) { func (r *RunningInput) Init() error { if p, ok := r.Input.(telegraf.Initializer); ok { - err := p.Init() + err := p.Init(telegraf.PluginConfig{Logger: telegraf.Logger{Name: r.LogName()}}) if err != nil { return err } diff --git a/internal/models/running_output.go b/internal/models/running_output.go index 120036a172ea6..cc10f476b9975 100644 --- a/internal/models/running_output.go +++ b/internal/models/running_output.go @@ -110,7 +110,7 @@ func (ro *RunningOutput) metricFiltered(metric telegraf.Metric) { func (ro *RunningOutput) Init() error { if p, ok := ro.Output.(telegraf.Initializer); ok { - err := p.Init() + err := p.Init(telegraf.PluginConfig{Logger: telegraf.Logger{Name: ro.LogName()}}) if err != nil { return err } diff --git a/internal/models/running_processor.go b/internal/models/running_processor.go index b5b4704e85c61..f927b2d6c9f37 100644 --- a/internal/models/running_processor.go +++ b/internal/models/running_processor.go @@ -52,7 +52,7 @@ func containsMetric(item telegraf.Metric, metrics []telegraf.Metric) bool { func (rp *RunningProcessor) Init() error { if p, ok := rp.Processor.(telegraf.Initializer); ok { - err := p.Init() + err := p.Init(telegraf.PluginConfig{Logger: telegraf.Logger{Name: rp.LogName()}}) if err != nil { return err } diff --git a/plugins/inputs/activemq/activemq.go b/plugins/inputs/activemq/activemq.go index 9d08661b72492..8a0827fb7b57d 100644 --- a/plugins/inputs/activemq/activemq.go +++ b/plugins/inputs/activemq/activemq.go @@ -133,7 +133,7 @@ func (a *ActiveMQ) createHttpClient() (*http.Client, error) { return client, nil } -func (a *ActiveMQ) Init() error { +func (a *ActiveMQ) Init(conf telegraf.PluginConfig) error { if a.ResponseTimeout.Duration < time.Second { a.ResponseTimeout.Duration = time.Second * 5 } diff --git a/plugins/inputs/activemq/activemq_test.go b/plugins/inputs/activemq/activemq_test.go index 407a381775adc..96a8221e8141f 100644 --- a/plugins/inputs/activemq/activemq_test.go +++ b/plugins/inputs/activemq/activemq_test.go @@ -6,6 +6,7 @@ import ( "net/http/httptest" "testing" + "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" ) @@ -50,7 +51,7 @@ func TestGatherQueuesMetrics(t *testing.T) { activeMQ := new(ActiveMQ) activeMQ.Server = "localhost" activeMQ.Port = 8161 - activeMQ.Init() + activeMQ.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) activeMQ.GatherQueuesMetrics(&acc, queues) acc.AssertContainsTaggedFields(t, "activemq_queues", records, tags) @@ -97,7 +98,7 @@ func TestGatherTopicsMetrics(t *testing.T) { activeMQ := new(ActiveMQ) activeMQ.Server = "localhost" activeMQ.Port = 8161 - activeMQ.Init() + activeMQ.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) activeMQ.GatherTopicsMetrics(&acc, topics) acc.AssertContainsTaggedFields(t, "activemq_topics", records, tags) @@ -138,7 +139,7 @@ func TestGatherSubscribersMetrics(t *testing.T) { activeMQ := new(ActiveMQ) activeMQ.Server = "localhost" activeMQ.Port = 8161 - activeMQ.Init() + activeMQ.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) activeMQ.GatherSubscribersMetrics(&acc, subscribers) acc.AssertContainsTaggedFields(t, "activemq_subscribers", records, tags) @@ -169,7 +170,7 @@ func TestURLs(t *testing.T) { URL: "http://" + ts.Listener.Addr().String(), Webadmin: "admin", } - err := plugin.Init() + err := plugin.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) require.NoError(t, err) var acc testutil.Accumulator diff --git a/plugins/inputs/docker_log/docker_log.go b/plugins/inputs/docker_log/docker_log.go index f2b5b514832f6..8901d52529058 100644 --- a/plugins/inputs/docker_log/docker_log.go +++ b/plugins/inputs/docker_log/docker_log.go @@ -106,7 +106,7 @@ func (d *DockerLogs) SampleConfig() string { return sampleConfig } -func (d *DockerLogs) Init() error { +func (d *DockerLogs) Init(conf telegraf.PluginConfig) error { var err error if d.Endpoint == "ENV" { d.client, err = d.newEnvClient() diff --git a/plugins/inputs/docker_log/docker_log_test.go b/plugins/inputs/docker_log/docker_log_test.go index ce61f6135a9fc..8eec37fa8251c 100644 --- a/plugins/inputs/docker_log/docker_log_test.go +++ b/plugins/inputs/docker_log/docker_log_test.go @@ -160,7 +160,7 @@ func Test(t *testing.T) { containerList: make(map[string]context.CancelFunc), } - err := plugin.Init() + err := plugin.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) require.NoError(t, err) err = plugin.Gather(&acc) diff --git a/plugins/inputs/exec/exec.go b/plugins/inputs/exec/exec.go index 615736b3c1c84..dfa8d105d47a6 100644 --- a/plugins/inputs/exec/exec.go +++ b/plugins/inputs/exec/exec.go @@ -3,7 +3,6 @@ package exec import ( "bytes" "fmt" - "log" "os/exec" "path/filepath" "runtime" @@ -51,6 +50,7 @@ type Exec struct { parser parsers.Parser runner Runner + log telegraf.Logger } func NewExec() *Exec { @@ -145,6 +145,7 @@ func (e *Exec) ProcessCommand(command string, acc telegraf.Accumulator, wg *sync defer wg.Done() _, isNagios := e.parser.(*nagios.NagiosParser) + e.log.Debugf("command %#v", command) out, errbuf, runErr := e.runner.Run(command, e.Timeout.Duration) if !isNagios && runErr != nil { err := fmt.Errorf("exec: %s for command '%s': %s", runErr, command, string(errbuf)) @@ -161,7 +162,7 @@ func (e *Exec) ProcessCommand(command string, acc telegraf.Accumulator, wg *sync if isNagios { metrics, err = nagios.TryAddState(runErr, metrics) if err != nil { - log.Printf("E! [inputs.exec] failed to add nagios state: %s", err) + e.log.Errorf("failed to add nagios state: %s", err) } } @@ -229,6 +230,12 @@ func (e *Exec) Gather(acc telegraf.Accumulator) error { return nil } +func (e *Exec) Init(conf telegraf.PluginConfig) error { + e.log = conf.Logger + + return nil +} + func init() { inputs.Add("exec", func() telegraf.Input { return NewExec() diff --git a/plugins/inputs/fireboard/fireboard.go b/plugins/inputs/fireboard/fireboard.go index 2e9c7b0254320..3b3fe72dbb40d 100644 --- a/plugins/inputs/fireboard/fireboard.go +++ b/plugins/inputs/fireboard/fireboard.go @@ -68,7 +68,7 @@ func (r *Fireboard) Description() string { } // Init the things -func (r *Fireboard) Init() error { +func (r *Fireboard) Init(conf telegraf.PluginConfig) error { if len(r.AuthToken) == 0 { return fmt.Errorf("You must specify an authToken") diff --git a/plugins/inputs/http/http.go b/plugins/inputs/http/http.go index 34db9d287549f..cdefc737d5008 100644 --- a/plugins/inputs/http/http.go +++ b/plugins/inputs/http/http.go @@ -88,7 +88,7 @@ func (*HTTP) Description() string { return "Read formatted metrics from one or more HTTP endpoints" } -func (h *HTTP) Init() error { +func (h *HTTP) Init(conf telegraf.PluginConfig) error { tlsCfg, err := h.ClientConfig.TLSConfig() if err != nil { return err diff --git a/plugins/inputs/http/http_test.go b/plugins/inputs/http/http_test.go index 21eff62650f7c..f25332db222b2 100644 --- a/plugins/inputs/http/http_test.go +++ b/plugins/inputs/http/http_test.go @@ -8,6 +8,7 @@ import ( "net/http/httptest" "testing" + "github.com/influxdata/telegraf" plugin "github.com/influxdata/telegraf/plugins/inputs/http" "github.com/influxdata/telegraf/plugins/parsers" "github.com/influxdata/telegraf/testutil" @@ -37,7 +38,7 @@ func TestHTTPwithJSONFormat(t *testing.T) { plugin.SetParser(p) var acc testutil.Accumulator - plugin.Init() + plugin.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) require.NoError(t, acc.GatherError(plugin.Gather)) require.Len(t, acc.Metrics, 1) @@ -79,7 +80,7 @@ func TestHTTPHeaders(t *testing.T) { plugin.SetParser(p) var acc testutil.Accumulator - plugin.Init() + plugin.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) require.NoError(t, acc.GatherError(plugin.Gather)) } @@ -102,7 +103,7 @@ func TestInvalidStatusCode(t *testing.T) { plugin.SetParser(p) var acc testutil.Accumulator - plugin.Init() + plugin.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) require.Error(t, acc.GatherError(plugin.Gather)) } @@ -128,7 +129,7 @@ func TestMethod(t *testing.T) { plugin.SetParser(p) var acc testutil.Accumulator - plugin.Init() + plugin.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) require.NoError(t, acc.GatherError(plugin.Gather)) } @@ -222,7 +223,7 @@ func TestBodyAndContentEncoding(t *testing.T) { tt.plugin.SetParser(parser) var acc testutil.Accumulator - tt.plugin.Init() + tt.plugin.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) err = tt.plugin.Gather(&acc) require.NoError(t, err) }) diff --git a/plugins/inputs/ping/ping.go b/plugins/inputs/ping/ping.go index 469859a345937..9a9ea58a107b3 100644 --- a/plugins/inputs/ping/ping.go +++ b/plugins/inputs/ping/ping.go @@ -327,7 +327,7 @@ func onFin(packetsSent int, resps []*ping.Response, destination string) (map[str } // Init ensures the plugin is configured correctly. -func (p *Ping) Init() error { +func (p *Ping) Init(conf telegraf.PluginConfig) error { if p.Count < 1 { return errors.New("bad number of packets to transmit") } diff --git a/plugins/inputs/x509_cert/x509_cert.go b/plugins/inputs/x509_cert/x509_cert.go index 8558378d14a83..35d1fa2cd903f 100644 --- a/plugins/inputs/x509_cert/x509_cert.go +++ b/plugins/inputs/x509_cert/x509_cert.go @@ -201,7 +201,7 @@ func (c *X509Cert) Gather(acc telegraf.Accumulator) error { return nil } -func (c *X509Cert) Init() error { +func (c *X509Cert) Init(conf telegraf.PluginConfig) error { tlsCfg, err := c.ClientConfig.TLSConfig() if err != nil { return err diff --git a/plugins/inputs/x509_cert/x509_cert_test.go b/plugins/inputs/x509_cert/x509_cert_test.go index 188b510d263d9..e5af57d3db3e3 100644 --- a/plugins/inputs/x509_cert/x509_cert_test.go +++ b/plugins/inputs/x509_cert/x509_cert_test.go @@ -110,7 +110,7 @@ func TestGatherRemote(t *testing.T) { Sources: []string{test.server}, Timeout: internal.Duration{Duration: test.timeout}, } - sc.Init() + sc.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) sc.InsecureSkipVerify = true testErr := false @@ -170,7 +170,7 @@ func TestGatherLocal(t *testing.T) { sc := X509Cert{ Sources: []string{f.Name()}, } - sc.Init() + sc.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) error := false @@ -220,7 +220,7 @@ func TestGatherChain(t *testing.T) { sc := X509Cert{ Sources: []string{f.Name()}, } - sc.Init() + sc.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) error := false @@ -240,7 +240,7 @@ func TestGatherChain(t *testing.T) { func TestStrings(t *testing.T) { sc := X509Cert{} - sc.Init() + sc.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) tests := []struct { name string @@ -269,7 +269,7 @@ func TestGatherCert(t *testing.T) { m := &X509Cert{ Sources: []string{"https://www.influxdata.com:443"}, } - m.Init() + m.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) var acc testutil.Accumulator err := m.Gather(&acc) diff --git a/plugins/outputs/influxdb/http.go b/plugins/outputs/influxdb/http.go index 794eee8b8c4bd..4717c59d8302d 100644 --- a/plugins/outputs/influxdb/http.go +++ b/plugins/outputs/influxdb/http.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "io" - "log" "net" "net/http" "net/url" @@ -106,6 +105,8 @@ type httpClient struct { client *http.Client config HTTPConfig createdDatabases map[string]bool + + log telegraf.Logger } func NewHTTPClient(config HTTPConfig) (*httpClient, error) { @@ -182,6 +183,10 @@ func (c *httpClient) URL() string { return c.config.URL.String() } +func (c *httpClient) SetLogger(log telegraf.Logger) { + c.log = log +} + // Database returns the default database that this client connects too. func (c *httpClient) Database() string { return c.config.Database @@ -257,7 +262,7 @@ func (c *httpClient) Write(ctx context.Context, metrics []telegraf.Metric) error if !c.config.SkipDatabaseCreation && !c.createdDatabases[db] { err := c.CreateDatabase(ctx, db) if err != nil { - log.Printf("W! [outputs.influxdb] when writing to [%s]: database %q creation failed: %v", + c.log.Warnf("when writing to [%s]: database %q creation failed: %v", c.config.URL, db, err) } } @@ -323,7 +328,7 @@ func (c *httpClient) writeBatch(ctx context.Context, db string, metrics []telegr // discarded for being older than the retention policy. Usually this not // a cause for concern and we don't want to retry. if strings.Contains(desc, errStringPointsBeyondRP) { - log.Printf("W! [outputs.influxdb]: when writing to [%s]: received error %v", + c.log.Warnf("when writing to [%s]: received error %v", c.URL(), desc) return nil } @@ -332,7 +337,7 @@ func (c *httpClient) writeBatch(ctx context.Context, db string, metrics []telegr // correctable at this point and so the point is dropped instead of // retrying. if strings.Contains(desc, errStringPartialWrite) { - log.Printf("E! [outputs.influxdb]: when writing to [%s]: received error %v; discarding points", + c.log.Errorf("when writing to [%s]: received error %v; discarding points", c.URL(), desc) return nil } @@ -340,7 +345,7 @@ func (c *httpClient) writeBatch(ctx context.Context, db string, metrics []telegr // This error indicates a bug in either Telegraf line protocol // serialization, retries would not be successful. if strings.Contains(desc, errStringUnableToParse) { - log.Printf("E! [outputs.influxdb]: when writing to [%s]: received error %v; discarding points", + c.log.Errorf("when writing to [%s]: received error %v; discarding points", c.URL(), desc) return nil } diff --git a/plugins/outputs/influxdb/influxdb.go b/plugins/outputs/influxdb/influxdb.go index b2d1a90266259..266e4924c8fbd 100644 --- a/plugins/outputs/influxdb/influxdb.go +++ b/plugins/outputs/influxdb/influxdb.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "log" "math/rand" "net/url" "time" @@ -28,6 +27,7 @@ type Client interface { Database() string URL() string Close() + SetLogger(telegraf.Logger) } // InfluxDB struct is the primary data structure for the plugin @@ -58,6 +58,7 @@ type InfluxDB struct { CreateUDPClientF func(config *UDPConfig) (Client, error) serializer *influx.Serializer + log telegraf.Logger } var sampleConfig = ` @@ -167,6 +168,8 @@ func (i *InfluxDB) Connect() error { return err } + c.SetLogger(i.log) + i.clients = append(i.clients, c) case "http", "https", "unix": c, err := i.httpClient(ctx, parts, proxy) @@ -174,6 +177,8 @@ func (i *InfluxDB) Connect() error { return err } + c.SetLogger(i.log) + i.clients = append(i.clients, c) default: return fmt.Errorf("unsupported scheme [%q]: %q", u, parts.Scheme) @@ -217,13 +222,13 @@ func (i *InfluxDB) Write(metrics []telegraf.Metric) error { if !i.SkipDatabaseCreation { err := client.CreateDatabase(ctx, apiError.Database) if err != nil { - log.Printf("E! [outputs.influxdb] when writing to [%s]: database %q not found and failed to recreate", + i.log.Errorf("when writing to [%s]: database %q not found and failed to recreate", client.URL(), apiError.Database) } } } - log.Printf("E! [outputs.influxdb] when writing to [%s]: %v", client.URL(), err) + i.log.Errorf("when writing to [%s]: %v", client.URL(), err) } return errors.New("could not write any address") @@ -276,7 +281,7 @@ func (i *InfluxDB) httpClient(ctx context.Context, url *url.URL, proxy *url.URL) if !i.SkipDatabaseCreation { err = c.CreateDatabase(ctx, c.Database()) if err != nil { - log.Printf("W! [outputs.influxdb] when writing to [%s]: database %q creation failed: %v", + i.log.Warnf("when writing to [%s]: database %q creation failed: %v", c.URL(), i.Database, err) } } @@ -284,6 +289,12 @@ func (i *InfluxDB) httpClient(ctx context.Context, url *url.URL, proxy *url.URL) return c, nil } +func (i *InfluxDB) Init(conf telegraf.PluginConfig) error { + i.log = conf.Logger + + return nil +} + func init() { outputs.Add("influxdb", func() telegraf.Output { return &InfluxDB{ diff --git a/plugins/outputs/influxdb/influxdb_test.go b/plugins/outputs/influxdb/influxdb_test.go index 73f481e9a5317..e3d1b5a7775bf 100644 --- a/plugins/outputs/influxdb/influxdb_test.go +++ b/plugins/outputs/influxdb/influxdb_test.go @@ -20,6 +20,8 @@ type MockClient struct { CreateDatabaseF func(ctx context.Context, database string) error DatabaseF func() string CloseF func() + + log telegraf.Logger } func (c *MockClient) URL() string { @@ -42,6 +44,10 @@ func (c *MockClient) Close() { c.CloseF() } +func (c *MockClient) SetLogger(log telegraf.Logger) { + c.log = log +} + func TestDeprecatedURLSupport(t *testing.T) { var actual *influxdb.UDPConfig output := influxdb.InfluxDB{ diff --git a/plugins/outputs/influxdb/udp.go b/plugins/outputs/influxdb/udp.go index a33b985634c40..65c53c2b04a94 100644 --- a/plugins/outputs/influxdb/udp.go +++ b/plugins/outputs/influxdb/udp.go @@ -5,7 +5,6 @@ import ( "bytes" "context" "fmt" - "log" "net" "net/url" @@ -69,12 +68,18 @@ type udpClient struct { dialer Dialer serializer *influx.Serializer url *url.URL + + log telegraf.Logger } func (c *udpClient) URL() string { return c.url.String() } +func (c *udpClient) SetLogger(log telegraf.Logger) { + c.log = log +} + func (c *udpClient) Database() string { return "" } @@ -93,7 +98,7 @@ func (c *udpClient) Write(ctx context.Context, metrics []telegraf.Metric) error if err != nil { // Since we are serializing multiple metrics, don't fail the // entire batch just because of one unserializable metric. - log.Printf("E! [outputs.influxdb] when writing to [%s] could not serialize metric: %v", + c.log.Errorf("when writing to [%s] could not serialize metric: %v", c.URL(), err) continue } From 8e77c0a369f9be250436b8ce90c7441f2730c227 Mon Sep 17 00:00:00 2001 From: greg linton Date: Mon, 5 Aug 2019 15:29:13 -0600 Subject: [PATCH 05/15] Add error counting for internal plugin --- input.go | 62 -------------- internal/models/log.go | 85 ++++++++++++++++++++ internal/models/running_aggregator.go | 2 +- internal/models/running_input.go | 2 +- internal/models/running_output.go | 2 +- internal/models/running_processor.go | 2 +- plugins/inputs/activemq/activemq_test.go | 10 +-- plugins/inputs/docker_log/docker_log_test.go | 3 +- plugins/inputs/exec/exec.go | 2 +- plugins/inputs/http/http_test.go | 12 +-- plugins/inputs/x509_cert/x509_cert_test.go | 11 +-- plugins/outputs/influxdb/influxdb.go | 2 +- 12 files changed, 110 insertions(+), 85 deletions(-) create mode 100644 internal/models/log.go diff --git a/input.go b/input.go index 0de0c844b80a0..071ab7d9df603 100644 --- a/input.go +++ b/input.go @@ -1,16 +1,5 @@ package telegraf -import "log" - -// Initializer is an interface that all plugin types: Inputs, Outputs, -// Processors, and Aggregators can optionally implement to initialize the -// plugin. -type Initializer interface { - // Init performs one time setup of the plugin and returns an error if the - // configuration is invalid. - Init(PluginConfig) error -} - type Input interface { // SampleConfig returns the default configuration of the Input SampleConfig() string @@ -33,54 +22,3 @@ type ServiceInput interface { // Stop stops the services and closes any necessary channels and connections Stop() } - -// PluginConfig contains individualized plugin configuration. -type PluginConfig struct { - Logger Logger -} - -// Logger defines a logging structure for plugins. -type Logger struct { - Name string // Name is the plugin name, will be printed in the `[]`. -} - -// Errorf logs an error message, patterned after log.Printf. -func (l Logger) Errorf(format string, args ...interface{}) { - // todo: keep tally of errors from plugins - log.Printf("E! ["+l.Name+"] "+format, args...) -} - -// Error logs an error message, patterned after log.Print. -func (l Logger) Error(args ...interface{}) { - log.Print(append([]interface{}{"E! [" + l.Name + "] "}, args...)...) -} - -// Debugf logs a debug message, patterned after log.Printf. -func (l Logger) Debugf(format string, args ...interface{}) { - log.Printf("D! ["+l.Name+"] "+format, args...) -} - -// Debug logs a debug message, patterned after log.Print. -func (l Logger) Debug(args ...interface{}) { - log.Print(append([]interface{}{"D! [" + l.Name + "] "}, args...)...) -} - -// Warnf logs a warning message, patterned after log.Printf. -func (l Logger) Warnf(format string, args ...interface{}) { - log.Printf("W! ["+l.Name+"] "+format, args...) -} - -// Warn logs a warning message, patterned after log.Print. -func (l Logger) Warn(args ...interface{}) { - log.Print(append([]interface{}{"W! [" + l.Name + "] "}, args...)...) -} - -// Infof logs an information message, patterned after log.Printf. -func (l Logger) Infof(format string, args ...interface{}) { - log.Printf("I! ["+l.Name+"] "+format, args...) -} - -// Info logs an information message, patterned after log.Print. -func (l Logger) Info(args ...interface{}) { - log.Print(append([]interface{}{"I! [" + l.Name + "] "}, args...)...) -} diff --git a/internal/models/log.go b/internal/models/log.go new file mode 100644 index 0000000000000..ed5dceab58819 --- /dev/null +++ b/internal/models/log.go @@ -0,0 +1,85 @@ +package models + +import ( + "log" + "strings" + + "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/selfstat" +) + +// PluginConfig contains individualized plugin configuration. +type PluginConfig struct { + Log Logger +} + +// Logger returns PluginConfig's Logger in order to satisfy the interface. +func (p PluginConfig) Logger() telegraf.Logger { + return p.Log +} + +// Logger defines a logging structure for plugins. +type Logger struct { + Name string // Name is the plugin name, will be printed in the `[]`. +} + +// Errorf logs an error message, patterned after log.Printf. +func (l Logger) Errorf(format string, args ...interface{}) { + // todo: keep tally of errors from plugins + log.Printf("E! ["+l.Name+"] "+format, args...) +} + +// Error logs an error message, patterned after log.Print. +func (l Logger) Error(args ...interface{}) { + log.Print(append([]interface{}{"E! [" + l.Name + "] "}, args...)...) +} + +// Debugf logs a debug message, patterned after log.Printf. +func (l Logger) Debugf(format string, args ...interface{}) { + log.Printf("D! ["+l.Name+"] "+format, args...) +} + +// Debug logs a debug message, patterned after log.Print. +func (l Logger) Debug(args ...interface{}) { + log.Print(append([]interface{}{"D! [" + l.Name + "] "}, args...)...) +} + +// Warnf logs a warning message, patterned after log.Printf. +func (l Logger) Warnf(format string, args ...interface{}) { + log.Printf("W! ["+l.Name+"] "+format, args...) +} + +// Warn logs a warning message, patterned after log.Print. +func (l Logger) Warn(args ...interface{}) { + log.Print(append([]interface{}{"W! [" + l.Name + "] "}, args...)...) +} + +// Infof logs an information message, patterned after log.Printf. +func (l Logger) Infof(format string, args ...interface{}) { + log.Printf("I! ["+l.Name+"] "+format, args...) +} + +// Info logs an information message, patterned after log.Print. +func (l Logger) Info(args ...interface{}) { + log.Print(append([]interface{}{"I! [" + l.Name + "] "}, args...)...) +} + +func (l Logger) addError() { + switch { + case strings.HasPrefix(l.Name, "aggregator"): + fallthrough + case strings.HasPrefix(l.Name, "input"): + iErrors.Incr(1) + case strings.HasPrefix(l.Name, "output"): + oErrors.Incr(1) + case strings.HasPrefix(l.Name, "processor"): + pErrors.Incr(1) + } +} + +var ( + aErrors = selfstat.Register("agent", "aggregator_errors", map[string]string{}) + iErrors = selfstat.Register("agent", "input_errors", map[string]string{}) + oErrors = selfstat.Register("agent", "output_errors", map[string]string{}) + pErrors = selfstat.Register("agent", "processor_errors", map[string]string{}) +) diff --git a/internal/models/running_aggregator.go b/internal/models/running_aggregator.go index e9d68258f1065..98a247928efe0 100644 --- a/internal/models/running_aggregator.go +++ b/internal/models/running_aggregator.go @@ -81,7 +81,7 @@ func (r *RunningAggregator) LogName() string { func (r *RunningAggregator) Init() error { if p, ok := r.Aggregator.(telegraf.Initializer); ok { - err := p.Init(telegraf.PluginConfig{Logger: telegraf.Logger{Name: r.LogName()}}) + err := p.Init(PluginConfig{Log: Logger{Name: r.LogName()}}) if err != nil { return err } diff --git a/internal/models/running_input.go b/internal/models/running_input.go index 3de64e001db67..4f6ca96d57db8 100644 --- a/internal/models/running_input.go +++ b/internal/models/running_input.go @@ -66,7 +66,7 @@ func (r *RunningInput) metricFiltered(metric telegraf.Metric) { func (r *RunningInput) Init() error { if p, ok := r.Input.(telegraf.Initializer); ok { - err := p.Init(telegraf.PluginConfig{Logger: telegraf.Logger{Name: r.LogName()}}) + err := p.Init(PluginConfig{Log: Logger{Name: r.LogName()}}) if err != nil { return err } diff --git a/internal/models/running_output.go b/internal/models/running_output.go index cc10f476b9975..6dd7b49cbd6d8 100644 --- a/internal/models/running_output.go +++ b/internal/models/running_output.go @@ -110,7 +110,7 @@ func (ro *RunningOutput) metricFiltered(metric telegraf.Metric) { func (ro *RunningOutput) Init() error { if p, ok := ro.Output.(telegraf.Initializer); ok { - err := p.Init(telegraf.PluginConfig{Logger: telegraf.Logger{Name: ro.LogName()}}) + err := p.Init(PluginConfig{Log: Logger{Name: ro.LogName()}}) if err != nil { return err } diff --git a/internal/models/running_processor.go b/internal/models/running_processor.go index f927b2d6c9f37..e9e5731671bb6 100644 --- a/internal/models/running_processor.go +++ b/internal/models/running_processor.go @@ -52,7 +52,7 @@ func containsMetric(item telegraf.Metric, metrics []telegraf.Metric) bool { func (rp *RunningProcessor) Init() error { if p, ok := rp.Processor.(telegraf.Initializer); ok { - err := p.Init(telegraf.PluginConfig{Logger: telegraf.Logger{Name: rp.LogName()}}) + err := p.Init(PluginConfig{Log: Logger{Name: rp.LogName()}}) if err != nil { return err } diff --git a/plugins/inputs/activemq/activemq_test.go b/plugins/inputs/activemq/activemq_test.go index 96a8221e8141f..17fe6f44c536e 100644 --- a/plugins/inputs/activemq/activemq_test.go +++ b/plugins/inputs/activemq/activemq_test.go @@ -6,7 +6,7 @@ import ( "net/http/httptest" "testing" - "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/internal/models" "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" ) @@ -51,7 +51,7 @@ func TestGatherQueuesMetrics(t *testing.T) { activeMQ := new(ActiveMQ) activeMQ.Server = "localhost" activeMQ.Port = 8161 - activeMQ.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) + activeMQ.Init(models.PluginConfig{Log: models.Logger{}}) activeMQ.GatherQueuesMetrics(&acc, queues) acc.AssertContainsTaggedFields(t, "activemq_queues", records, tags) @@ -98,7 +98,7 @@ func TestGatherTopicsMetrics(t *testing.T) { activeMQ := new(ActiveMQ) activeMQ.Server = "localhost" activeMQ.Port = 8161 - activeMQ.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) + activeMQ.Init(models.PluginConfig{Log: models.Logger{}}) activeMQ.GatherTopicsMetrics(&acc, topics) acc.AssertContainsTaggedFields(t, "activemq_topics", records, tags) @@ -139,7 +139,7 @@ func TestGatherSubscribersMetrics(t *testing.T) { activeMQ := new(ActiveMQ) activeMQ.Server = "localhost" activeMQ.Port = 8161 - activeMQ.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) + activeMQ.Init(models.PluginConfig{Log: models.Logger{}}) activeMQ.GatherSubscribersMetrics(&acc, subscribers) acc.AssertContainsTaggedFields(t, "activemq_subscribers", records, tags) @@ -170,7 +170,7 @@ func TestURLs(t *testing.T) { URL: "http://" + ts.Listener.Addr().String(), Webadmin: "admin", } - err := plugin.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) + err := plugin.Init(models.PluginConfig{Log: models.Logger{}}) require.NoError(t, err) var acc testutil.Accumulator diff --git a/plugins/inputs/docker_log/docker_log_test.go b/plugins/inputs/docker_log/docker_log_test.go index 8eec37fa8251c..dacfec93ab8ba 100644 --- a/plugins/inputs/docker_log/docker_log_test.go +++ b/plugins/inputs/docker_log/docker_log_test.go @@ -13,6 +13,7 @@ import ( "github.com/docker/docker/pkg/stdcopy" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal" + "github.com/influxdata/telegraf/internal/models" "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" ) @@ -160,7 +161,7 @@ func Test(t *testing.T) { containerList: make(map[string]context.CancelFunc), } - err := plugin.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) + err := plugin.Init(models.PluginConfig{Log: models.Logger{}}) require.NoError(t, err) err = plugin.Gather(&acc) diff --git a/plugins/inputs/exec/exec.go b/plugins/inputs/exec/exec.go index dfa8d105d47a6..739f15cd7852a 100644 --- a/plugins/inputs/exec/exec.go +++ b/plugins/inputs/exec/exec.go @@ -231,7 +231,7 @@ func (e *Exec) Gather(acc telegraf.Accumulator) error { } func (e *Exec) Init(conf telegraf.PluginConfig) error { - e.log = conf.Logger + e.log = conf.Logger() return nil } diff --git a/plugins/inputs/http/http_test.go b/plugins/inputs/http/http_test.go index f25332db222b2..d62bde4877796 100644 --- a/plugins/inputs/http/http_test.go +++ b/plugins/inputs/http/http_test.go @@ -8,7 +8,7 @@ import ( "net/http/httptest" "testing" - "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/internal/models" plugin "github.com/influxdata/telegraf/plugins/inputs/http" "github.com/influxdata/telegraf/plugins/parsers" "github.com/influxdata/telegraf/testutil" @@ -38,7 +38,7 @@ func TestHTTPwithJSONFormat(t *testing.T) { plugin.SetParser(p) var acc testutil.Accumulator - plugin.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) + plugin.Init(models.PluginConfig{Log: models.Logger{}}) require.NoError(t, acc.GatherError(plugin.Gather)) require.Len(t, acc.Metrics, 1) @@ -80,7 +80,7 @@ func TestHTTPHeaders(t *testing.T) { plugin.SetParser(p) var acc testutil.Accumulator - plugin.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) + plugin.Init(models.PluginConfig{Log: models.Logger{}}) require.NoError(t, acc.GatherError(plugin.Gather)) } @@ -103,7 +103,7 @@ func TestInvalidStatusCode(t *testing.T) { plugin.SetParser(p) var acc testutil.Accumulator - plugin.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) + plugin.Init(models.PluginConfig{Log: models.Logger{}}) require.Error(t, acc.GatherError(plugin.Gather)) } @@ -129,7 +129,7 @@ func TestMethod(t *testing.T) { plugin.SetParser(p) var acc testutil.Accumulator - plugin.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) + plugin.Init(models.PluginConfig{Log: models.Logger{}}) require.NoError(t, acc.GatherError(plugin.Gather)) } @@ -223,7 +223,7 @@ func TestBodyAndContentEncoding(t *testing.T) { tt.plugin.SetParser(parser) var acc testutil.Accumulator - tt.plugin.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) + tt.plugin.Init(models.PluginConfig{Log: models.Logger{}}) err = tt.plugin.Gather(&acc) require.NoError(t, err) }) diff --git a/plugins/inputs/x509_cert/x509_cert_test.go b/plugins/inputs/x509_cert/x509_cert_test.go index e5af57d3db3e3..f293f25b6b4d3 100644 --- a/plugins/inputs/x509_cert/x509_cert_test.go +++ b/plugins/inputs/x509_cert/x509_cert_test.go @@ -14,6 +14,7 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal" + "github.com/influxdata/telegraf/internal/models" "github.com/influxdata/telegraf/testutil" ) @@ -110,7 +111,7 @@ func TestGatherRemote(t *testing.T) { Sources: []string{test.server}, Timeout: internal.Duration{Duration: test.timeout}, } - sc.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) + sc.Init(models.PluginConfig{Log: models.Logger{}}) sc.InsecureSkipVerify = true testErr := false @@ -170,7 +171,7 @@ func TestGatherLocal(t *testing.T) { sc := X509Cert{ Sources: []string{f.Name()}, } - sc.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) + sc.Init(models.PluginConfig{Log: models.Logger{}}) error := false @@ -220,7 +221,7 @@ func TestGatherChain(t *testing.T) { sc := X509Cert{ Sources: []string{f.Name()}, } - sc.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) + sc.Init(models.PluginConfig{Log: models.Logger{}}) error := false @@ -240,7 +241,7 @@ func TestGatherChain(t *testing.T) { func TestStrings(t *testing.T) { sc := X509Cert{} - sc.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) + sc.Init(models.PluginConfig{Log: models.Logger{}}) tests := []struct { name string @@ -269,7 +270,7 @@ func TestGatherCert(t *testing.T) { m := &X509Cert{ Sources: []string{"https://www.influxdata.com:443"}, } - m.Init(telegraf.PluginConfig{Logger: telegraf.Logger{}}) + m.Init(models.PluginConfig{Log: models.Logger{}}) var acc testutil.Accumulator err := m.Gather(&acc) diff --git a/plugins/outputs/influxdb/influxdb.go b/plugins/outputs/influxdb/influxdb.go index 266e4924c8fbd..dc406797e00ea 100644 --- a/plugins/outputs/influxdb/influxdb.go +++ b/plugins/outputs/influxdb/influxdb.go @@ -290,7 +290,7 @@ func (i *InfluxDB) httpClient(ctx context.Context, url *url.URL, proxy *url.URL) } func (i *InfluxDB) Init(conf telegraf.PluginConfig) error { - i.log = conf.Logger + i.log = conf.Logger() return nil } From 837ca03c73e587aae68f7c4518baed880bb2f581 Mon Sep 17 00:00:00 2001 From: greg linton Date: Tue, 6 Aug 2019 08:46:13 -0600 Subject: [PATCH 06/15] Test logging and tracking errors --- internal/models/log.go | 5 ++-- internal/models/log_test.go | 47 +++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 internal/models/log_test.go diff --git a/internal/models/log.go b/internal/models/log.go index ed5dceab58819..24fda2f4ddfa0 100644 --- a/internal/models/log.go +++ b/internal/models/log.go @@ -25,12 +25,13 @@ type Logger struct { // Errorf logs an error message, patterned after log.Printf. func (l Logger) Errorf(format string, args ...interface{}) { - // todo: keep tally of errors from plugins + l.addError() log.Printf("E! ["+l.Name+"] "+format, args...) } // Error logs an error message, patterned after log.Print. func (l Logger) Error(args ...interface{}) { + l.addError() log.Print(append([]interface{}{"E! [" + l.Name + "] "}, args...)...) } @@ -67,7 +68,7 @@ func (l Logger) Info(args ...interface{}) { func (l Logger) addError() { switch { case strings.HasPrefix(l.Name, "aggregator"): - fallthrough + aErrors.Incr(1) case strings.HasPrefix(l.Name, "input"): iErrors.Incr(1) case strings.HasPrefix(l.Name, "output"): diff --git a/internal/models/log_test.go b/internal/models/log_test.go new file mode 100644 index 0000000000000..5c7a4984dfab1 --- /dev/null +++ b/internal/models/log_test.go @@ -0,0 +1,47 @@ +package models + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestErrorCount(t *testing.T) { + log := Logger{Name: "inputs.test"} + log.Errorf("something went wrong") + log.Error("something went wrong") + + log.Name = "aggregators.test" + log.Error("another thing happened") + + log.Name = "outputs.test" + log.Error("another thing happened") + + log.Name = "processors.test" + log.Error("another thing happened") + + require.Equal(t, int64(2), iErrors.Get()) + require.Equal(t, int64(1), aErrors.Get()) + require.Equal(t, int64(1), oErrors.Get()) + require.Equal(t, int64(1), pErrors.Get()) +} + +func TestPluginConfig(t *testing.T) { + p := PluginConfig{Log: Logger{Name: "inputs.test"}} + log := p.Logger() + + log.Debugf("something happened") + log.Debug("something happened") + + log.Warnf("something happened") + log.Warn("something happened") + require.Equal(t, int64(0), iErrors.Get()) + + log.Infof("something happened") + log.Info("something happened") + require.Equal(t, int64(0), iErrors.Get()) + + log.Errorf("something happened") + log.Error("something happened") + require.Equal(t, int64(2), iErrors.Get()) +} From 506479c0698c67800d7cc8b148d6b9e90e07e402 Mon Sep 17 00:00:00 2001 From: greg linton Date: Tue, 6 Aug 2019 08:51:11 -0600 Subject: [PATCH 07/15] Add plugin.go with Initializer and Logger interface --- plugin.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 plugin.go diff --git a/plugin.go b/plugin.go new file mode 100644 index 0000000000000..afae6c52e6143 --- /dev/null +++ b/plugin.go @@ -0,0 +1,35 @@ +package telegraf + +// Initializer is an interface that all plugin types: Inputs, Outputs, +// Processors, and Aggregators can optionally implement to initialize the +// plugin. +type Initializer interface { + // Init performs one time setup of the plugin and returns an error if the + // configuration is invalid. + Init(PluginConfig) error +} + +// PluginConfig allows the configuration of plugins. +type PluginConfig interface { + Logger() Logger +} + +// Logger defines an interface for logging. +type Logger interface { + // Errorf logs an error message, patterned after log.Printf. + Errorf(format string, args ...interface{}) + // Error logs an error message, patterned after log.Print. + Error(args ...interface{}) + // Debugf logs a debug message, patterned after log.Printf. + Debugf(format string, args ...interface{}) + // Debug logs a debug message, patterned after log.Print. + Debug(args ...interface{}) + // Warnf logs a warning message, patterned after log.Printf. + Warnf(format string, args ...interface{}) + // Warn logs a warning message, patterned after log.Print. + Warn(args ...interface{}) + // Infof logs an information message, patterned after log.Printf. + Infof(format string, args ...interface{}) + // Info logs an information message, patterned after log.Print. + Info(args ...interface{}) +} From cbfdf00cc9d2763b02a4863918eb33e5af63f87c Mon Sep 17 00:00:00 2001 From: greg linton Date: Tue, 13 Aug 2019 10:58:28 -0600 Subject: [PATCH 08/15] Initialize logger in tests --- internal/models/log_test.go | 6 ++++++ plugins/outputs/influxdb/http.go | 2 ++ plugins/outputs/influxdb/influxdb.go | 2 ++ plugins/outputs/influxdb/influxdb_test.go | 16 ++++++++++++++-- plugins/outputs/influxdb/udp_test.go | 7 +++++++ 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/internal/models/log_test.go b/internal/models/log_test.go index 5c7a4984dfab1..b86d770bdfc2c 100644 --- a/internal/models/log_test.go +++ b/internal/models/log_test.go @@ -7,6 +7,11 @@ import ( ) func TestErrorCount(t *testing.T) { + iErrors.Set(0) + aErrors.Set(0) + oErrors.Set(0) + pErrors.Set(0) + log := Logger{Name: "inputs.test"} log.Errorf("something went wrong") log.Error("something went wrong") @@ -27,6 +32,7 @@ func TestErrorCount(t *testing.T) { } func TestPluginConfig(t *testing.T) { + iErrors.Set(0) p := PluginConfig{Log: Logger{Name: "inputs.test"}} log := p.Logger() diff --git a/plugins/outputs/influxdb/http.go b/plugins/outputs/influxdb/http.go index 4717c59d8302d..fa864be39cb4e 100644 --- a/plugins/outputs/influxdb/http.go +++ b/plugins/outputs/influxdb/http.go @@ -15,6 +15,7 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal" + "github.com/influxdata/telegraf/internal/models" "github.com/influxdata/telegraf/plugins/serializers/influx" ) @@ -174,6 +175,7 @@ func NewHTTPClient(config HTTPConfig) (*httpClient, error) { }, createdDatabases: make(map[string]bool), config: config, + log: models.Logger{Name: userAgent}, } return client, nil } diff --git a/plugins/outputs/influxdb/influxdb.go b/plugins/outputs/influxdb/influxdb.go index dc406797e00ea..0507de9544926 100644 --- a/plugins/outputs/influxdb/influxdb.go +++ b/plugins/outputs/influxdb/influxdb.go @@ -10,6 +10,7 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal" + "github.com/influxdata/telegraf/internal/models" "github.com/influxdata/telegraf/internal/tls" "github.com/influxdata/telegraf/plugins/outputs" "github.com/influxdata/telegraf/plugins/serializers/influx" @@ -305,6 +306,7 @@ func init() { CreateUDPClientF: func(config *UDPConfig) (Client, error) { return NewUDPClient(*config) }, + log: models.Logger{Name: "outputs.influxdb"}, } }) } diff --git a/plugins/outputs/influxdb/influxdb_test.go b/plugins/outputs/influxdb/influxdb_test.go index e3d1b5a7775bf..ac42319b8bfde 100644 --- a/plugins/outputs/influxdb/influxdb_test.go +++ b/plugins/outputs/influxdb/influxdb_test.go @@ -8,6 +8,7 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal" + "github.com/influxdata/telegraf/internal/models" "github.com/influxdata/telegraf/internal/tls" "github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/plugins/outputs/influxdb" @@ -58,6 +59,9 @@ func TestDeprecatedURLSupport(t *testing.T) { return &MockClient{}, nil }, } + + output.Init(models.PluginConfig{Log: models.Logger{}}) + err := output.Connect() require.NoError(t, err) require.Equal(t, "udp://localhost:8089", actual.URL.String()) @@ -78,6 +82,9 @@ func TestDefaultURL(t *testing.T) { }, nil }, } + + output.Init(models.PluginConfig{Log: models.Logger{}}) + err := output.Connect() require.NoError(t, err) require.Equal(t, "http://localhost:8086", actual.URL.String()) @@ -95,6 +102,8 @@ func TestConnectUDPConfig(t *testing.T) { return &MockClient{}, nil }, } + output.Init(models.PluginConfig{Log: models.Logger{}}) + err := output.Connect() require.NoError(t, err) @@ -136,6 +145,9 @@ func TestConnectHTTPConfig(t *testing.T) { }, nil }, } + + output.Init(models.PluginConfig{Log: models.Logger{}}) + err := output.Connect() require.NoError(t, err) @@ -159,7 +171,6 @@ func TestConnectHTTPConfig(t *testing.T) { func TestWriteRecreateDatabaseIfDatabaseNotFound(t *testing.T) { output := influxdb.InfluxDB{ URLs: []string{"http://localhost:8086"}, - CreateHTTPClientF: func(config *influxdb.HTTPConfig) (influxdb.Client, error) { return &MockClient{ DatabaseF: func() string { @@ -179,12 +190,13 @@ func TestWriteRecreateDatabaseIfDatabaseNotFound(t *testing.T) { }, URLF: func() string { return "http://localhost:8086" - }, }, nil }, } + output.Init(models.PluginConfig{Log: models.Logger{}}) + err := output.Connect() require.NoError(t, err) diff --git a/plugins/outputs/influxdb/udp_test.go b/plugins/outputs/influxdb/udp_test.go index 136ebb787f61b..0d17a2b7a5a4f 100644 --- a/plugins/outputs/influxdb/udp_test.go +++ b/plugins/outputs/influxdb/udp_test.go @@ -12,6 +12,7 @@ import ( "time" "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/internal/models" "github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/plugins/outputs/influxdb" "github.com/stretchr/testify/require" @@ -78,6 +79,7 @@ func TestUDP_URL(t *testing.T) { } client, err := influxdb.NewUDPClient(config) + client.SetLogger(models.Logger{}) require.NoError(t, err) require.Equal(t, u.String(), client.URL()) @@ -101,6 +103,7 @@ func TestUDP_Simple(t *testing.T) { }, } client, err := influxdb.NewUDPClient(config) + client.SetLogger(models.Logger{}) require.NoError(t, err) ctx := context.Background() @@ -127,6 +130,7 @@ func TestUDP_DialError(t *testing.T) { }, } client, err := influxdb.NewUDPClient(config) + client.SetLogger(models.Logger{}) require.NoError(t, err) ctx := context.Background() @@ -156,6 +160,7 @@ func TestUDP_WriteError(t *testing.T) { }, } client, err := influxdb.NewUDPClient(config) + client.SetLogger(models.Logger{}) require.NoError(t, err) ctx := context.Background() @@ -219,6 +224,7 @@ func TestUDP_ErrorLogging(t *testing.T) { log.SetOutput(&b) client, err := influxdb.NewUDPClient(tt.config) + client.SetLogger(models.Logger{}) require.NoError(t, err) ctx := context.Background() @@ -262,6 +268,7 @@ func TestUDP_WriteWithRealConn(t *testing.T) { URL: u, } client, err := influxdb.NewUDPClient(config) + client.SetLogger(models.Logger{}) require.NoError(t, err) ctx := context.Background() From 4f04ef5d535518e6e8d166fc5fd90e1b4b3b6324 Mon Sep 17 00:00:00 2001 From: greg linton Date: Tue, 13 Aug 2019 11:49:55 -0600 Subject: [PATCH 09/15] Initialize logger in more tests --- plugins/inputs/exec/exec.go | 2 ++ plugins/inputs/exec/exec_test.go | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/plugins/inputs/exec/exec.go b/plugins/inputs/exec/exec.go index 739f15cd7852a..93fc0e8b34aef 100644 --- a/plugins/inputs/exec/exec.go +++ b/plugins/inputs/exec/exec.go @@ -14,6 +14,7 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal" + "github.com/influxdata/telegraf/internal/models" "github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/parsers" "github.com/influxdata/telegraf/plugins/parsers/nagios" @@ -55,6 +56,7 @@ type Exec struct { func NewExec() *Exec { return &Exec{ + log: models.Logger{}, runner: CommandRunner{}, Timeout: internal.Duration{Duration: time.Second * 5}, } diff --git a/plugins/inputs/exec/exec_test.go b/plugins/inputs/exec/exec_test.go index 5aaef8961ee69..59ffe29cf609b 100644 --- a/plugins/inputs/exec/exec_test.go +++ b/plugins/inputs/exec/exec_test.go @@ -7,6 +7,7 @@ import ( "testing" "time" + "github.com/influxdata/telegraf/internal/models" "github.com/influxdata/telegraf/plugins/parsers" "github.com/influxdata/telegraf/testutil" @@ -97,6 +98,7 @@ func TestExec(t *testing.T) { MetricName: "exec", }) e := &Exec{ + log: models.Logger{}, runner: newRunnerMock([]byte(validJson), nil, nil), Commands: []string{"testcommand arg1"}, parser: parser, @@ -126,6 +128,7 @@ func TestExecMalformed(t *testing.T) { MetricName: "exec", }) e := &Exec{ + log: models.Logger{}, runner: newRunnerMock([]byte(malformedJson), nil, nil), Commands: []string{"badcommand arg1"}, parser: parser, @@ -142,6 +145,7 @@ func TestCommandError(t *testing.T) { MetricName: "exec", }) e := &Exec{ + log: models.Logger{}, runner: newRunnerMock(nil, nil, fmt.Errorf("exit status code 1")), Commands: []string{"badcommand"}, parser: parser, From 53aaa966d11ac2bce3bd029422cb2d51c311b5fe Mon Sep 17 00:00:00 2001 From: greg linton Date: Thu, 15 Aug 2019 16:58:31 -0600 Subject: [PATCH 10/15] Address feedback --- agent/accumulator.go | 2 +- internal/models/log.go | 11 ----- internal/models/log_test.go | 5 +- internal/models/running_aggregator.go | 2 +- internal/models/running_input.go | 2 +- internal/models/running_output.go | 2 +- internal/models/running_processor.go | 2 +- plugin.go | 6 +-- plugins/inputs/activemq/activemq_test.go | 10 ++-- plugins/inputs/docker_log/docker_log_test.go | 3 +- plugins/inputs/exec/exec.go | 5 +- plugins/inputs/exec/exec_test.go | 8 ++-- plugins/inputs/http/http_test.go | 12 ++--- plugins/inputs/x509_cert/x509_cert_test.go | 11 ++--- plugins/outputs/influxdb/http.go | 4 +- plugins/outputs/influxdb/http_test.go | 2 + plugins/outputs/influxdb/influxdb.go | 4 +- plugins/outputs/influxdb/influxdb_test.go | 12 ++--- plugins/outputs/influxdb/udp.go | 1 + plugins/outputs/influxdb/udp_test.go | 14 +++--- testutil/log.go | 50 ++++++++++++++++++++ 21 files changed, 100 insertions(+), 68 deletions(-) create mode 100644 testutil/log.go diff --git a/agent/accumulator.go b/agent/accumulator.go index 929a904426a20..53167d6c6b950 100644 --- a/agent/accumulator.go +++ b/agent/accumulator.go @@ -112,7 +112,7 @@ func (ac *accumulator) AddError(err error) { return } NErrors.Incr(1) - log.Printf("E! [%s]: Error in plugin: %v", ac.maker.LogName(), err) + log.Printf("E! [%s] Error in plugin: %v", ac.maker.LogName(), err) } func (ac *accumulator) SetPrecision(precision time.Duration) { diff --git a/internal/models/log.go b/internal/models/log.go index 24fda2f4ddfa0..8eb1b931fd3f2 100644 --- a/internal/models/log.go +++ b/internal/models/log.go @@ -4,20 +4,9 @@ import ( "log" "strings" - "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/selfstat" ) -// PluginConfig contains individualized plugin configuration. -type PluginConfig struct { - Log Logger -} - -// Logger returns PluginConfig's Logger in order to satisfy the interface. -func (p PluginConfig) Logger() telegraf.Logger { - return p.Log -} - // Logger defines a logging structure for plugins. type Logger struct { Name string // Name is the plugin name, will be printed in the `[]`. diff --git a/internal/models/log_test.go b/internal/models/log_test.go index b86d770bdfc2c..36ed77ea2ddd2 100644 --- a/internal/models/log_test.go +++ b/internal/models/log_test.go @@ -31,10 +31,9 @@ func TestErrorCount(t *testing.T) { require.Equal(t, int64(1), pErrors.Get()) } -func TestPluginConfig(t *testing.T) { +func TestLogging(t *testing.T) { iErrors.Set(0) - p := PluginConfig{Log: Logger{Name: "inputs.test"}} - log := p.Logger() + log := Logger{Name: "inputs.test"} log.Debugf("something happened") log.Debug("something happened") diff --git a/internal/models/running_aggregator.go b/internal/models/running_aggregator.go index 98a247928efe0..d9f5a8485ba12 100644 --- a/internal/models/running_aggregator.go +++ b/internal/models/running_aggregator.go @@ -81,7 +81,7 @@ func (r *RunningAggregator) LogName() string { func (r *RunningAggregator) Init() error { if p, ok := r.Aggregator.(telegraf.Initializer); ok { - err := p.Init(PluginConfig{Log: Logger{Name: r.LogName()}}) + err := p.Init(telegraf.PluginConfig{Log: Logger{Name: r.LogName()}}) if err != nil { return err } diff --git a/internal/models/running_input.go b/internal/models/running_input.go index 4f6ca96d57db8..6fa4ec7f478a6 100644 --- a/internal/models/running_input.go +++ b/internal/models/running_input.go @@ -66,7 +66,7 @@ func (r *RunningInput) metricFiltered(metric telegraf.Metric) { func (r *RunningInput) Init() error { if p, ok := r.Input.(telegraf.Initializer); ok { - err := p.Init(PluginConfig{Log: Logger{Name: r.LogName()}}) + err := p.Init(telegraf.PluginConfig{Log: Logger{Name: r.LogName()}}) if err != nil { return err } diff --git a/internal/models/running_output.go b/internal/models/running_output.go index 6dd7b49cbd6d8..44776232f516e 100644 --- a/internal/models/running_output.go +++ b/internal/models/running_output.go @@ -110,7 +110,7 @@ func (ro *RunningOutput) metricFiltered(metric telegraf.Metric) { func (ro *RunningOutput) Init() error { if p, ok := ro.Output.(telegraf.Initializer); ok { - err := p.Init(PluginConfig{Log: Logger{Name: ro.LogName()}}) + err := p.Init(telegraf.PluginConfig{Log: Logger{Name: ro.LogName()}}) if err != nil { return err } diff --git a/internal/models/running_processor.go b/internal/models/running_processor.go index e9e5731671bb6..7162e6a997699 100644 --- a/internal/models/running_processor.go +++ b/internal/models/running_processor.go @@ -52,7 +52,7 @@ func containsMetric(item telegraf.Metric, metrics []telegraf.Metric) bool { func (rp *RunningProcessor) Init() error { if p, ok := rp.Processor.(telegraf.Initializer); ok { - err := p.Init(PluginConfig{Log: Logger{Name: rp.LogName()}}) + err := p.Init(telegraf.PluginConfig{Log: Logger{Name: rp.LogName()}}) if err != nil { return err } diff --git a/plugin.go b/plugin.go index afae6c52e6143..043164718e8f9 100644 --- a/plugin.go +++ b/plugin.go @@ -9,9 +9,9 @@ type Initializer interface { Init(PluginConfig) error } -// PluginConfig allows the configuration of plugins. -type PluginConfig interface { - Logger() Logger +// PluginConfig contains individualized plugin configuration. +type PluginConfig struct { + Log Logger } // Logger defines an interface for logging. diff --git a/plugins/inputs/activemq/activemq_test.go b/plugins/inputs/activemq/activemq_test.go index 17fe6f44c536e..887d44eefbf30 100644 --- a/plugins/inputs/activemq/activemq_test.go +++ b/plugins/inputs/activemq/activemq_test.go @@ -6,7 +6,7 @@ import ( "net/http/httptest" "testing" - "github.com/influxdata/telegraf/internal/models" + "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" ) @@ -51,7 +51,7 @@ func TestGatherQueuesMetrics(t *testing.T) { activeMQ := new(ActiveMQ) activeMQ.Server = "localhost" activeMQ.Port = 8161 - activeMQ.Init(models.PluginConfig{Log: models.Logger{}}) + activeMQ.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) activeMQ.GatherQueuesMetrics(&acc, queues) acc.AssertContainsTaggedFields(t, "activemq_queues", records, tags) @@ -98,7 +98,7 @@ func TestGatherTopicsMetrics(t *testing.T) { activeMQ := new(ActiveMQ) activeMQ.Server = "localhost" activeMQ.Port = 8161 - activeMQ.Init(models.PluginConfig{Log: models.Logger{}}) + activeMQ.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) activeMQ.GatherTopicsMetrics(&acc, topics) acc.AssertContainsTaggedFields(t, "activemq_topics", records, tags) @@ -139,7 +139,7 @@ func TestGatherSubscribersMetrics(t *testing.T) { activeMQ := new(ActiveMQ) activeMQ.Server = "localhost" activeMQ.Port = 8161 - activeMQ.Init(models.PluginConfig{Log: models.Logger{}}) + activeMQ.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) activeMQ.GatherSubscribersMetrics(&acc, subscribers) acc.AssertContainsTaggedFields(t, "activemq_subscribers", records, tags) @@ -170,7 +170,7 @@ func TestURLs(t *testing.T) { URL: "http://" + ts.Listener.Addr().String(), Webadmin: "admin", } - err := plugin.Init(models.PluginConfig{Log: models.Logger{}}) + err := plugin.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) require.NoError(t, err) var acc testutil.Accumulator diff --git a/plugins/inputs/docker_log/docker_log_test.go b/plugins/inputs/docker_log/docker_log_test.go index dacfec93ab8ba..1d99a1a29d01f 100644 --- a/plugins/inputs/docker_log/docker_log_test.go +++ b/plugins/inputs/docker_log/docker_log_test.go @@ -13,7 +13,6 @@ import ( "github.com/docker/docker/pkg/stdcopy" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal" - "github.com/influxdata/telegraf/internal/models" "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" ) @@ -161,7 +160,7 @@ func Test(t *testing.T) { containerList: make(map[string]context.CancelFunc), } - err := plugin.Init(models.PluginConfig{Log: models.Logger{}}) + err := plugin.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) require.NoError(t, err) err = plugin.Gather(&acc) diff --git a/plugins/inputs/exec/exec.go b/plugins/inputs/exec/exec.go index 93fc0e8b34aef..53eab1a146804 100644 --- a/plugins/inputs/exec/exec.go +++ b/plugins/inputs/exec/exec.go @@ -14,7 +14,6 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal" - "github.com/influxdata/telegraf/internal/models" "github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/parsers" "github.com/influxdata/telegraf/plugins/parsers/nagios" @@ -56,7 +55,6 @@ type Exec struct { func NewExec() *Exec { return &Exec{ - log: models.Logger{}, runner: CommandRunner{}, Timeout: internal.Duration{Duration: time.Second * 5}, } @@ -147,7 +145,6 @@ func (e *Exec) ProcessCommand(command string, acc telegraf.Accumulator, wg *sync defer wg.Done() _, isNagios := e.parser.(*nagios.NagiosParser) - e.log.Debugf("command %#v", command) out, errbuf, runErr := e.runner.Run(command, e.Timeout.Duration) if !isNagios && runErr != nil { err := fmt.Errorf("exec: %s for command '%s': %s", runErr, command, string(errbuf)) @@ -233,7 +230,7 @@ func (e *Exec) Gather(acc telegraf.Accumulator) error { } func (e *Exec) Init(conf telegraf.PluginConfig) error { - e.log = conf.Logger() + e.log = conf.Log return nil } diff --git a/plugins/inputs/exec/exec_test.go b/plugins/inputs/exec/exec_test.go index 59ffe29cf609b..0523a181d009d 100644 --- a/plugins/inputs/exec/exec_test.go +++ b/plugins/inputs/exec/exec_test.go @@ -7,9 +7,7 @@ import ( "testing" "time" - "github.com/influxdata/telegraf/internal/models" "github.com/influxdata/telegraf/plugins/parsers" - "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -98,7 +96,7 @@ func TestExec(t *testing.T) { MetricName: "exec", }) e := &Exec{ - log: models.Logger{}, + log: testutil.Logger{}, runner: newRunnerMock([]byte(validJson), nil, nil), Commands: []string{"testcommand arg1"}, parser: parser, @@ -128,7 +126,7 @@ func TestExecMalformed(t *testing.T) { MetricName: "exec", }) e := &Exec{ - log: models.Logger{}, + log: testutil.Logger{}, runner: newRunnerMock([]byte(malformedJson), nil, nil), Commands: []string{"badcommand arg1"}, parser: parser, @@ -145,7 +143,7 @@ func TestCommandError(t *testing.T) { MetricName: "exec", }) e := &Exec{ - log: models.Logger{}, + log: testutil.Logger{}, runner: newRunnerMock(nil, nil, fmt.Errorf("exit status code 1")), Commands: []string{"badcommand"}, parser: parser, diff --git a/plugins/inputs/http/http_test.go b/plugins/inputs/http/http_test.go index d62bde4877796..cbfb8282adf5b 100644 --- a/plugins/inputs/http/http_test.go +++ b/plugins/inputs/http/http_test.go @@ -8,7 +8,7 @@ import ( "net/http/httptest" "testing" - "github.com/influxdata/telegraf/internal/models" + "github.com/influxdata/telegraf" plugin "github.com/influxdata/telegraf/plugins/inputs/http" "github.com/influxdata/telegraf/plugins/parsers" "github.com/influxdata/telegraf/testutil" @@ -38,7 +38,7 @@ func TestHTTPwithJSONFormat(t *testing.T) { plugin.SetParser(p) var acc testutil.Accumulator - plugin.Init(models.PluginConfig{Log: models.Logger{}}) + plugin.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) require.NoError(t, acc.GatherError(plugin.Gather)) require.Len(t, acc.Metrics, 1) @@ -80,7 +80,7 @@ func TestHTTPHeaders(t *testing.T) { plugin.SetParser(p) var acc testutil.Accumulator - plugin.Init(models.PluginConfig{Log: models.Logger{}}) + plugin.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) require.NoError(t, acc.GatherError(plugin.Gather)) } @@ -103,7 +103,7 @@ func TestInvalidStatusCode(t *testing.T) { plugin.SetParser(p) var acc testutil.Accumulator - plugin.Init(models.PluginConfig{Log: models.Logger{}}) + plugin.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) require.Error(t, acc.GatherError(plugin.Gather)) } @@ -129,7 +129,7 @@ func TestMethod(t *testing.T) { plugin.SetParser(p) var acc testutil.Accumulator - plugin.Init(models.PluginConfig{Log: models.Logger{}}) + plugin.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) require.NoError(t, acc.GatherError(plugin.Gather)) } @@ -223,7 +223,7 @@ func TestBodyAndContentEncoding(t *testing.T) { tt.plugin.SetParser(parser) var acc testutil.Accumulator - tt.plugin.Init(models.PluginConfig{Log: models.Logger{}}) + tt.plugin.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) err = tt.plugin.Gather(&acc) require.NoError(t, err) }) diff --git a/plugins/inputs/x509_cert/x509_cert_test.go b/plugins/inputs/x509_cert/x509_cert_test.go index f293f25b6b4d3..67f8e40e1f558 100644 --- a/plugins/inputs/x509_cert/x509_cert_test.go +++ b/plugins/inputs/x509_cert/x509_cert_test.go @@ -14,7 +14,6 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal" - "github.com/influxdata/telegraf/internal/models" "github.com/influxdata/telegraf/testutil" ) @@ -111,7 +110,7 @@ func TestGatherRemote(t *testing.T) { Sources: []string{test.server}, Timeout: internal.Duration{Duration: test.timeout}, } - sc.Init(models.PluginConfig{Log: models.Logger{}}) + sc.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) sc.InsecureSkipVerify = true testErr := false @@ -171,7 +170,7 @@ func TestGatherLocal(t *testing.T) { sc := X509Cert{ Sources: []string{f.Name()}, } - sc.Init(models.PluginConfig{Log: models.Logger{}}) + sc.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) error := false @@ -221,7 +220,7 @@ func TestGatherChain(t *testing.T) { sc := X509Cert{ Sources: []string{f.Name()}, } - sc.Init(models.PluginConfig{Log: models.Logger{}}) + sc.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) error := false @@ -241,7 +240,7 @@ func TestGatherChain(t *testing.T) { func TestStrings(t *testing.T) { sc := X509Cert{} - sc.Init(models.PluginConfig{Log: models.Logger{}}) + sc.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) tests := []struct { name string @@ -270,7 +269,7 @@ func TestGatherCert(t *testing.T) { m := &X509Cert{ Sources: []string{"https://www.influxdata.com:443"}, } - m.Init(models.PluginConfig{Log: models.Logger{}}) + m.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) var acc testutil.Accumulator err := m.Gather(&acc) diff --git a/plugins/outputs/influxdb/http.go b/plugins/outputs/influxdb/http.go index fa864be39cb4e..637836554daf7 100644 --- a/plugins/outputs/influxdb/http.go +++ b/plugins/outputs/influxdb/http.go @@ -15,7 +15,6 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal" - "github.com/influxdata/telegraf/internal/models" "github.com/influxdata/telegraf/plugins/serializers/influx" ) @@ -100,6 +99,7 @@ type HTTPConfig struct { InfluxUintSupport bool `toml:"influx_uint_support"` Serializer *influx.Serializer + log telegraf.Logger } type httpClient struct { @@ -175,7 +175,7 @@ func NewHTTPClient(config HTTPConfig) (*httpClient, error) { }, createdDatabases: make(map[string]bool), config: config, - log: models.Logger{Name: userAgent}, + log: config.log, } return client, nil } diff --git a/plugins/outputs/influxdb/http_test.go b/plugins/outputs/influxdb/http_test.go index 2b6b45eefa8d0..98ec4ef5be19d 100644 --- a/plugins/outputs/influxdb/http_test.go +++ b/plugins/outputs/influxdb/http_test.go @@ -21,6 +21,7 @@ import ( "github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/plugins/outputs/influxdb" + "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" ) @@ -471,6 +472,7 @@ func TestHTTP_Write(t *testing.T) { client, err := influxdb.NewHTTPClient(tt.config) require.NoError(t, err) + client.SetLogger(testutil.Logger{}) err = client.Write(ctx, metrics) if tt.errFunc != nil { tt.errFunc(t, err) diff --git a/plugins/outputs/influxdb/influxdb.go b/plugins/outputs/influxdb/influxdb.go index 0507de9544926..216bf775ac0a5 100644 --- a/plugins/outputs/influxdb/influxdb.go +++ b/plugins/outputs/influxdb/influxdb.go @@ -10,7 +10,6 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal" - "github.com/influxdata/telegraf/internal/models" "github.com/influxdata/telegraf/internal/tls" "github.com/influxdata/telegraf/plugins/outputs" "github.com/influxdata/telegraf/plugins/serializers/influx" @@ -291,7 +290,7 @@ func (i *InfluxDB) httpClient(ctx context.Context, url *url.URL, proxy *url.URL) } func (i *InfluxDB) Init(conf telegraf.PluginConfig) error { - i.log = conf.Logger() + i.log = conf.Log return nil } @@ -306,7 +305,6 @@ func init() { CreateUDPClientF: func(config *UDPConfig) (Client, error) { return NewUDPClient(*config) }, - log: models.Logger{Name: "outputs.influxdb"}, } }) } diff --git a/plugins/outputs/influxdb/influxdb_test.go b/plugins/outputs/influxdb/influxdb_test.go index ac42319b8bfde..6ad40a469eb52 100644 --- a/plugins/outputs/influxdb/influxdb_test.go +++ b/plugins/outputs/influxdb/influxdb_test.go @@ -8,10 +8,10 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/internal" - "github.com/influxdata/telegraf/internal/models" "github.com/influxdata/telegraf/internal/tls" "github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/plugins/outputs/influxdb" + "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" ) @@ -60,7 +60,7 @@ func TestDeprecatedURLSupport(t *testing.T) { }, } - output.Init(models.PluginConfig{Log: models.Logger{}}) + output.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) err := output.Connect() require.NoError(t, err) @@ -83,7 +83,7 @@ func TestDefaultURL(t *testing.T) { }, } - output.Init(models.PluginConfig{Log: models.Logger{}}) + output.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) err := output.Connect() require.NoError(t, err) @@ -102,7 +102,7 @@ func TestConnectUDPConfig(t *testing.T) { return &MockClient{}, nil }, } - output.Init(models.PluginConfig{Log: models.Logger{}}) + output.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) err := output.Connect() require.NoError(t, err) @@ -146,7 +146,7 @@ func TestConnectHTTPConfig(t *testing.T) { }, } - output.Init(models.PluginConfig{Log: models.Logger{}}) + output.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) err := output.Connect() require.NoError(t, err) @@ -195,7 +195,7 @@ func TestWriteRecreateDatabaseIfDatabaseNotFound(t *testing.T) { }, } - output.Init(models.PluginConfig{Log: models.Logger{}}) + output.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) err := output.Connect() require.NoError(t, err) diff --git a/plugins/outputs/influxdb/udp.go b/plugins/outputs/influxdb/udp.go index 65c53c2b04a94..76fdb786241bc 100644 --- a/plugins/outputs/influxdb/udp.go +++ b/plugins/outputs/influxdb/udp.go @@ -31,6 +31,7 @@ type UDPConfig struct { URL *url.URL Serializer *influx.Serializer Dialer Dialer + Log telegraf.Logger } func NewUDPClient(config UDPConfig) (*udpClient, error) { diff --git a/plugins/outputs/influxdb/udp_test.go b/plugins/outputs/influxdb/udp_test.go index 0d17a2b7a5a4f..61c64ff7750c2 100644 --- a/plugins/outputs/influxdb/udp_test.go +++ b/plugins/outputs/influxdb/udp_test.go @@ -12,9 +12,9 @@ import ( "time" "github.com/influxdata/telegraf" - "github.com/influxdata/telegraf/internal/models" "github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/plugins/outputs/influxdb" + "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" ) @@ -79,7 +79,7 @@ func TestUDP_URL(t *testing.T) { } client, err := influxdb.NewUDPClient(config) - client.SetLogger(models.Logger{}) + client.SetLogger(testutil.Logger{}) require.NoError(t, err) require.Equal(t, u.String(), client.URL()) @@ -103,7 +103,7 @@ func TestUDP_Simple(t *testing.T) { }, } client, err := influxdb.NewUDPClient(config) - client.SetLogger(models.Logger{}) + client.SetLogger(testutil.Logger{}) require.NoError(t, err) ctx := context.Background() @@ -130,7 +130,7 @@ func TestUDP_DialError(t *testing.T) { }, } client, err := influxdb.NewUDPClient(config) - client.SetLogger(models.Logger{}) + client.SetLogger(testutil.Logger{}) require.NoError(t, err) ctx := context.Background() @@ -160,7 +160,7 @@ func TestUDP_WriteError(t *testing.T) { }, } client, err := influxdb.NewUDPClient(config) - client.SetLogger(models.Logger{}) + client.SetLogger(testutil.Logger{}) require.NoError(t, err) ctx := context.Background() @@ -224,7 +224,7 @@ func TestUDP_ErrorLogging(t *testing.T) { log.SetOutput(&b) client, err := influxdb.NewUDPClient(tt.config) - client.SetLogger(models.Logger{}) + client.SetLogger(testutil.Logger{}) require.NoError(t, err) ctx := context.Background() @@ -268,7 +268,7 @@ func TestUDP_WriteWithRealConn(t *testing.T) { URL: u, } client, err := influxdb.NewUDPClient(config) - client.SetLogger(models.Logger{}) + client.SetLogger(testutil.Logger{}) require.NoError(t, err) ctx := context.Background() diff --git a/testutil/log.go b/testutil/log.go new file mode 100644 index 0000000000000..5e0458dc73c2e --- /dev/null +++ b/testutil/log.go @@ -0,0 +1,50 @@ +package testutil + +import ( + "log" +) + +// Logger defines a logging structure for plugins. +type Logger struct { + Name string // Name is the plugin name, will be printed in the `[]`. +} + +// Errorf logs an error message, patterned after log.Printf. +func (l Logger) Errorf(format string, args ...interface{}) { + log.Printf("E! ["+l.Name+"] "+format, args...) +} + +// Error logs an error message, patterned after log.Print. +func (l Logger) Error(args ...interface{}) { + log.Print(append([]interface{}{"E! [" + l.Name + "] "}, args...)...) +} + +// Debugf logs a debug message, patterned after log.Printf. +func (l Logger) Debugf(format string, args ...interface{}) { + log.Printf("D! ["+l.Name+"] "+format, args...) +} + +// Debug logs a debug message, patterned after log.Print. +func (l Logger) Debug(args ...interface{}) { + log.Print(append([]interface{}{"D! [" + l.Name + "] "}, args...)...) +} + +// Warnf logs a warning message, patterned after log.Printf. +func (l Logger) Warnf(format string, args ...interface{}) { + log.Printf("W! ["+l.Name+"] "+format, args...) +} + +// Warn logs a warning message, patterned after log.Print. +func (l Logger) Warn(args ...interface{}) { + log.Print(append([]interface{}{"W! [" + l.Name + "] "}, args...)...) +} + +// Infof logs an information message, patterned after log.Printf. +func (l Logger) Infof(format string, args ...interface{}) { + log.Printf("I! ["+l.Name+"] "+format, args...) +} + +// Info logs an information message, patterned after log.Print. +func (l Logger) Info(args ...interface{}) { + log.Print(append([]interface{}{"I! [" + l.Name + "] "}, args...)...) +} From 17f64c2bd9c0ee7791ff0379bdf22cc412792cf1 Mon Sep 17 00:00:00 2001 From: greg linton Date: Mon, 19 Aug 2019 15:14:54 -0600 Subject: [PATCH 11/15] Set plugin's logger outside of Init --- agent/agent.go | 4 +-- internal/models/running_aggregator.go | 4 ++- internal/models/running_input.go | 4 ++- internal/models/running_output.go | 28 +++++++++++++++++++- internal/models/running_processor.go | 4 ++- plugin.go | 7 +---- plugins/inputs/activemq/activemq.go | 2 +- plugins/inputs/activemq/activemq_test.go | 9 +++---- plugins/inputs/docker_log/docker_log.go | 2 +- plugins/inputs/docker_log/docker_log_test.go | 2 +- plugins/inputs/exec/exec.go | 4 +-- plugins/inputs/fireboard/fireboard.go | 2 +- plugins/inputs/http/http.go | 2 +- plugins/inputs/http/http_test.go | 11 ++++---- plugins/inputs/ping/ping.go | 2 +- plugins/inputs/x509_cert/x509_cert.go | 2 +- plugins/inputs/x509_cert/x509_cert_test.go | 10 +++---- plugins/outputs/influxdb/influxdb.go | 18 +++++-------- plugins/outputs/influxdb/influxdb_test.go | 10 +++---- 19 files changed, 72 insertions(+), 55 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 3fbd35518c91f..a0a772613500d 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -548,7 +548,7 @@ func (a *Agent) flush( logError := func(err error) { if err != nil { - log.Printf("E! [agent] Error writing to [%s]: %v", output.LogName(), err) + log.Printf("E! [agent] Error writing to %s: %v", output.LogName(), err) } } @@ -614,7 +614,7 @@ func (a *Agent) initPlugins() error { err := input.Init() if err != nil { return fmt.Errorf("could not initialize input %s: %v", - input.Config.Name, err) + input.LogName(), err) } } for _, processor := range a.Config.Processors { diff --git a/internal/models/running_aggregator.go b/internal/models/running_aggregator.go index 6f294ac7b956e..a5dd5c4a5e67f 100644 --- a/internal/models/running_aggregator.go +++ b/internal/models/running_aggregator.go @@ -81,8 +81,10 @@ func (r *RunningAggregator) LogName() string { } func (r *RunningAggregator) Init() error { + setLogIfExist(r.Aggregator, Logger{Name: r.LogName()}) + if p, ok := r.Aggregator.(telegraf.Initializer); ok { - err := p.Init(telegraf.PluginConfig{Log: Logger{Name: r.LogName()}}) + err := p.Init() if err != nil { return err } diff --git a/internal/models/running_input.go b/internal/models/running_input.go index 6fa4ec7f478a6..215c70254796b 100644 --- a/internal/models/running_input.go +++ b/internal/models/running_input.go @@ -65,8 +65,10 @@ func (r *RunningInput) metricFiltered(metric telegraf.Metric) { } func (r *RunningInput) Init() error { + setLogIfExist(r.Input, Logger{Name: r.LogName()}) + if p, ok := r.Input.(telegraf.Initializer); ok { - err := p.Init(telegraf.PluginConfig{Log: Logger{Name: r.LogName()}}) + err := p.Init() if err != nil { return err } diff --git a/internal/models/running_output.go b/internal/models/running_output.go index 44776232f516e..601150df7cb19 100644 --- a/internal/models/running_output.go +++ b/internal/models/running_output.go @@ -2,6 +2,7 @@ package models import ( "log" + "reflect" "sync" "sync/atomic" "time" @@ -109,15 +110,40 @@ func (ro *RunningOutput) metricFiltered(metric telegraf.Metric) { } func (ro *RunningOutput) Init() error { + setLogIfExist(ro.Output, Logger{Name: ro.LogName()}) + if p, ok := ro.Output.(telegraf.Initializer); ok { - err := p.Init(telegraf.PluginConfig{Log: Logger{Name: ro.LogName()}}) + err := p.Init() if err != nil { return err } + } return nil } +func setLogIfExist(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 +} + // AddMetric adds a metric to the output. // // Takes ownership of metric diff --git a/internal/models/running_processor.go b/internal/models/running_processor.go index 7162e6a997699..fbdb905913882 100644 --- a/internal/models/running_processor.go +++ b/internal/models/running_processor.go @@ -51,8 +51,10 @@ func containsMetric(item telegraf.Metric, metrics []telegraf.Metric) bool { } func (rp *RunningProcessor) Init() error { + setLogIfExist(rp.Processor, Logger{Name: rp.LogName()}) + if p, ok := rp.Processor.(telegraf.Initializer); ok { - err := p.Init(telegraf.PluginConfig{Log: Logger{Name: rp.LogName()}}) + err := p.Init() if err != nil { return err } diff --git a/plugin.go b/plugin.go index 043164718e8f9..f79721958c2ad 100644 --- a/plugin.go +++ b/plugin.go @@ -6,12 +6,7 @@ package telegraf type Initializer interface { // Init performs one time setup of the plugin and returns an error if the // configuration is invalid. - Init(PluginConfig) error -} - -// PluginConfig contains individualized plugin configuration. -type PluginConfig struct { - Log Logger + Init() error } // Logger defines an interface for logging. diff --git a/plugins/inputs/activemq/activemq.go b/plugins/inputs/activemq/activemq.go index 8a0827fb7b57d..9d08661b72492 100644 --- a/plugins/inputs/activemq/activemq.go +++ b/plugins/inputs/activemq/activemq.go @@ -133,7 +133,7 @@ func (a *ActiveMQ) createHttpClient() (*http.Client, error) { return client, nil } -func (a *ActiveMQ) Init(conf telegraf.PluginConfig) error { +func (a *ActiveMQ) Init() error { if a.ResponseTimeout.Duration < time.Second { a.ResponseTimeout.Duration = time.Second * 5 } diff --git a/plugins/inputs/activemq/activemq_test.go b/plugins/inputs/activemq/activemq_test.go index 887d44eefbf30..407a381775adc 100644 --- a/plugins/inputs/activemq/activemq_test.go +++ b/plugins/inputs/activemq/activemq_test.go @@ -6,7 +6,6 @@ import ( "net/http/httptest" "testing" - "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" ) @@ -51,7 +50,7 @@ func TestGatherQueuesMetrics(t *testing.T) { activeMQ := new(ActiveMQ) activeMQ.Server = "localhost" activeMQ.Port = 8161 - activeMQ.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + activeMQ.Init() activeMQ.GatherQueuesMetrics(&acc, queues) acc.AssertContainsTaggedFields(t, "activemq_queues", records, tags) @@ -98,7 +97,7 @@ func TestGatherTopicsMetrics(t *testing.T) { activeMQ := new(ActiveMQ) activeMQ.Server = "localhost" activeMQ.Port = 8161 - activeMQ.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + activeMQ.Init() activeMQ.GatherTopicsMetrics(&acc, topics) acc.AssertContainsTaggedFields(t, "activemq_topics", records, tags) @@ -139,7 +138,7 @@ func TestGatherSubscribersMetrics(t *testing.T) { activeMQ := new(ActiveMQ) activeMQ.Server = "localhost" activeMQ.Port = 8161 - activeMQ.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + activeMQ.Init() activeMQ.GatherSubscribersMetrics(&acc, subscribers) acc.AssertContainsTaggedFields(t, "activemq_subscribers", records, tags) @@ -170,7 +169,7 @@ func TestURLs(t *testing.T) { URL: "http://" + ts.Listener.Addr().String(), Webadmin: "admin", } - err := plugin.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + err := plugin.Init() require.NoError(t, err) var acc testutil.Accumulator diff --git a/plugins/inputs/docker_log/docker_log.go b/plugins/inputs/docker_log/docker_log.go index 467fe3a2b67a4..6a675219fc859 100644 --- a/plugins/inputs/docker_log/docker_log.go +++ b/plugins/inputs/docker_log/docker_log.go @@ -106,7 +106,7 @@ func (d *DockerLogs) SampleConfig() string { return sampleConfig } -func (d *DockerLogs) Init(conf telegraf.PluginConfig) error { +func (d *DockerLogs) Init() error { var err error if d.Endpoint == "ENV" { d.client, err = d.newEnvClient() diff --git a/plugins/inputs/docker_log/docker_log_test.go b/plugins/inputs/docker_log/docker_log_test.go index 1d99a1a29d01f..ce61f6135a9fc 100644 --- a/plugins/inputs/docker_log/docker_log_test.go +++ b/plugins/inputs/docker_log/docker_log_test.go @@ -160,7 +160,7 @@ func Test(t *testing.T) { containerList: make(map[string]context.CancelFunc), } - err := plugin.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + err := plugin.Init() require.NoError(t, err) err = plugin.Gather(&acc) diff --git a/plugins/inputs/exec/exec.go b/plugins/inputs/exec/exec.go index 53eab1a146804..2d3643ad0d2c5 100644 --- a/plugins/inputs/exec/exec.go +++ b/plugins/inputs/exec/exec.go @@ -229,9 +229,7 @@ func (e *Exec) Gather(acc telegraf.Accumulator) error { return nil } -func (e *Exec) Init(conf telegraf.PluginConfig) error { - e.log = conf.Log - +func (e *Exec) Init() error { return nil } diff --git a/plugins/inputs/fireboard/fireboard.go b/plugins/inputs/fireboard/fireboard.go index 3b3fe72dbb40d..2e9c7b0254320 100644 --- a/plugins/inputs/fireboard/fireboard.go +++ b/plugins/inputs/fireboard/fireboard.go @@ -68,7 +68,7 @@ func (r *Fireboard) Description() string { } // Init the things -func (r *Fireboard) Init(conf telegraf.PluginConfig) error { +func (r *Fireboard) Init() error { if len(r.AuthToken) == 0 { return fmt.Errorf("You must specify an authToken") diff --git a/plugins/inputs/http/http.go b/plugins/inputs/http/http.go index cdefc737d5008..34db9d287549f 100644 --- a/plugins/inputs/http/http.go +++ b/plugins/inputs/http/http.go @@ -88,7 +88,7 @@ func (*HTTP) Description() string { return "Read formatted metrics from one or more HTTP endpoints" } -func (h *HTTP) Init(conf telegraf.PluginConfig) error { +func (h *HTTP) Init() error { tlsCfg, err := h.ClientConfig.TLSConfig() if err != nil { return err diff --git a/plugins/inputs/http/http_test.go b/plugins/inputs/http/http_test.go index cbfb8282adf5b..21eff62650f7c 100644 --- a/plugins/inputs/http/http_test.go +++ b/plugins/inputs/http/http_test.go @@ -8,7 +8,6 @@ import ( "net/http/httptest" "testing" - "github.com/influxdata/telegraf" plugin "github.com/influxdata/telegraf/plugins/inputs/http" "github.com/influxdata/telegraf/plugins/parsers" "github.com/influxdata/telegraf/testutil" @@ -38,7 +37,7 @@ func TestHTTPwithJSONFormat(t *testing.T) { plugin.SetParser(p) var acc testutil.Accumulator - plugin.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + plugin.Init() require.NoError(t, acc.GatherError(plugin.Gather)) require.Len(t, acc.Metrics, 1) @@ -80,7 +79,7 @@ func TestHTTPHeaders(t *testing.T) { plugin.SetParser(p) var acc testutil.Accumulator - plugin.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + plugin.Init() require.NoError(t, acc.GatherError(plugin.Gather)) } @@ -103,7 +102,7 @@ func TestInvalidStatusCode(t *testing.T) { plugin.SetParser(p) var acc testutil.Accumulator - plugin.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + plugin.Init() require.Error(t, acc.GatherError(plugin.Gather)) } @@ -129,7 +128,7 @@ func TestMethod(t *testing.T) { plugin.SetParser(p) var acc testutil.Accumulator - plugin.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + plugin.Init() require.NoError(t, acc.GatherError(plugin.Gather)) } @@ -223,7 +222,7 @@ func TestBodyAndContentEncoding(t *testing.T) { tt.plugin.SetParser(parser) var acc testutil.Accumulator - tt.plugin.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + tt.plugin.Init() err = tt.plugin.Gather(&acc) require.NoError(t, err) }) diff --git a/plugins/inputs/ping/ping.go b/plugins/inputs/ping/ping.go index 9a9ea58a107b3..469859a345937 100644 --- a/plugins/inputs/ping/ping.go +++ b/plugins/inputs/ping/ping.go @@ -327,7 +327,7 @@ func onFin(packetsSent int, resps []*ping.Response, destination string) (map[str } // Init ensures the plugin is configured correctly. -func (p *Ping) Init(conf telegraf.PluginConfig) error { +func (p *Ping) Init() error { if p.Count < 1 { return errors.New("bad number of packets to transmit") } diff --git a/plugins/inputs/x509_cert/x509_cert.go b/plugins/inputs/x509_cert/x509_cert.go index fd605f38d4228..825fd5eeb18d1 100644 --- a/plugins/inputs/x509_cert/x509_cert.go +++ b/plugins/inputs/x509_cert/x509_cert.go @@ -208,7 +208,7 @@ func (c *X509Cert) Gather(acc telegraf.Accumulator) error { return nil } -func (c *X509Cert) Init(conf telegraf.PluginConfig) error { +func (c *X509Cert) Init() error { tlsCfg, err := c.ClientConfig.TLSConfig() if err != nil { return err diff --git a/plugins/inputs/x509_cert/x509_cert_test.go b/plugins/inputs/x509_cert/x509_cert_test.go index 67f8e40e1f558..188b510d263d9 100644 --- a/plugins/inputs/x509_cert/x509_cert_test.go +++ b/plugins/inputs/x509_cert/x509_cert_test.go @@ -110,7 +110,7 @@ func TestGatherRemote(t *testing.T) { Sources: []string{test.server}, Timeout: internal.Duration{Duration: test.timeout}, } - sc.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + sc.Init() sc.InsecureSkipVerify = true testErr := false @@ -170,7 +170,7 @@ func TestGatherLocal(t *testing.T) { sc := X509Cert{ Sources: []string{f.Name()}, } - sc.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + sc.Init() error := false @@ -220,7 +220,7 @@ func TestGatherChain(t *testing.T) { sc := X509Cert{ Sources: []string{f.Name()}, } - sc.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + sc.Init() error := false @@ -240,7 +240,7 @@ func TestGatherChain(t *testing.T) { func TestStrings(t *testing.T) { sc := X509Cert{} - sc.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + sc.Init() tests := []struct { name string @@ -269,7 +269,7 @@ func TestGatherCert(t *testing.T) { m := &X509Cert{ Sources: []string{"https://www.influxdata.com:443"}, } - m.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + m.Init() var acc testutil.Accumulator err := m.Gather(&acc) diff --git a/plugins/outputs/influxdb/influxdb.go b/plugins/outputs/influxdb/influxdb.go index 571841962508b..0a6f6669629d4 100644 --- a/plugins/outputs/influxdb/influxdb.go +++ b/plugins/outputs/influxdb/influxdb.go @@ -59,7 +59,7 @@ type InfluxDB struct { CreateUDPClientF func(config *UDPConfig) (Client, error) serializer *influx.Serializer - log telegraf.Logger + Log telegraf.Logger } var sampleConfig = ` @@ -172,7 +172,7 @@ func (i *InfluxDB) Connect() error { return err } - c.SetLogger(i.log) + c.SetLogger(i.Log) i.clients = append(i.clients, c) case "http", "https", "unix": @@ -181,7 +181,7 @@ func (i *InfluxDB) Connect() error { return err } - c.SetLogger(i.log) + c.SetLogger(i.Log) i.clients = append(i.clients, c) default: @@ -226,13 +226,13 @@ func (i *InfluxDB) Write(metrics []telegraf.Metric) error { if !i.SkipDatabaseCreation { err := client.CreateDatabase(ctx, apiError.Database) if err != nil { - i.log.Errorf("when writing to [%s]: database %q not found and failed to recreate", + i.Log.Errorf("when writing to [%s]: database %q not found and failed to recreate", client.URL(), apiError.Database) } } } - i.log.Errorf("when writing to [%s]: %v", client.URL(), err) + i.Log.Errorf("when writing to [%s]: %v", client.URL(), err) } return errors.New("could not write any address") @@ -286,7 +286,7 @@ func (i *InfluxDB) httpClient(ctx context.Context, url *url.URL, proxy *url.URL) if !i.SkipDatabaseCreation { err = c.CreateDatabase(ctx, c.Database()) if err != nil { - i.log.Warnf("when writing to [%s]: database %q creation failed: %v", + i.Log.Warnf("when writing to [%s]: database %q creation failed: %v", c.URL(), i.Database, err) } } @@ -294,12 +294,6 @@ func (i *InfluxDB) httpClient(ctx context.Context, url *url.URL, proxy *url.URL) return c, nil } -func (i *InfluxDB) Init(conf telegraf.PluginConfig) error { - i.log = conf.Log - - return nil -} - func init() { outputs.Add("influxdb", func() telegraf.Output { return &InfluxDB{ diff --git a/plugins/outputs/influxdb/influxdb_test.go b/plugins/outputs/influxdb/influxdb_test.go index 6ad40a469eb52..4b86de4de4dba 100644 --- a/plugins/outputs/influxdb/influxdb_test.go +++ b/plugins/outputs/influxdb/influxdb_test.go @@ -60,7 +60,7 @@ func TestDeprecatedURLSupport(t *testing.T) { }, } - output.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + output.Log = testutil.Logger{} err := output.Connect() require.NoError(t, err) @@ -83,7 +83,7 @@ func TestDefaultURL(t *testing.T) { }, } - output.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + output.Log = testutil.Logger{} err := output.Connect() require.NoError(t, err) @@ -102,7 +102,7 @@ func TestConnectUDPConfig(t *testing.T) { return &MockClient{}, nil }, } - output.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + output.Log = testutil.Logger{} err := output.Connect() require.NoError(t, err) @@ -146,7 +146,7 @@ func TestConnectHTTPConfig(t *testing.T) { }, } - output.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + output.Log = testutil.Logger{} err := output.Connect() require.NoError(t, err) @@ -195,7 +195,7 @@ func TestWriteRecreateDatabaseIfDatabaseNotFound(t *testing.T) { }, } - output.Init(telegraf.PluginConfig{Log: testutil.Logger{}}) + output.Log = testutil.Logger{} err := output.Connect() require.NoError(t, err) From 9365c5c4121a1c26aff0162c51343612e558eca1 Mon Sep 17 00:00:00 2001 From: greg linton Date: Tue, 20 Aug 2019 13:12:33 -0600 Subject: [PATCH 12/15] Pass selfstat when creating logger --- internal/models/log.go | 42 +++++------------ internal/models/log_test.go | 66 +++++++++++++++++---------- internal/models/running_aggregator.go | 21 +++++++-- internal/models/running_input.go | 17 +++++-- internal/models/running_output.go | 19 ++++++-- internal/models/running_processor.go | 7 ++- 6 files changed, 104 insertions(+), 68 deletions(-) diff --git a/internal/models/log.go b/internal/models/log.go index 8eb1b931fd3f2..6d0e0f11e65e4 100644 --- a/internal/models/log.go +++ b/internal/models/log.go @@ -2,74 +2,54 @@ package models import ( "log" - "strings" "github.com/influxdata/telegraf/selfstat" ) // Logger defines a logging structure for plugins. type Logger struct { + Errs selfstat.Stat Name string // Name is the plugin name, will be printed in the `[]`. } // Errorf logs an error message, patterned after log.Printf. -func (l Logger) Errorf(format string, args ...interface{}) { - l.addError() +func (l *Logger) Errorf(format string, args ...interface{}) { + l.Errs.Incr(1) log.Printf("E! ["+l.Name+"] "+format, args...) } // Error logs an error message, patterned after log.Print. -func (l Logger) Error(args ...interface{}) { - l.addError() +func (l *Logger) Error(args ...interface{}) { + l.Errs.Incr(1) log.Print(append([]interface{}{"E! [" + l.Name + "] "}, args...)...) } // Debugf logs a debug message, patterned after log.Printf. -func (l Logger) Debugf(format string, args ...interface{}) { +func (l *Logger) Debugf(format string, args ...interface{}) { log.Printf("D! ["+l.Name+"] "+format, args...) } // Debug logs a debug message, patterned after log.Print. -func (l Logger) Debug(args ...interface{}) { +func (l *Logger) Debug(args ...interface{}) { log.Print(append([]interface{}{"D! [" + l.Name + "] "}, args...)...) } // Warnf logs a warning message, patterned after log.Printf. -func (l Logger) Warnf(format string, args ...interface{}) { +func (l *Logger) Warnf(format string, args ...interface{}) { log.Printf("W! ["+l.Name+"] "+format, args...) } // Warn logs a warning message, patterned after log.Print. -func (l Logger) Warn(args ...interface{}) { +func (l *Logger) Warn(args ...interface{}) { log.Print(append([]interface{}{"W! [" + l.Name + "] "}, args...)...) } // Infof logs an information message, patterned after log.Printf. -func (l Logger) Infof(format string, args ...interface{}) { +func (l *Logger) Infof(format string, args ...interface{}) { log.Printf("I! ["+l.Name+"] "+format, args...) } // Info logs an information message, patterned after log.Print. -func (l Logger) Info(args ...interface{}) { +func (l *Logger) Info(args ...interface{}) { log.Print(append([]interface{}{"I! [" + l.Name + "] "}, args...)...) } - -func (l Logger) addError() { - switch { - case strings.HasPrefix(l.Name, "aggregator"): - aErrors.Incr(1) - case strings.HasPrefix(l.Name, "input"): - iErrors.Incr(1) - case strings.HasPrefix(l.Name, "output"): - oErrors.Incr(1) - case strings.HasPrefix(l.Name, "processor"): - pErrors.Incr(1) - } -} - -var ( - aErrors = selfstat.Register("agent", "aggregator_errors", map[string]string{}) - iErrors = selfstat.Register("agent", "input_errors", map[string]string{}) - oErrors = selfstat.Register("agent", "output_errors", map[string]string{}) - pErrors = selfstat.Register("agent", "processor_errors", map[string]string{}) -) diff --git a/internal/models/log_test.go b/internal/models/log_test.go index 36ed77ea2ddd2..d4bb6ca09b019 100644 --- a/internal/models/log_test.go +++ b/internal/models/log_test.go @@ -3,50 +3,68 @@ package models import ( "testing" + "github.com/influxdata/telegraf/selfstat" "github.com/stretchr/testify/require" ) -func TestErrorCount(t *testing.T) { - iErrors.Set(0) - aErrors.Set(0) - oErrors.Set(0) - pErrors.Set(0) +func TestErrorCounting(t *testing.T) { + iLog := Logger{Name: "inputs.test", Errs: selfstat.Register( + "gather", + "errors", + map[string]string{"input": "test"}, + )} + iLog.Error("something went wrong") + iLog.Errorf("something went wrong") - log := Logger{Name: "inputs.test"} - log.Errorf("something went wrong") - log.Error("something went wrong") + aLog := Logger{Name: "aggregators.test", Errs: selfstat.Register( + "aggregate", + "errors", + map[string]string{"aggregator": "test"}, + )} + aLog.Name = "aggregators.test" + aLog.Error("another thing happened") - log.Name = "aggregators.test" - log.Error("another thing happened") + oLog := Logger{Name: "outputs.test", Errs: selfstat.Register( + "write", + "errors", + map[string]string{"output": "test"}, + )} + oLog.Error("another thing happened") - log.Name = "outputs.test" - log.Error("another thing happened") + pLog := Logger{Name: "processors.test", Errs: selfstat.Register( + "process", + "errors", + map[string]string{"processor": "test"}, + )} + pLog.Error("another thing happened") - log.Name = "processors.test" - log.Error("another thing happened") - - require.Equal(t, int64(2), iErrors.Get()) - require.Equal(t, int64(1), aErrors.Get()) - require.Equal(t, int64(1), oErrors.Get()) - require.Equal(t, int64(1), pErrors.Get()) + require.Equal(t, int64(2), iLog.Errs.Get()) + require.Equal(t, int64(1), aLog.Errs.Get()) + require.Equal(t, int64(1), oLog.Errs.Get()) + require.Equal(t, int64(1), pLog.Errs.Get()) } func TestLogging(t *testing.T) { - iErrors.Set(0) - log := Logger{Name: "inputs.test"} + log := Logger{Name: "inputs.test", Errs: selfstat.Register( + "gather", + "errors", + map[string]string{"input": "test"}, + )} + + log.Errs.Set(0) log.Debugf("something happened") log.Debug("something happened") log.Warnf("something happened") log.Warn("something happened") - require.Equal(t, int64(0), iErrors.Get()) + require.Equal(t, int64(0), log.Errs.Get()) log.Infof("something happened") log.Info("something happened") - require.Equal(t, int64(0), iErrors.Get()) + require.Equal(t, int64(0), log.Errs.Get()) log.Errorf("something happened") log.Error("something happened") - require.Equal(t, int64(2), iErrors.Get()) + require.Equal(t, int64(2), log.Errs.Get()) } diff --git a/internal/models/running_aggregator.go b/internal/models/running_aggregator.go index a5dd5c4a5e67f..0cd9bc4e28925 100644 --- a/internal/models/running_aggregator.go +++ b/internal/models/running_aggregator.go @@ -33,26 +33,33 @@ func NewRunningAggregator( MetricsPushed: selfstat.Register( "aggregate", "metrics_pushed", - map[string]string{"aggregator": config.Name}, + map[string]string{"aggregator": config.LogName()}, ), MetricsFiltered: selfstat.Register( "aggregate", "metrics_filtered", - map[string]string{"aggregator": config.Name}, + map[string]string{"aggregator": config.LogName()}, ), MetricsDropped: selfstat.Register( "aggregate", "metrics_dropped", - map[string]string{"aggregator": config.Name}, + map[string]string{"aggregator": config.LogName()}, ), PushTime: selfstat.Register( "aggregate", "push_time_ns", - map[string]string{"aggregator": config.Name}, + map[string]string{"aggregator": config.LogName()}, ), } } +func (c *AggregatorConfig) LogName() string { + if c.Alias == "" { + return c.Name + } + return c.Name + "::" + c.Alias +} + // AggregatorConfig is the common config for all aggregators. type AggregatorConfig struct { Name string @@ -81,7 +88,11 @@ func (r *RunningAggregator) LogName() string { } func (r *RunningAggregator) Init() error { - setLogIfExist(r.Aggregator, Logger{Name: r.LogName()}) + setLogIfExist(r.Aggregator, &Logger{ + Name: r.LogName(), + Errs: selfstat.Register("aggregate", "errors", + map[string]string{"aggregator": r.LogName()}), + }) if p, ok := r.Aggregator.(telegraf.Initializer); ok { err := p.Init() diff --git a/internal/models/running_input.go b/internal/models/running_input.go index 215c70254796b..af5f616e1c867 100644 --- a/internal/models/running_input.go +++ b/internal/models/running_input.go @@ -26,12 +26,12 @@ func NewRunningInput(input telegraf.Input, config *InputConfig) *RunningInput { MetricsGathered: selfstat.Register( "gather", "metrics_gathered", - map[string]string{"input": config.Name}, + map[string]string{"input": config.LogName()}, ), GatherTime: selfstat.RegisterTiming( "gather", "gather_time_ns", - map[string]string{"input": config.Name}, + map[string]string{"input": config.LogName()}, ), } } @@ -49,6 +49,13 @@ type InputConfig struct { Filter Filter } +func (c *InputConfig) LogName() string { + if c.Alias == "" { + return c.Name + } + return c.Name + "::" + c.Alias +} + func (r *RunningInput) Name() string { return "inputs." + r.Config.Name } @@ -65,7 +72,11 @@ func (r *RunningInput) metricFiltered(metric telegraf.Metric) { } func (r *RunningInput) Init() error { - setLogIfExist(r.Input, Logger{Name: r.LogName()}) + setLogIfExist(r.Input, &Logger{ + Name: r.LogName(), + Errs: selfstat.Register("gather", "errors", + map[string]string{"input": r.LogName()}), + }) if p, ok := r.Input.(telegraf.Initializer); ok { err := p.Init() diff --git a/internal/models/running_output.go b/internal/models/running_output.go index 601150df7cb19..2e4e614c9c74a 100644 --- a/internal/models/running_output.go +++ b/internal/models/running_output.go @@ -72,7 +72,7 @@ func NewRunningOutput( } ro := &RunningOutput{ - buffer: NewBuffer(name, bufferLimit), + buffer: NewBuffer(conf.LogName(), bufferLimit), BatchReady: make(chan time.Time, 1), Output: output, Config: conf, @@ -81,18 +81,25 @@ func NewRunningOutput( MetricsFiltered: selfstat.Register( "write", "metrics_filtered", - map[string]string{"output": name}, + map[string]string{"output": conf.LogName()}, ), WriteTime: selfstat.RegisterTiming( "write", "write_time_ns", - map[string]string{"output": name}, + map[string]string{"output": conf.LogName()}, ), } return ro } +func (c *OutputConfig) LogName() string { + if c.Alias == "" { + return c.Name + } + return c.Name + "::" + c.Alias +} + func (ro *RunningOutput) Name() string { return "outputs." + ro.Config.Name } @@ -110,7 +117,11 @@ func (ro *RunningOutput) metricFiltered(metric telegraf.Metric) { } func (ro *RunningOutput) Init() error { - setLogIfExist(ro.Output, Logger{Name: ro.LogName()}) + setLogIfExist(ro.Output, &Logger{ + Name: ro.LogName(), + Errs: selfstat.Register("write", "errors", + map[string]string{"output": ro.LogName()}), + }) if p, ok := ro.Output.(telegraf.Initializer); ok { err := p.Init() diff --git a/internal/models/running_processor.go b/internal/models/running_processor.go index fbdb905913882..7d3874be5e880 100644 --- a/internal/models/running_processor.go +++ b/internal/models/running_processor.go @@ -4,6 +4,7 @@ import ( "sync" "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/selfstat" ) type RunningProcessor struct { @@ -51,7 +52,11 @@ func containsMetric(item telegraf.Metric, metrics []telegraf.Metric) bool { } func (rp *RunningProcessor) Init() error { - setLogIfExist(rp.Processor, Logger{Name: rp.LogName()}) + setLogIfExist(rp.Processor, &Logger{ + Name: rp.LogName(), + Errs: selfstat.Register("process", "errors", + map[string]string{"processor": rp.LogName()}), + }) if p, ok := rp.Processor.(telegraf.Initializer); ok { err := p.Init() From 997f2cfc94d093fc70574153698ff1703b066229 Mon Sep 17 00:00:00 2001 From: greg linton Date: Wed, 21 Aug 2019 11:52:34 -0600 Subject: [PATCH 13/15] Remove reference to PluginConfig --- docs/AGGREGATORS.md | 2 +- docs/INPUTS.md | 2 +- docs/OUTPUTS.md | 2 +- docs/PROCESSORS.md | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/AGGREGATORS.md b/docs/AGGREGATORS.md index 4af69c27384a2..a5930a3e0df6d 100644 --- a/docs/AGGREGATORS.md +++ b/docs/AGGREGATORS.md @@ -52,7 +52,7 @@ var sampleConfig = ` drop_original = false ` -func (m *Min) Init(telegraf.PluginConfig) error { +func (m *Min) Init() error { return nil } diff --git a/docs/INPUTS.md b/docs/INPUTS.md index 0ce768020a37f..f8e906f318fee 100644 --- a/docs/INPUTS.md +++ b/docs/INPUTS.md @@ -52,7 +52,7 @@ func (s *Simple) SampleConfig() string { ` } -func (s *Simple) Init(telegraf.PluginConfig) error { +func (s *Simple) Init() error { return nil } diff --git a/docs/OUTPUTS.md b/docs/OUTPUTS.md index ab3577870a43e..9d89491cc39d7 100644 --- a/docs/OUTPUTS.md +++ b/docs/OUTPUTS.md @@ -43,7 +43,7 @@ func (s *Simple) SampleConfig() string { ` } -func (s *Simple) Init(telegraf.PluginConfig) error { +func (s *Simple) Init() error { return nil } diff --git a/docs/PROCESSORS.md b/docs/PROCESSORS.md index 30e99fd11cdeb..6ea82fdae3309 100644 --- a/docs/PROCESSORS.md +++ b/docs/PROCESSORS.md @@ -46,7 +46,7 @@ func (p *Printer) Description() string { return "Print all metrics that pass through this filter." } -func (p *Printer) Init(telegraf.PluginConfig) error { +func (p *Printer) Init() error { return nil } From 7b26f1a2f92eeccdbf70287e4b026baea2367e7c Mon Sep 17 00:00:00 2001 From: greg linton Date: Wed, 21 Aug 2019 12:02:22 -0600 Subject: [PATCH 14/15] Set alias as a separate tag on plugin selfstat --- internal/models/running_aggregator.go | 17 +++++------------ internal/models/running_input.go | 13 +++---------- internal/models/running_output.go | 14 +++++++------- internal/models/running_processor.go | 10 +++++----- 4 files changed, 20 insertions(+), 34 deletions(-) diff --git a/internal/models/running_aggregator.go b/internal/models/running_aggregator.go index 0cd9bc4e28925..64c4d0589827c 100644 --- a/internal/models/running_aggregator.go +++ b/internal/models/running_aggregator.go @@ -33,33 +33,26 @@ func NewRunningAggregator( MetricsPushed: selfstat.Register( "aggregate", "metrics_pushed", - map[string]string{"aggregator": config.LogName()}, + map[string]string{"aggregator": config.Name, "alias": config.Alias}, ), MetricsFiltered: selfstat.Register( "aggregate", "metrics_filtered", - map[string]string{"aggregator": config.LogName()}, + map[string]string{"aggregator": config.Name, "alias": config.Alias}, ), MetricsDropped: selfstat.Register( "aggregate", "metrics_dropped", - map[string]string{"aggregator": config.LogName()}, + map[string]string{"aggregator": config.Name, "alias": config.Alias}, ), PushTime: selfstat.Register( "aggregate", "push_time_ns", - map[string]string{"aggregator": config.LogName()}, + map[string]string{"aggregator": config.Name, "alias": config.Alias}, ), } } -func (c *AggregatorConfig) LogName() string { - if c.Alias == "" { - return c.Name - } - return c.Name + "::" + c.Alias -} - // AggregatorConfig is the common config for all aggregators. type AggregatorConfig struct { Name string @@ -91,7 +84,7 @@ func (r *RunningAggregator) Init() error { setLogIfExist(r.Aggregator, &Logger{ Name: r.LogName(), Errs: selfstat.Register("aggregate", "errors", - map[string]string{"aggregator": r.LogName()}), + map[string]string{"aggregator": r.Config.Name, "alias": r.Config.Alias}), }) if p, ok := r.Aggregator.(telegraf.Initializer); ok { diff --git a/internal/models/running_input.go b/internal/models/running_input.go index af5f616e1c867..2582ba807b91d 100644 --- a/internal/models/running_input.go +++ b/internal/models/running_input.go @@ -26,12 +26,12 @@ func NewRunningInput(input telegraf.Input, config *InputConfig) *RunningInput { MetricsGathered: selfstat.Register( "gather", "metrics_gathered", - map[string]string{"input": config.LogName()}, + map[string]string{"input": config.Name, "alias": config.Alias}, ), GatherTime: selfstat.RegisterTiming( "gather", "gather_time_ns", - map[string]string{"input": config.LogName()}, + map[string]string{"input": config.Name, "alias": config.Alias}, ), } } @@ -49,13 +49,6 @@ type InputConfig struct { Filter Filter } -func (c *InputConfig) LogName() string { - if c.Alias == "" { - return c.Name - } - return c.Name + "::" + c.Alias -} - func (r *RunningInput) Name() string { return "inputs." + r.Config.Name } @@ -75,7 +68,7 @@ func (r *RunningInput) Init() error { setLogIfExist(r.Input, &Logger{ Name: r.LogName(), Errs: selfstat.Register("gather", "errors", - map[string]string{"input": r.LogName()}), + map[string]string{"input": r.Config.Name, "alias": r.Config.Alias}), }) if p, ok := r.Input.(telegraf.Initializer); ok { diff --git a/internal/models/running_output.go b/internal/models/running_output.go index 2e4e614c9c74a..309e631d4f5ff 100644 --- a/internal/models/running_output.go +++ b/internal/models/running_output.go @@ -81,12 +81,12 @@ func NewRunningOutput( MetricsFiltered: selfstat.Register( "write", "metrics_filtered", - map[string]string{"output": conf.LogName()}, + map[string]string{"output": conf.Name, "alias": conf.Alias}, ), WriteTime: selfstat.RegisterTiming( "write", "write_time_ns", - map[string]string{"output": conf.LogName()}, + map[string]string{"output": conf.Name, "alias": conf.Alias}, ), } @@ -116,14 +116,14 @@ func (ro *RunningOutput) metricFiltered(metric telegraf.Metric) { metric.Drop() } -func (ro *RunningOutput) Init() error { - setLogIfExist(ro.Output, &Logger{ - Name: ro.LogName(), +func (r *RunningOutput) Init() error { + setLogIfExist(r.Output, &Logger{ + Name: r.LogName(), Errs: selfstat.Register("write", "errors", - map[string]string{"output": ro.LogName()}), + map[string]string{"output": r.Config.Name, "alias": r.Config.Alias}), }) - if p, ok := ro.Output.(telegraf.Initializer); ok { + if p, ok := r.Output.(telegraf.Initializer); ok { err := p.Init() if err != nil { return err diff --git a/internal/models/running_processor.go b/internal/models/running_processor.go index 7d3874be5e880..9d30ef2807ae9 100644 --- a/internal/models/running_processor.go +++ b/internal/models/running_processor.go @@ -51,14 +51,14 @@ func containsMetric(item telegraf.Metric, metrics []telegraf.Metric) bool { return false } -func (rp *RunningProcessor) Init() error { - setLogIfExist(rp.Processor, &Logger{ - Name: rp.LogName(), +func (r *RunningProcessor) Init() error { + setLogIfExist(r.Processor, &Logger{ + Name: r.LogName(), Errs: selfstat.Register("process", "errors", - map[string]string{"processor": rp.LogName()}), + map[string]string{"processor": r.Config.Name, "alias": r.Config.Alias}), }) - if p, ok := rp.Processor.(telegraf.Initializer); ok { + if p, ok := r.Processor.(telegraf.Initializer); ok { err := p.Init() if err != nil { return err From 462ad92a8ddb759eb62ab0761baf7b88b9028408 Mon Sep 17 00:00:00 2001 From: greg linton Date: Wed, 21 Aug 2019 12:50:44 -0600 Subject: [PATCH 15/15] Add logger to Running*. Use LogName only when needed --- agent/accumulator.go | 1 - agent/agent.go | 2 +- internal/config/config.go | 5 +- internal/models/log.go | 32 +++++++++ internal/models/running_aggregator.go | 37 ++++------ internal/models/running_input.go | 29 ++++---- internal/models/running_output.go | 98 ++++++++++----------------- internal/models/running_processor.go | 26 +++---- 8 files changed, 109 insertions(+), 121 deletions(-) diff --git a/agent/accumulator.go b/agent/accumulator.go index 53167d6c6b950..21146e3e22cb0 100644 --- a/agent/accumulator.go +++ b/agent/accumulator.go @@ -14,7 +14,6 @@ var ( ) type MetricMaker interface { - Name() string LogName() string MakeMetric(metric telegraf.Metric) telegraf.Metric } diff --git a/agent/agent.go b/agent/agent.go index a0a772613500d..b7e70024c5cd3 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -209,7 +209,7 @@ func (a *Agent) Test(ctx context.Context, waitDuration time.Duration) error { // Special instructions for some inputs. cpu, for example, needs to be // run twice in order to return cpu usage percentages. - switch input.Name() { + switch input.Config.Name { case "inputs.cpu", "inputs.mongodb", "inputs.procstat": nulAcc := NewAccumulator(input, nulC) nulAcc.SetPrecision(a.Precision()) diff --git a/internal/config/config.go b/internal/config/config.go index 94f38b146ec1c..f2617e8b3751a 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -920,10 +920,7 @@ func (c *Config) addProcessor(name string, table *ast.Table) error { return err } - rf := &models.RunningProcessor{ - Processor: processor, - Config: processorConfig, - } + rf := models.NewRunningProcessor(processor, processorConfig) c.Processors = append(c.Processors, rf) return nil diff --git a/internal/models/log.go b/internal/models/log.go index 6d0e0f11e65e4..a99eb32123dcc 100644 --- a/internal/models/log.go +++ b/internal/models/log.go @@ -2,7 +2,9 @@ package models import ( "log" + "reflect" + "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/selfstat" ) @@ -53,3 +55,33 @@ func (l *Logger) Infof(format string, args ...interface{}) { func (l *Logger) Info(args ...interface{}) { log.Print(append([]interface{}{"I! [" + l.Name + "] "}, args...)...) } + +// logName returns the log-friendly name/type. +func logName(pluginType, name, alias string) string { + if alias == "" { + return pluginType + "." + name + } + return pluginType + "." + name + "::" + alias +} + +func setLogIfExist(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 +} diff --git a/internal/models/running_aggregator.go b/internal/models/running_aggregator.go index 64c4d0589827c..ee46e5b13f557 100644 --- a/internal/models/running_aggregator.go +++ b/internal/models/running_aggregator.go @@ -1,7 +1,6 @@ package models import ( - "log" "sync" "time" @@ -16,6 +15,7 @@ type RunningAggregator struct { Config *AggregatorConfig periodStart time.Time periodEnd time.Time + log telegraf.Logger MetricsPushed selfstat.Stat MetricsFiltered selfstat.Stat @@ -23,10 +23,15 @@ type RunningAggregator struct { PushTime selfstat.Stat } -func NewRunningAggregator( - aggregator telegraf.Aggregator, - config *AggregatorConfig, -) *RunningAggregator { +func NewRunningAggregator(aggregator telegraf.Aggregator, config *AggregatorConfig) *RunningAggregator { + logger := &Logger{ + Name: logName("aggregators", config.Name, config.Alias), + Errs: selfstat.Register("aggregate", "errors", + map[string]string{"input": config.Name, "alias": config.Alias}), + } + + setLogIfExist(aggregator, logger) + return &RunningAggregator{ Aggregator: aggregator, Config: config, @@ -50,6 +55,7 @@ func NewRunningAggregator( "push_time_ns", map[string]string{"aggregator": config.Name, "alias": config.Alias}, ), + log: logger, } } @@ -69,24 +75,11 @@ type AggregatorConfig struct { Filter Filter } -func (r *RunningAggregator) Name() string { - return "aggregators." + r.Config.Name -} - func (r *RunningAggregator) LogName() string { - if r.Config.Alias == "" { - return r.Name() - } - return r.Name() + "::" + r.Config.Alias + return logName("aggregators", r.Config.Name, r.Config.Alias) } func (r *RunningAggregator) Init() error { - setLogIfExist(r.Aggregator, &Logger{ - Name: r.LogName(), - Errs: selfstat.Register("aggregate", "errors", - map[string]string{"aggregator": r.Config.Name, "alias": r.Config.Alias}), - }) - if p, ok := r.Aggregator.(telegraf.Initializer); ok { err := p.Init() if err != nil { @@ -107,7 +100,7 @@ func (r *RunningAggregator) EndPeriod() time.Time { func (r *RunningAggregator) UpdateWindow(start, until time.Time) { r.periodStart = start r.periodEnd = until - log.Printf("D! [%s] Updated aggregation range [%s, %s]", r.LogName(), start, until) + r.log.Debugf("Updated aggregation range [%s, %s]", start, until) } func (r *RunningAggregator) MakeMetric(metric telegraf.Metric) telegraf.Metric { @@ -151,8 +144,8 @@ func (r *RunningAggregator) Add(m telegraf.Metric) bool { defer r.Unlock() if m.Time().Before(r.periodStart.Add(-r.Config.Grace)) || m.Time().After(r.periodEnd.Add(r.Config.Delay)) { - log.Printf("D! [%s] metric is outside aggregation window; discarding. %s: m: %s e: %s g: %s", - r.LogName(), m.Time(), r.periodStart, r.periodEnd, r.Config.Grace) + r.log.Debugf("metric is outside aggregation window; discarding. %s: m: %s e: %s g: %s", + m.Time(), r.periodStart, r.periodEnd, r.Config.Grace) r.MetricsDropped.Incr(1) return r.Config.DropOriginal } diff --git a/internal/models/running_input.go b/internal/models/running_input.go index 2582ba807b91d..85f0afb81869c 100644 --- a/internal/models/running_input.go +++ b/internal/models/running_input.go @@ -13,6 +13,7 @@ type RunningInput struct { Input telegraf.Input Config *InputConfig + log telegraf.Logger defaultTags map[string]string MetricsGathered selfstat.Stat @@ -20,6 +21,14 @@ type RunningInput struct { } func NewRunningInput(input telegraf.Input, config *InputConfig) *RunningInput { + logger := &Logger{ + Name: logName("inputs", config.Name, config.Alias), + Errs: selfstat.Register("gather", "errors", + map[string]string{"input": config.Name, "alias": config.Alias}), + } + + setLogIfExist(input, logger) + return &RunningInput{ Input: input, Config: config, @@ -33,6 +42,7 @@ func NewRunningInput(input telegraf.Input, config *InputConfig) *RunningInput { "gather_time_ns", map[string]string{"input": config.Name, "alias": config.Alias}, ), + log: logger, } } @@ -49,28 +59,15 @@ type InputConfig struct { Filter Filter } -func (r *RunningInput) Name() string { - return "inputs." + r.Config.Name +func (r *RunningInput) metricFiltered(metric telegraf.Metric) { + metric.Drop() } func (r *RunningInput) LogName() string { - if r.Config.Alias == "" { - return r.Name() - } - return r.Name() + "::" + r.Config.Alias -} - -func (r *RunningInput) metricFiltered(metric telegraf.Metric) { - metric.Drop() + return logName("inputs", r.Config.Name, r.Config.Alias) } func (r *RunningInput) Init() error { - setLogIfExist(r.Input, &Logger{ - Name: r.LogName(), - Errs: selfstat.Register("gather", "errors", - map[string]string{"input": r.Config.Name, "alias": r.Config.Alias}), - }) - if p, ok := r.Input.(telegraf.Initializer); ok { err := p.Init() if err != nil { diff --git a/internal/models/running_output.go b/internal/models/running_output.go index 309e631d4f5ff..86e68f0571d36 100644 --- a/internal/models/running_output.go +++ b/internal/models/running_output.go @@ -1,8 +1,6 @@ package models import ( - "log" - "reflect" "sync" "sync/atomic" "time" @@ -47,6 +45,7 @@ type RunningOutput struct { BatchReady chan time.Time buffer *Buffer + log telegraf.Logger aggMutex sync.Mutex } @@ -54,40 +53,49 @@ type RunningOutput struct { func NewRunningOutput( name string, output telegraf.Output, - conf *OutputConfig, + config *OutputConfig, batchSize int, bufferLimit int, ) *RunningOutput { - if conf.MetricBufferLimit > 0 { - bufferLimit = conf.MetricBufferLimit + logger := &Logger{ + Name: logName("outputs", config.Name, config.Alias), + Errs: selfstat.Register("gather", "errors", + map[string]string{"output": config.Name, "alias": config.Alias}), + } + + setLogIfExist(output, logger) + + if config.MetricBufferLimit > 0 { + bufferLimit = config.MetricBufferLimit } if bufferLimit == 0 { bufferLimit = DEFAULT_METRIC_BUFFER_LIMIT } - if conf.MetricBatchSize > 0 { - batchSize = conf.MetricBatchSize + if config.MetricBatchSize > 0 { + batchSize = config.MetricBatchSize } if batchSize == 0 { batchSize = DEFAULT_METRIC_BATCH_SIZE } ro := &RunningOutput{ - buffer: NewBuffer(conf.LogName(), bufferLimit), + buffer: NewBuffer(config.LogName(), bufferLimit), BatchReady: make(chan time.Time, 1), Output: output, - Config: conf, + Config: config, MetricBufferLimit: bufferLimit, MetricBatchSize: batchSize, MetricsFiltered: selfstat.Register( "write", "metrics_filtered", - map[string]string{"output": conf.Name, "alias": conf.Alias}, + map[string]string{"output": config.Name, "alias": config.Alias}, ), WriteTime: selfstat.RegisterTiming( "write", "write_time_ns", - map[string]string{"output": conf.Name, "alias": conf.Alias}, + map[string]string{"output": config.Name, "alias": config.Alias}, ), + log: logger, } return ro @@ -100,15 +108,8 @@ func (c *OutputConfig) LogName() string { return c.Name + "::" + c.Alias } -func (ro *RunningOutput) Name() string { - return "outputs." + ro.Config.Name -} - -func (ro *RunningOutput) LogName() string { - if ro.Config.Alias == "" { - return ro.Name() - } - return ro.Name() + "::" + ro.Config.Alias +func (r *RunningOutput) LogName() string { + return logName("outputs", r.Config.Name, r.Config.Alias) } func (ro *RunningOutput) metricFiltered(metric telegraf.Metric) { @@ -117,12 +118,6 @@ func (ro *RunningOutput) metricFiltered(metric telegraf.Metric) { } func (r *RunningOutput) Init() error { - setLogIfExist(r.Output, &Logger{ - Name: r.LogName(), - Errs: selfstat.Register("write", "errors", - map[string]string{"output": r.Config.Name, "alias": r.Config.Alias}), - }) - if p, ok := r.Output.(telegraf.Initializer); ok { err := p.Init() if err != nil { @@ -133,28 +128,6 @@ func (r *RunningOutput) Init() error { return nil } -func setLogIfExist(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 -} - // AddMetric adds a metric to the output. // // Takes ownership of metric @@ -240,35 +213,32 @@ func (ro *RunningOutput) WriteBatch() error { return nil } -func (ro *RunningOutput) Close() { - err := ro.Output.Close() +func (r *RunningOutput) Close() { + err := r.Output.Close() if err != nil { - log.Printf("E! [%s] Error closing output: %v", ro.LogName(), err) + r.log.Errorf("Error closing output: %v", err) } } -func (ro *RunningOutput) write(metrics []telegraf.Metric) error { - dropped := atomic.LoadInt64(&ro.droppedMetrics) +func (r *RunningOutput) write(metrics []telegraf.Metric) error { + dropped := atomic.LoadInt64(&r.droppedMetrics) if dropped > 0 { - log.Printf("W! [%s] Metric buffer overflow; %d metrics have been dropped", - ro.LogName(), dropped) - atomic.StoreInt64(&ro.droppedMetrics, 0) + r.log.Warnf("Metric buffer overflow; %d metrics have been dropped", dropped) + atomic.StoreInt64(&r.droppedMetrics, 0) } start := time.Now() - err := ro.Output.Write(metrics) + err := r.Output.Write(metrics) elapsed := time.Since(start) - ro.WriteTime.Incr(elapsed.Nanoseconds()) + r.WriteTime.Incr(elapsed.Nanoseconds()) if err == nil { - log.Printf("D! [%s] wrote batch of %d metrics in %s", - ro.LogName(), len(metrics), elapsed) + r.log.Debugf("Wrote batch of %d metrics in %s", len(metrics), elapsed) } return err } -func (ro *RunningOutput) LogBufferStatus() { - nBuffer := ro.buffer.Len() - log.Printf("D! [%s] buffer fullness: %d / %d metrics", - ro.LogName(), nBuffer, ro.MetricBufferLimit) +func (r *RunningOutput) LogBufferStatus() { + nBuffer := r.buffer.Len() + r.log.Debugf("Buffer fullness: %d / %d metrics", nBuffer, r.MetricBufferLimit) } diff --git a/internal/models/running_processor.go b/internal/models/running_processor.go index 9d30ef2807ae9..5a12716e5ebeb 100644 --- a/internal/models/running_processor.go +++ b/internal/models/running_processor.go @@ -9,6 +9,7 @@ import ( type RunningProcessor struct { sync.Mutex + log telegraf.Logger Processor telegraf.Processor Config *ProcessorConfig } @@ -27,15 +28,20 @@ type ProcessorConfig struct { Filter Filter } -func (rp *RunningProcessor) Name() string { - return "processors." + rp.Config.Name -} +func NewRunningProcessor(processor telegraf.Processor, config *ProcessorConfig) *RunningProcessor { + logger := &Logger{ + Name: logName("processors", config.Name, config.Alias), + Errs: selfstat.Register("process", "errors", + map[string]string{"input": config.Name, "alias": config.Alias}), + } -func (rp *RunningProcessor) LogName() string { - if rp.Config.Alias == "" { - return rp.Name() + setLogIfExist(processor, logger) + + return &RunningProcessor{ + Processor: processor, + Config: config, + log: logger, } - return rp.Name() + "::" + rp.Config.Alias } func (rp *RunningProcessor) metricFiltered(metric telegraf.Metric) { @@ -52,12 +58,6 @@ func containsMetric(item telegraf.Metric, metrics []telegraf.Metric) bool { } func (r *RunningProcessor) Init() error { - setLogIfExist(r.Processor, &Logger{ - Name: r.LogName(), - Errs: selfstat.Register("process", "errors", - map[string]string{"processor": r.Config.Name, "alias": r.Config.Alias}), - }) - if p, ok := r.Processor.(telegraf.Initializer); ok { err := p.Init() if err != nil {