mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-01-20 23:39:48 +01:00
lib/promscrape: make consistent scrape time offsets across reloads for the same ScrapeURL and Labels
This should make consistent intervals between data points for scrape targets across reloads. Previously these intervals were random.
This commit is contained in:
parent
f25416984b
commit
ab1e6a76bb
@ -3,7 +3,6 @@ package promscrape
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -14,6 +13,7 @@ import (
|
|||||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promrelabel"
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promrelabel"
|
||||||
parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/prometheus"
|
parser "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/prometheus"
|
||||||
"github.com/VictoriaMetrics/metrics"
|
"github.com/VictoriaMetrics/metrics"
|
||||||
|
xxhash "github.com/cespare/xxhash/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -121,11 +121,22 @@ type scrapeWork struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (sw *scrapeWork) run(stopCh <-chan struct{}) {
|
func (sw *scrapeWork) run(stopCh <-chan struct{}) {
|
||||||
// Randomize start time for the first scrape in order to spread load
|
// Calculate start time for the first scrape from ScrapeURL and labels.
|
||||||
// when scraping many targets.
|
// This should spread load when scraping many targets with different
|
||||||
|
// scrape urls and labels.
|
||||||
|
// This also makes consistent scrape times across restarts
|
||||||
|
// for a target with the same ScrapeURL and labels.
|
||||||
scrapeInterval := sw.Config.ScrapeInterval
|
scrapeInterval := sw.Config.ScrapeInterval
|
||||||
randSleep := time.Duration(float64(scrapeInterval) * rand.Float64())
|
key := fmt.Sprintf("ScrapeURL=%s, Labels=%s", sw.Config.ScrapeURL, sw.Config.LabelsString())
|
||||||
timer := time.NewTimer(randSleep)
|
h := uint32(xxhash.Sum64([]byte(key)))
|
||||||
|
randSleep := uint64(float64(scrapeInterval) * (float64(h) / (1 << 32)))
|
||||||
|
sleepOffset := uint64(time.Now().UnixNano()) % uint64(scrapeInterval)
|
||||||
|
logger.Infof("randsleep=%d, sleepOffset=%d", randSleep, sleepOffset)
|
||||||
|
if randSleep < sleepOffset {
|
||||||
|
randSleep += uint64(scrapeInterval)
|
||||||
|
}
|
||||||
|
randSleep -= sleepOffset
|
||||||
|
timer := time.NewTimer(time.Duration(randSleep))
|
||||||
var timestamp int64
|
var timestamp int64
|
||||||
var ticker *time.Ticker
|
var ticker *time.Ticker
|
||||||
select {
|
select {
|
||||||
|
Loading…
Reference in New Issue
Block a user