Repository: prometheus/prometheus
File: scrape/scrape.go (Go)

Code before:
		maxSchema:                      maxSchema,
		labelLimits:                    labelLimits,
		interval:                       interval,
		timeout:                        timeout,
		parserOptions:                  parserOpts,
		alwaysScrapeClassicHist:        alwaysScrapeClassicHist,
		convertClassicHistToNHCB:       convertClassicHistToNHCB,
		enableCTZeroIngestion:          enableCTZeroIngestion,
		enableTypeAndUnitLabels:        enableTypeAndUnitLabels,
		fallbackScrapeProtocol:         fallbackScrapeProtocol,
		enableNativeHistogramIngestion: enableNativeHistogramIngestion,
		reportExtraMetrics:             reportExtraMetrics,
		appendMetadataToWAL:            appendMetadataToWAL,
		metrics:                        metrics,
		skipOffsetting:                 skipOffsetting,
		validationScheme:               validationScheme,
		escapingScheme:                 escapingScheme,
	}
	sl.ctx, sl.cancel = context.WithCancel(ctx)

	return sl
}

// rebuildParserOptions rebuilds the parser options slice.
// Used when tests modify the boolean fields after construction.
func (sl *scrapeLoop) rebuildParserOptions() {
	opts := []textparse.ParserOptions{
		textparse.WithFallbackType(sl.fallbackScrapeProtocol),
	}
	if sl.alwaysScrapeClassicHist {
		opts = append(opts, textparse.WithKeepClassicOnClassicAndNativeHistograms())
	}
	if sl.convertClassicHistToNHCB {
		opts = append(opts, textparse.WithClassicHistogramsToNHCB())
	}
	if sl.enableCTZeroIngestion {
		opts = append(opts, textparse.WithOMCTSeriesSkipped())
	}
	if sl.enableTypeAndUnitLabels {
		opts = append(opts, textparse.WithTypeAndUnitLabels())
	}
	sl.parserOptions = opts
}

func (sl *scrapeLoop) setScrapeFailureLogger(l FailureLogger) {
	sl.scrapeFailureLoggerMtx.Lock()
	defer sl.scrapeFailureLoggerMtx.Unlock()
	if ts, ok := sl.scraper.(fmt.Stringer); ok && l != nil {
		l = slog.New(l).With("target", ts.String()).Handler().(FailureLogger)
	}
	sl.scrapeFailureLogger = l

Review comment (suggestion):
@bwplotka not sure but thought this is needed to reassign the options is this okay :/ 
mostly used in [test](https://github.com/prometheus/prometheus/pull/17155/files#diff-344d08b4acfbe19edf8173007073c1f12f126c10830855eca8d47936e53f9371R4841)

Code after:
	sl.ctx, sl.cancel = context.WithCancel(ctx)

	return sl
}

func (sl *scrapeLoop) setScrapeFailureLogger(l FailureLogger) {
	sl.scrapeFailureLoggerMtx.Lock()
	defer sl.scrapeFailureLoggerMtx.Unlock()
	if ts, ok := sl.scraper.(fmt.Stringer); ok && l != nil {
		l = slog.New(l).With("target", ts.String()).Handler().(FailureLogger)
	}
	sl.scrapeFailureLogger = l
}

func (sl *scrapeLoop) run(errc chan<- error) {
	if !sl.skipOffsetting {
		select {
		case <-time.After(sl.scraper.offset(sl.interval, sl.offsetSeed)):
			// Continue after a scraping offset.
		case <-sl.ctx.Done():
			close(sl.stopped)
			return
		}
	}

	var last time.Time

	alignedScrapeTime := time.Now().Round(0)
	ticker := time.NewTicker(sl.interval)
	defer ticker.Stop()

mainLoop:
	for {
		select {
		case <-sl.parentCtx.Done():
			close(sl.stopped)
			return
		case <-sl.ctx.Done():
			break mainLoop
		default:
		}

		// Temporary workaround for a jitter in go timers that causes disk space
		// increase in TSDB.
		// See https://github.com/prometheus/prometheus/issues/7846
		// Calling Round ensures the time used is the wall clock, as otherwise .Sub
		// and .Add on time.Time behave differently (see time package docs).
		scrapeTime := time.Now().Round(0)
		if AlignScrapeTimestamps {
			// Tolerance is clamped to maximum 1% of the scrape interval.
			tolerance := min(sl.interval/100, ScrapeTimestampTolerance)