Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
Only print final collection when runing --test (influxdata#4991)
Browse files Browse the repository at this point in the history
glinton authored and danielnelson committed Nov 15, 2018
1 parent 274af39 commit 91ecec7
Showing 2 changed files with 45 additions and 23 deletions.
63 changes: 42 additions & 21 deletions agent/agent.go
Original file line number Diff line number Diff line change
@@ -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.
func (a *Agent) Test() error {
func (a *Agent) Test(ctx context.Context) error {
var wg sync.WaitGroup
metricC := make(chan telegraf.Metric)
nulC := make(chan telegraf.Metric)
defer func() {
close(metricC)
close(nulC)
wg.Wait()
}()

@@ -156,36 +158,55 @@ func (a *Agent) Test() error {
octets, err := s.Serialize(metric)
if err == nil {
fmt.Print("> ", string(octets))

}
}
}()

for _, input := range a.Config.Inputs {
if _, ok := input.Input.(telegraf.ServiceInput); ok {
log.Printf("W!: [agent] skipping plugin [[%s]]: service inputs not supported in --test mode",
input.Name())
continue
wg.Add(1)
go func() {
defer wg.Done()
for range nulC {
}
}()

acc := NewAccumulator(input, metricC)
acc.SetPrecision(a.Config.Agent.Precision.Duration,
a.Config.Agent.Interval.Duration)
input.SetDefaultTags(a.Config.Tags)
for _, input := range a.Config.Inputs {
select {
case <-ctx.Done():
return nil
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
}

if err := input.Input.Gather(acc); err != nil {
return err
}
acc := NewAccumulator(input, metricC)
acc.SetPrecision(a.Config.Agent.Precision.Duration,
a.Config.Agent.Interval.Duration)
input.SetDefaultTags(a.Config.Tags)

// Special instructions for some inputs. cpu, for example, needs to be
// 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
// run twice in order to return cpu usage percentages.
switch input.Name() {
case "inputs.cpu", "inputs.mongodb", "inputs.procstat":
time.Sleep(500 * time.Millisecond)
if err := input.Input.Gather(acc); err != nil {
return err
time.Sleep(500 * time.Millisecond)
if err := input.Input.Gather(acc); err != nil {
return err
}
default:
if err := input.Input.Gather(acc); err != nil {
return err
}
}
}

}

return nil
5 changes: 3 additions & 2 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
@@ -83,7 +83,8 @@ func reloadLoop(
ctx, cancel := context.WithCancel(context.Background())

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() {
select {
case sig := <-signals:
@@ -154,7 +155,7 @@ func runAgent(ctx context.Context,
)

if *fTest {
return ag.Test()
return ag.Test(ctx)
}

log.Printf("I! Starting Telegraf %s\n", version)

0 comments on commit 91ecec7

Please sign in to comment.