Repository: apache/kafka
File: metadata/src/main/java/org/apache/kafka/controller/metrics/QuorumControllerMetrics.java (Java)

Code before:
        }));
        registry.ifPresent(r -> r.newGauge(TIMED_OUT_BROKER_HEARTBEAT_COUNT, new Gauge<Long>() {
            @Override
            public Long value() {
                return timedOutHeartbeats();
            }
        }));
        registry.ifPresent(r -> r.newGauge(EVENT_QUEUE_OPERATIONS_STARTED_COUNT, new Gauge<Long>() {
            @Override
            public Long value() {
                return operationsStarted();
            }
        }));
        registry.ifPresent(r -> r.newGauge(EVENT_QUEUE_OPERATIONS_TIMED_OUT_COUNT, new Gauge<Long>() {
            @Override
            public Long value() {
                return operationsTimedOut();
            }
        }));
        registry.ifPresent(r -> r.newGauge(NEW_ACTIVE_CONTROLLERS_COUNT, new Gauge<Long>() {
            @Override
            public Long value() {
                return newActiveControllers();
            }
        }));
        registry.ifPresent(r -> r.newGauge(AVERAGE_IDLE_RATIO, this.avgIdleTimeRatio));
    }

    public void updateIdleStartTime() {
        if (idleStartTime.isEmpty()) {
            idleStartTime = OptionalLong.of(time.milliseconds());
        }
    }

    public void updateIdleEndTime() {
        if (this.idleStartTime.isPresent()) {
            long idleDurationMs = Math.max(time.milliseconds() - idleStartTime.getAsLong(), 0);
            avgIdleTimeRatio.record((double) idleDurationMs, time.milliseconds());
            idleStartTime = OptionalLong.empty();
        }
    }
    public void addTimeSinceLastHeartbeatMetric(int brokerId) {
        brokerContactTimesMs.put(brokerId, time.milliseconds());
        registry.ifPresent(r -> r.newGauge(
            getBrokerIdTagMetricName(
                "KafkaController",
                TIME_SINCE_LAST_HEARTBEAT_RECEIVED_METRIC_NAME,
                brokerId
            ),
            new Gauge<Integer>() {
                @Override

Review comment (suggestion):
Since `Gauge` is a functional interface, can we use the `TimeRatio` class directly in `QuorumControllerMetrics`? Then our gauge can be an anonymous class where `value()` returns `timeRatio.meausre()`. This removes the need for either `TimeRatio` wrapper class.

Code after:
        registry.ifPresent(r -> r.newGauge(TIMED_OUT_BROKER_HEARTBEAT_COUNT, new Gauge<Long>() {
            @Override
            public Long value() {
                return timedOutHeartbeats();
            }
        }));
        registry.ifPresent(r -> r.newGauge(EVENT_QUEUE_OPERATIONS_STARTED_COUNT, new Gauge<Long>() {
            @Override
            public Long value() {
                return operationsStarted();
            }
        }));
        registry.ifPresent(r -> r.newGauge(EVENT_QUEUE_OPERATIONS_TIMED_OUT_COUNT, new Gauge<Long>() {
            @Override
            public Long value() {
                return operationsTimedOut();
            }
        }));
        registry.ifPresent(r -> r.newGauge(NEW_ACTIVE_CONTROLLERS_COUNT, new Gauge<Long>() {
            @Override
            public Long value() {
                return newActiveControllers();
            }
        }));
        registry.ifPresent(r -> r.newGauge(AVERAGE_IDLE_RATIO, new Gauge<Double>() {
            @Override
            public Double value() {
                synchronized (avgIdleTimeRatio) {
                    return avgIdleTimeRatio.measure();
                }
            }
        }));
    }

    public void updateIdleTime(long idleDurationMs) {
        synchronized (avgIdleTimeRatio) {
            avgIdleTimeRatio.record((double) idleDurationMs, time.milliseconds());
        }
    }

    public void addTimeSinceLastHeartbeatMetric(int brokerId) {
        brokerContactTimesMs.put(brokerId, time.milliseconds());
        registry.ifPresent(r -> r.newGauge(
            getBrokerIdTagMetricName(
                "KafkaController",
                TIME_SINCE_LAST_HEARTBEAT_RECEIVED_METRIC_NAME,
                brokerId
            ),
            new Gauge<Integer>() {
                @Override
                public Integer value() {