From 6d2b2a0b29e5e017030a528278b9debfdf6c98be Mon Sep 17 00:00:00 2001 From: Daniel Blanco Date: Fri, 20 May 2016 13:27:04 +0100 Subject: [PATCH] Flush metrics only after all service inputs have stopped --- agent/agent.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 60f2d63c6..904a6698d 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -315,6 +315,12 @@ func jitterInterval(ininterval, injitter time.Duration) time.Duration { func (a *Agent) Run(shutdown chan struct{}) error { 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, a.Config.Agent.FlushJitter.Duration) @@ -353,7 +359,7 @@ func (a *Agent) Run(shutdown chan struct{}) error { wg.Add(1) go func() { 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()) close(shutdown) } @@ -373,8 +379,6 @@ func (a *Agent) Run(shutdown chan struct{}) error { } } - defer wg.Wait() - for { if err := a.gatherParallel(metricC); err != nil { log.Printf(err.Error())