From 5678a869245e54b44201e99cebb54042e7c74eac Mon Sep 17 00:00:00 2001 From: beorn7 Date: Fri, 6 Feb 2015 16:44:56 +0100 Subject: [PATCH] Throttle scraping if a scrape took longer than the configured interval. The simple algorithm applied here will increase the actual interval incrementally, whenever and as long as the scrape itself takes longer than the configured interval. Once it takes shorter again, the actual interval will iteratively decrease again. --- retrieval/target.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/retrieval/target.go b/retrieval/target.go index 218338683d..3e2a22f672 100644 --- a/retrieval/target.go +++ b/retrieval/target.go @@ -265,10 +265,18 @@ func (t *target) RunScraper(ingester extraction.Ingester, interval time.Duration case <-t.scraperStopping: return case <-ticker.C: - targetIntervalLength.WithLabelValues(interval.String()).Observe(float64(time.Since(t.lastScrape) / time.Second)) + took := time.Since(t.lastScrape) t.Lock() // Write t.lastScrape requires locking. t.lastScrape = time.Now() t.Unlock() + targetIntervalLength.WithLabelValues(interval.String()).Observe( + float64(took) / float64(time.Second), // Sub-second precision. + ) + // Throttle the scrape if it took longer than interval - by + // sleeping for the time it took longer. This will make the + // actual scrape interval increase as long as a scrape takes + // longer than the interval we are aiming for. + time.Sleep(took - interval) t.scrape(ingester) } }