diff --git a/scrape/manager_test.go b/scrape/manager_test.go index f90fd0ce661..f503b329aa1 100644 --- a/scrape/manager_test.go +++ b/scrape/manager_test.go @@ -20,6 +20,7 @@ import ( "net/http/httptest" "net/url" "os" + "slices" "strconv" "sync" "testing" @@ -857,3 +858,53 @@ func getResultFloats(app *collectResultAppender, expectedMetricName string) (res } return result } + +func TestManagerDisableEndOfRunStalenessMarkers(t *testing.T) { + configForJob := func(jobName string) *config.ScrapeConfig { + return &config.ScrapeConfig{ + JobName: jobName, + ScrapeInterval: model.Duration(1 * time.Minute), + ScrapeTimeout: model.Duration(1 * time.Minute), + ScrapeProtocols: config.DefaultScrapeProtocols, + } + } + + cfg := &config.Config{ScrapeConfigs: []*config.ScrapeConfig{ + configForJob("one"), + configForJob("two"), + }} + + m, err := NewManager(&Options{}, nil, &nopAppendable{}, prometheus.NewRegistry()) + require.NoError(t, err) + defer m.Stop() + require.NoError(t, m.ApplyConfig(cfg)) + + // Pass targets to the manager. + tgs := map[string][]*targetgroup.Group{ + "one": {{Targets: []model.LabelSet{{"__address__": "h1"}, {"__address__": "h2"}, {"__address__": "h3"}}}}, + "two": {{Targets: []model.LabelSet{{"__address__": "h4"}}}}, + } + m.updateTsets(tgs) + m.reload() + + activeTargets := m.TargetsActive() + targetsToDisable := []*Target{ + activeTargets["one"][0], + activeTargets["one"][2], + NewTarget(labels.FromStrings("__address__", "h4"), labels.EmptyLabels(), nil), // non-existent target. + } + + // Disable end of run staleness markers for some targets. + m.DisableEndOfRunStalenessMarkers("one", targetsToDisable) + // This should be a no-op + m.DisableEndOfRunStalenessMarkers("non-existent-job", targetsToDisable) + + // Check that the end of run staleness markers are disabled for the correct targets. + for _, group := range []string{"one", "two"} { + for _, tg := range activeTargets[group] { + loop := m.scrapePools[group].loops[tg.hash()].(*scrapeLoop) + expectedDisabled := slices.Contains(targetsToDisable, tg) + require.Equal(t, expectedDisabled, loop.disabledEndOfRunStalenessMarkers.Load()) + } + } +}