Add ability to label inputs for logging (#6207)

This commit is contained in:
Greg
2019-08-21 17:49:07 -06:00
committed by Daniel Nelson
parent bc52592c87
commit 5c8d0e3ac9
22 changed files with 475 additions and 103 deletions

View File

@@ -14,7 +14,7 @@ var (
)
type MetricMaker interface {
Name() string
LogName() string
MakeMetric(metric telegraf.Metric) telegraf.Metric
}
@@ -111,7 +111,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) {

View File

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

View File

@@ -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())
@@ -337,8 +337,8 @@ func (a *Agent) gatherOnce(
case err := <-done:
return err
case <-ticker.C:
log.Printf("W! [agent] input %q did not complete within its interval",
input.Name())
log.Printf("W! [agent] [%s] did not complete within its interval",
input.LogName())
}
}
}
@@ -551,7 +551,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)
}
}
@@ -603,8 +603,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()
}
}
@@ -617,7 +617,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 {
@@ -647,11 +647,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 {
@@ -663,7 +663,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
}
@@ -693,8 +693,8 @@ 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)
log.Printf("E! [agent] Service for [%s] failed to start: %v",
input.LogName(), err)
for _, si := range started {
si.Stop()
@@ -745,8 +745,8 @@ 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",
input.Name(), err, trace)
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: " +
"https://github.com/influxdata/telegraf/issues/new/choose")