Repository: apache/kafka
File: clients/clients-integration-tests/src/test/java/org/apache/kafka/server/quota/CustomQuotaCallbackTest.java (Java)

Code before:

        @Override
        public void updateQuota(ClientQuotaType quotaType, ClientQuotaEntity quotaEntity, double newValue) {

        }

        @Override
        public void removeQuota(ClientQuotaType quotaType, ClientQuotaEntity quotaEntity) {

        }

        @Override
        public boolean quotaResetRequired(ClientQuotaType quotaType) {
            return true;
        }

        @Override
        public boolean updateClusterMetadata(Cluster cluster) {
            COUNTERS.computeIfAbsent(nodeId, k -> new AtomicInteger()).incrementAndGet();
            return true;
        }

        @Override
        public Set<ClientQuotaEntity> getActiveQuotasEntities() {
            return Set.of();
        }

        @Override
        public void close() {

        }

        @Override
        public void configure(Map<String, ?> configs) {
            nodeId = (String) configs.get("node.id");
        }

    }

    public static class MonitorableCustomQuotaCallback extends CustomQuotaCallback implements Monitorable {
        
        private static final String METRIC_NAME = "monitorable-custom-quota-callback-name";
        private static final String METRIC_DESCRIPTION = "monitorable-custom-quota-callback-description";

        @Override
        public void withPluginMetrics(PluginMetrics metrics) {
            MetricName metricName = metrics.metricName(METRIC_NAME, METRIC_DESCRIPTION, new LinkedHashMap<>());
            metrics.addMetric(metricName, (Gauge<Integer>) (config, now) -> 1);
        }
        
    }

Review comment (suggestion):
We shouldn't rely on updating custom quota callbacks since users may have their own implementations.

Code after:
        @Override
        public void updateQuota(ClientQuotaType quotaType, ClientQuotaEntity quotaEntity, double newValue) {

        }

        @Override
        public void removeQuota(ClientQuotaType quotaType, ClientQuotaEntity quotaEntity) {

        }

        @Override
        public boolean quotaResetRequired(ClientQuotaType quotaType) {
            return true;
        }

        @Override
        public boolean updateClusterMetadata(Cluster cluster) {
            COUNTERS.computeIfAbsent(nodeId, k -> new AtomicInteger()).incrementAndGet();
            return true;
        }

        @Override
        public void close() {

        }

        @Override
        public void configure(Map<String, ?> configs) {
            nodeId = (String) configs.get("node.id");
        }

    }

    public static class MonitorableCustomQuotaCallback extends CustomQuotaCallback implements Monitorable {
        
        private static final String METRIC_NAME = "monitorable-custom-quota-callback-name";
        private static final String METRIC_DESCRIPTION = "monitorable-custom-quota-callback-description";

        @Override
        public void withPluginMetrics(PluginMetrics metrics) {
            MetricName metricName = metrics.metricName(METRIC_NAME, METRIC_DESCRIPTION, new LinkedHashMap<>());
            metrics.addMetric(metricName, (Gauge<Integer>) (config, now) -> 1);
        }
        
    }
}