scraping: reset symbol table periodically

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2023-11-24 19:46:26 +00:00
parent 4e748b9cd8
commit 5f50d974c9
3 changed files with 45 additions and 22 deletions

View file

@ -523,11 +523,12 @@ scrape_configs:
loops: map[uint64]loop{ loops: map[uint64]loop{
1: noopLoop(), 1: noopLoop(),
}, },
newLoop: newLoop, newLoop: newLoop,
logger: nil, logger: nil,
config: cfg1.ScrapeConfigs[0], config: cfg1.ScrapeConfigs[0],
client: http.DefaultClient, client: http.DefaultClient,
metrics: scrapeManager.metrics, metrics: scrapeManager.metrics,
symbolTable: labels.NewSymbolTable(),
} }
scrapeManager.scrapePools = map[string]*scrapePool{ scrapeManager.scrapePools = map[string]*scrapePool{
"job1": sp, "job1": sp,

View file

@ -73,7 +73,9 @@ type scrapePool struct {
client *http.Client client *http.Client
loops map[uint64]loop loops map[uint64]loop
symbolTable *labels.SymbolTable symbolTable *labels.SymbolTable
lastSymbolTableCheck time.Time
initialSymbolTableLen int
targetMtx sync.Mutex targetMtx sync.Mutex
// activeTargets and loops must always be synchronized to have the same // activeTargets and loops must always be synchronized to have the same
@ -132,17 +134,18 @@ func newScrapePool(cfg *config.ScrapeConfig, app storage.Appendable, offsetSeed
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
sp := &scrapePool{ sp := &scrapePool{
cancel: cancel, cancel: cancel,
appendable: app, appendable: app,
config: cfg, config: cfg,
client: client, client: client,
activeTargets: map[uint64]*Target{}, activeTargets: map[uint64]*Target{},
loops: map[uint64]loop{}, loops: map[uint64]loop{},
symbolTable: labels.NewSymbolTable(), // TODO: clean this out from time to time. symbolTable: labels.NewSymbolTable(),
logger: logger, lastSymbolTableCheck: time.Now(),
metrics: metrics, logger: logger,
httpOpts: options.HTTPClientOptions, metrics: metrics,
noDefaultPort: options.NoDefaultPort, httpOpts: options.HTTPClientOptions,
noDefaultPort: options.NoDefaultPort,
} }
sp.newLoop = func(opts scrapeLoopOptions) loop { sp.newLoop = func(opts scrapeLoopOptions) loop {
// Update the targets retrieval function for metadata to a new scrape cache. // Update the targets retrieval function for metadata to a new scrape cache.
@ -352,6 +355,21 @@ func (sp *scrapePool) reload(cfg *config.ScrapeConfig) error {
sp.metrics.targetReloadIntervalLength.WithLabelValues(interval.String()).Observe( sp.metrics.targetReloadIntervalLength.WithLabelValues(interval.String()).Observe(
time.Since(start).Seconds(), time.Since(start).Seconds(),
) )
// Here we take steps to clear out the symbol table if it has grown a lot.
// After waiting some time for things to settle, we take the size of the symbol-table.
// If, after some more time, the table has grown to twice that size, we start a new one.
const minTimeToCleanSymbolTable = 5 * time.Minute
if time.Since(sp.lastSymbolTableCheck) > minTimeToCleanSymbolTable {
if sp.initialSymbolTableLen == 0 {
sp.initialSymbolTableLen = sp.symbolTable.Len()
} else if sp.symbolTable.Len() > 2*sp.initialSymbolTableLen {
sp.symbolTable = labels.NewSymbolTable()
sp.initialSymbolTableLen = 0
}
sp.lastSymbolTableCheck = time.Now()
}
return nil return nil
} }

View file

@ -279,6 +279,7 @@ func TestScrapePoolReload(t *testing.T) {
logger: nil, logger: nil,
client: http.DefaultClient, client: http.DefaultClient,
metrics: newTestScrapeMetrics(t), metrics: newTestScrapeMetrics(t),
symbolTable: labels.NewSymbolTable(),
} }
// Reloading a scrape pool with a new scrape configuration must stop all scrape // Reloading a scrape pool with a new scrape configuration must stop all scrape
@ -357,10 +358,11 @@ func TestScrapePoolReloadPreserveRelabeledIntervalTimeout(t *testing.T) {
loops: map[uint64]loop{ loops: map[uint64]loop{
1: noopLoop(), 1: noopLoop(),
}, },
newLoop: newLoop, newLoop: newLoop,
logger: nil, logger: nil,
client: http.DefaultClient, client: http.DefaultClient,
metrics: newTestScrapeMetrics(t), metrics: newTestScrapeMetrics(t),
symbolTable: labels.NewSymbolTable(),
} }
err := sp.reload(reloadCfg) err := sp.reload(reloadCfg)
@ -391,6 +393,7 @@ func TestScrapePoolTargetLimit(t *testing.T) {
logger: log.NewNopLogger(), logger: log.NewNopLogger(),
client: http.DefaultClient, client: http.DefaultClient,
metrics: newTestScrapeMetrics(t), metrics: newTestScrapeMetrics(t),
symbolTable: labels.NewSymbolTable(),
} }
tgs := []*targetgroup.Group{} tgs := []*targetgroup.Group{}
@ -623,6 +626,7 @@ func TestScrapePoolScrapeLoopsStarted(t *testing.T) {
logger: nil, logger: nil,
client: http.DefaultClient, client: http.DefaultClient,
metrics: newTestScrapeMetrics(t), metrics: newTestScrapeMetrics(t),
symbolTable: labels.NewSymbolTable(),
} }
tgs := []*targetgroup.Group{ tgs := []*targetgroup.Group{
@ -660,7 +664,7 @@ func newBasicScrapeLoop(t testing.TB, ctx context.Context, scraper scraper, app
nopMutator, nopMutator,
app, app,
nil, nil,
nil, labels.NewSymbolTable(),
0, 0,
true, true,
false, false,