Only print final collection when runing --test (#4991)

This commit is contained in:
Greg 2018-11-15 16:44:36 -07:00 committed by Daniel Nelson
parent 274af39a5e
commit 91ecec71ea
2 changed files with 45 additions and 23 deletions

View File

@ -138,11 +138,13 @@ func (a *Agent) Run(ctx context.Context) error {
} }
// Test runs the inputs once and prints the output to stdout in line protocol. // Test runs the inputs once and prints the output to stdout in line protocol.
func (a *Agent) Test() error { func (a *Agent) Test(ctx context.Context) error {
var wg sync.WaitGroup var wg sync.WaitGroup
metricC := make(chan telegraf.Metric) metricC := make(chan telegraf.Metric)
nulC := make(chan telegraf.Metric)
defer func() { defer func() {
close(metricC) close(metricC)
close(nulC)
wg.Wait() wg.Wait()
}() }()
@ -156,36 +158,55 @@ func (a *Agent) Test() error {
octets, err := s.Serialize(metric) octets, err := s.Serialize(metric)
if err == nil { if err == nil {
fmt.Print("> ", string(octets)) fmt.Print("> ", string(octets))
} }
} }
}() }()
wg.Add(1)
go func() {
defer wg.Done()
for range nulC {
}
}()
for _, input := range a.Config.Inputs { for _, input := range a.Config.Inputs {
if _, ok := input.Input.(telegraf.ServiceInput); ok { select {
log.Printf("W!: [agent] skipping plugin [[%s]]: service inputs not supported in --test mode", case <-ctx.Done():
input.Name()) return nil
continue default:
} if _, ok := input.Input.(telegraf.ServiceInput); ok {
log.Printf("W!: [agent] skipping plugin [[%s]]: service inputs not supported in --test mode",
input.Name())
continue
}
acc := NewAccumulator(input, metricC) acc := NewAccumulator(input, metricC)
acc.SetPrecision(a.Config.Agent.Precision.Duration, acc.SetPrecision(a.Config.Agent.Precision.Duration,
a.Config.Agent.Interval.Duration) a.Config.Agent.Interval.Duration)
input.SetDefaultTags(a.Config.Tags) input.SetDefaultTags(a.Config.Tags)
if err := input.Input.Gather(acc); err != nil { // Special instructions for some inputs. cpu, for example, needs to be
return err // run twice in order to return cpu usage percentages.
} switch input.Name() {
case "inputs.cpu", "inputs.mongodb", "inputs.procstat":
nulAcc := NewAccumulator(input, nulC)
nulAcc.SetPrecision(a.Config.Agent.Precision.Duration,
a.Config.Agent.Interval.Duration)
if err := input.Input.Gather(nulAcc); err != nil {
return err
}
// Special instructions for some inputs. cpu, for example, needs to be time.Sleep(500 * time.Millisecond)
// run twice in order to return cpu usage percentages. if err := input.Input.Gather(acc); err != nil {
switch input.Name() { return err
case "inputs.cpu", "inputs.mongodb", "inputs.procstat": }
time.Sleep(500 * time.Millisecond) default:
if err := input.Input.Gather(acc); err != nil { if err := input.Input.Gather(acc); err != nil {
return err return err
}
} }
} }
} }
return nil return nil

View File

@ -83,7 +83,8 @@ func reloadLoop(
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
signals := make(chan os.Signal) signals := make(chan os.Signal)
signal.Notify(signals, os.Interrupt, syscall.SIGHUP, syscall.SIGTERM) signal.Notify(signals, os.Interrupt, syscall.SIGHUP,
syscall.SIGTERM, syscall.SIGINT)
go func() { go func() {
select { select {
case sig := <-signals: case sig := <-signals:
@ -154,7 +155,7 @@ func runAgent(ctx context.Context,
) )
if *fTest { if *fTest {
return ag.Test() return ag.Test(ctx)
} }
log.Printf("I! Starting Telegraf %s\n", version) log.Printf("I! Starting Telegraf %s\n", version)