Run ServiceInputs during test mode; add --test-wait option (#5911)
This commit is contained in:
parent
fec1b3ec19
commit
de096428be
|
@ -136,7 +136,7 @@ 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(ctx context.Context) error {
|
||||
func (a *Agent) Test(ctx context.Context, waitDuration time.Duration) error {
|
||||
var wg sync.WaitGroup
|
||||
metricC := make(chan telegraf.Metric)
|
||||
nulC := make(chan telegraf.Metric)
|
||||
|
@ -156,8 +156,8 @@ func (a *Agent) Test(ctx context.Context) error {
|
|||
octets, err := s.Serialize(metric)
|
||||
if err == nil {
|
||||
fmt.Print("> ", string(octets))
|
||||
|
||||
}
|
||||
metric.Reject()
|
||||
}
|
||||
}()
|
||||
|
||||
|
@ -168,20 +168,32 @@ func (a *Agent) Test(ctx context.Context) error {
|
|||
}
|
||||
}()
|
||||
|
||||
hasServiceInputs := false
|
||||
for _, input := range a.Config.Inputs {
|
||||
if _, ok := input.Input.(telegraf.ServiceInput); ok {
|
||||
hasServiceInputs = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if hasServiceInputs {
|
||||
log.Printf("D! [agent] Starting service inputs")
|
||||
err := a.startServiceInputs(ctx, metricC)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
break
|
||||
}
|
||||
|
||||
acc := NewAccumulator(input, metricC)
|
||||
acc.SetPrecision(a.Precision())
|
||||
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.
|
||||
|
@ -190,19 +202,25 @@ func (a *Agent) Test(ctx context.Context) error {
|
|||
nulAcc := NewAccumulator(input, nulC)
|
||||
nulAcc.SetPrecision(a.Precision())
|
||||
if err := input.Input.Gather(nulAcc); err != nil {
|
||||
return err
|
||||
acc.AddError(err)
|
||||
}
|
||||
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
if err := input.Input.Gather(acc); err != nil {
|
||||
return err
|
||||
acc.AddError(err)
|
||||
}
|
||||
default:
|
||||
if err := input.Input.Gather(acc); err != nil {
|
||||
return err
|
||||
acc.AddError(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if hasServiceInputs {
|
||||
log.Printf("D! [agent] Waiting for service inputs")
|
||||
internal.SleepContext(ctx, waitDuration)
|
||||
log.Printf("D! [agent] Stopping service inputs")
|
||||
a.stopServiceInputs()
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -13,6 +13,7 @@ import (
|
|||
"runtime"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/telegraf/agent"
|
||||
"github.com/influxdata/telegraf/internal"
|
||||
|
@ -33,7 +34,8 @@ var pprofAddr = flag.String("pprof-addr", "",
|
|||
"pprof address to listen on, not activate pprof if empty")
|
||||
var fQuiet = flag.Bool("quiet", false,
|
||||
"run in quiet mode")
|
||||
var fTest = flag.Bool("test", false, "gather metrics, print them out, and exit")
|
||||
var fTest = flag.Bool("test", false, "enable test mode: gather metrics, print them out, and exit")
|
||||
var fTestWait = flag.Int("test-wait", 0, "wait up to this many seconds for service inputs to complete in test mode")
|
||||
var fConfig = flag.String("config", "", "configuration file to load")
|
||||
var fConfigDirectory = flag.String("config-directory", "",
|
||||
"directory containing additional *.conf files")
|
||||
|
@ -167,8 +169,9 @@ func runAgent(ctx context.Context,
|
|||
|
||||
logger.SetupLogging(logConfig)
|
||||
|
||||
if *fTest {
|
||||
return ag.Test(ctx)
|
||||
if *fTest || *fTestWait != 0 {
|
||||
testWaitDuration := time.Duration(*fTestWait) * time.Second
|
||||
return ag.Test(ctx, testWaitDuration)
|
||||
}
|
||||
|
||||
log.Printf("I! Loaded inputs: %s", strings.Join(c.InputNames(), " "))
|
||||
|
|
|
@ -31,6 +31,8 @@ The commands & flags are:
|
|||
--sample-config print out full sample configuration
|
||||
--test gather metrics, print them out, and exit;
|
||||
processors, aggregators, and outputs are not run
|
||||
--test-wait wait up to this many seconds for service
|
||||
inputs to complete in test mode
|
||||
--usage <plugin> print usage for a plugin, ie, 'telegraf --usage mysql'
|
||||
--version display the version and exit
|
||||
|
||||
|
|
|
@ -31,6 +31,8 @@ The commands & flags are:
|
|||
'processors', 'aggregators' and 'inputs'
|
||||
--test gather metrics, print them out, and exit;
|
||||
processors, aggregators, and outputs are not run
|
||||
--test-wait wait up to this many seconds for service
|
||||
inputs to complete in test mode
|
||||
--usage <plugin> print usage for a plugin, ie, 'telegraf --usage mysql'
|
||||
--version display the version and exit
|
||||
|
||||
|
|
Loading…
Reference in New Issue