Flush metrics only after all service inputs have stopped

This commit is contained in:
Daniel Blanco 2016-05-20 13:27:04 +01:00 committed by Daniel Blanco
parent 741331308a
commit 6d2b2a0b29
1 changed files with 7 additions and 3 deletions

View File

@ -315,6 +315,12 @@ func jitterInterval(ininterval, injitter time.Duration) time.Duration {
func (a *Agent) Run(shutdown chan struct{}) error { func (a *Agent) Run(shutdown chan struct{}) error {
var wg sync.WaitGroup var wg sync.WaitGroup
flusherShutdown := make(chan struct{})
defer func() {
close(flusherShutdown)
wg.Wait()
}()
a.Config.Agent.FlushInterval.Duration = jitterInterval( a.Config.Agent.FlushInterval.Duration = jitterInterval(
a.Config.Agent.FlushInterval.Duration, a.Config.Agent.FlushInterval.Duration,
a.Config.Agent.FlushJitter.Duration) a.Config.Agent.FlushJitter.Duration)
@ -353,7 +359,7 @@ func (a *Agent) Run(shutdown chan struct{}) error {
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
if err := a.flusher(shutdown, metricC); err != nil { if err := a.flusher(flusherShutdown, metricC); err != nil {
log.Printf("Flusher routine failed, exiting: %s\n", err.Error()) log.Printf("Flusher routine failed, exiting: %s\n", err.Error())
close(shutdown) close(shutdown)
} }
@ -373,8 +379,6 @@ func (a *Agent) Run(shutdown chan struct{}) error {
} }
} }
defer wg.Wait()
for { for {
if err := a.gatherParallel(metricC); err != nil { if err := a.gatherParallel(metricC); err != nil {
log.Printf(err.Error()) log.Printf(err.Error())