Repository: apache/kafka
File: metadata/src/main/java/org/apache/kafka/image/loader/metrics/MetadataLoaderMetrics.java (Java)

Code before:
    private final Time time;
    private final AtomicReference<MetadataVersion> currentMetadataVersion =
            new AtomicReference<>(MetadataVersion.MINIMUM_VERSION);
    private final Map<String, Short> finalizedFeatureLevels = new ConcurrentHashMap<>();
    private final AtomicInteger currentControllerId = new AtomicInteger(-1);
    private final AtomicLong handleLoadSnapshotCount = new AtomicLong(0);
    private final Consumer<Long> batchProcessingTimeNsUpdater;
    private final Consumer<Integer> batchSizesUpdater;
    private final AtomicReference<MetadataProvenance> lastAppliedProvenance;
    private final TimeRatio avgIdleTimeRatio;

    /**
     * Create a new LoaderMetrics object.
     *
     * @param registry                      The metrics registry, or Optional.empty if this is a
     *                                      test and we don't have one.
     * @param batchProcessingTimeNsUpdater  Updates the batch processing time histogram.
     * @param batchSizesUpdater             Updates the batch sizes histogram.
     * @param time                          The time object to use.
     */
    public MetadataLoaderMetrics(
        Optional<MetricsRegistry> registry,
        Consumer<Long> batchProcessingTimeNsUpdater,
        Consumer<Integer> batchSizesUpdater,
        AtomicReference<MetadataProvenance> lastAppliedProvenance,
        Time time
    ) {
        this.registry = registry;
        this.time = time;
        this.batchProcessingTimeNsUpdater = batchProcessingTimeNsUpdater;
        this.batchSizesUpdater = batchSizesUpdater;
        this.lastAppliedProvenance = lastAppliedProvenance;
        this.avgIdleTimeRatio = new TimeRatio(1);
        registry.ifPresent(r -> r.newGauge(CURRENT_METADATA_VERSION, new Gauge<Integer>() {
            @Override
            public Integer value() {
                return (int) currentMetadataVersion().featureLevel();
            }
        }));
        registry.ifPresent(r -> r.newGauge(CURRENT_CONTROLLER_ID, new Gauge<Integer>() {
            @Override
            public Integer value() {
                return currentControllerId();
            }
        }));
        registry.ifPresent(r -> r.newGauge(HANDLE_LOAD_SNAPSHOT_COUNT, new Gauge<Long>() {
            @Override
            public Long value() {
                return handleLoadSnapshotCount();
            }
        }));

Review comment (suggestion):
Minor but this module tends order `Time` as one of first few parameters. How about:
```java
public MetadataLoaderMetrics(
    Time time,
    Optional<MetricsRegistry> registry,
    ...
) {
```

Code after:
            new AtomicReference<>(MetadataVersion.MINIMUM_VERSION);
    private final Map<String, Short> finalizedFeatureLevels = new ConcurrentHashMap<>();
    private final AtomicInteger currentControllerId = new AtomicInteger(-1);
    private final AtomicLong handleLoadSnapshotCount = new AtomicLong(0);
    private final Consumer<Long> batchProcessingTimeNsUpdater;
    private final Consumer<Integer> batchSizesUpdater;
    private final AtomicReference<MetadataProvenance> lastAppliedProvenance;
    private final TimeRatio avgIdleTimeRatio;

    /**
     * Create a new LoaderMetrics object.
     *
     * @param registry                      The metrics registry, or Optional.empty if this is a
     *                                      test and we don't have one.
     * @param batchProcessingTimeNsUpdater  Updates the batch processing time histogram.
     * @param batchSizesUpdater             Updates the batch sizes histogram.
     */
    public MetadataLoaderMetrics(
        Optional<MetricsRegistry> registry,
        Consumer<Long> batchProcessingTimeNsUpdater,
        Consumer<Integer> batchSizesUpdater,
        AtomicReference<MetadataProvenance> lastAppliedProvenance
    ) {
        this.registry = registry;
        this.batchProcessingTimeNsUpdater = batchProcessingTimeNsUpdater;
        this.batchSizesUpdater = batchSizesUpdater;
        this.lastAppliedProvenance = lastAppliedProvenance;
        this.avgIdleTimeRatio = new TimeRatio(1);
        registry.ifPresent(r -> r.newGauge(CURRENT_METADATA_VERSION, new Gauge<Integer>() {
            @Override
            public Integer value() {
                return (int) currentMetadataVersion().featureLevel();
            }
        }));
        registry.ifPresent(r -> r.newGauge(CURRENT_CONTROLLER_ID, new Gauge<Integer>() {
            @Override
            public Integer value() {
                return currentControllerId();
            }
        }));
        registry.ifPresent(r -> r.newGauge(HANDLE_LOAD_SNAPSHOT_COUNT, new Gauge<Long>() {
            @Override
            public Long value() {
                return handleLoadSnapshotCount();
            }
        }));
        registry.ifPresent(r -> r.newGauge(AVERAGE_IDLE_RATIO, new Gauge<Double>() {
            @Override
            public Double value() {
                synchronized (avgIdleTimeRatio) {
                    return avgIdleTimeRatio.measure();