private Sample advance(MetricConfig config, long timeMs) {
        // keep one extra placeholder for "overlapping sample" (see purgeObsoleteSamples() logic)
        int maxSamples = config.samples() + 1;
        this.current = (this.current + 1) % maxSamples;
        if (this.current >= samples.size()) {
            Sample sample = newSample(timeMs);
            this.samples.add(sample);
            return sample;
        } else {
            Sample sample = current(timeMs);
            sample.reset(timeMs);
            return sample;
        }
    }
A SampledStat records a single scalar value measured over one or more samples. Each sample is recorded over a
configurable window. The window can be defined by number of events or elapsed time (or both, if both are given the
window is complete when <i>either</i> the event count or elapsed time criterion is met).
<p>
All the samples are combined to produce the measurement. When a window is complete the oldest sample is cleared and
recycled to begin recording the next sample.

Subclasses of this class define different statistics measured using this basic pattern.
clients/src/main/java/org/apache/kafka/common/metrics/stats/SampledStat.java