apoorvmittal10 commented on code in PR #14926:
URL: https://github.com/apache/kafka/pull/14926#discussion_r1415398373


##########
tools/src/main/java/org/apache/kafka/tools/ClientMetricsCommand.java:
##########
@@ -0,0 +1,379 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.tools;
+
+import joptsimple.ArgumentAcceptingOptionSpec;
+import joptsimple.OptionSpec;
+import joptsimple.OptionSpecBuilder;
+
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.AdminClientConfig;
+import org.apache.kafka.clients.admin.AlterConfigOp;
+import org.apache.kafka.clients.admin.AlterConfigsOptions;
+import org.apache.kafka.clients.admin.ClientMetricsResourceListing;
+import org.apache.kafka.clients.admin.Config;
+import org.apache.kafka.clients.admin.ConfigEntry;
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.config.ConfigResource;
+import org.apache.kafka.common.utils.Exit;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+public class ClientMetricsCommand {
+    private static final Logger LOG = 
LoggerFactory.getLogger(ClientMetricsCommand.class);
+
+    public static void main(String... args) {
+        Exit.exit(mainNoExit(args));
+    }
+
+     static int mainNoExit(String... args) {
+        try {
+            execute(args);
+            return 0;
+        } catch (Throwable e) {
+            System.err.println(e.getMessage());
+            System.err.println(Utils.stackTrace(e));
+            return 1;
+        }
+    }
+
+    static void execute(String... args) throws Exception {
+        ClientMetricsCommandOptions opts = new 
ClientMetricsCommandOptions(args);
+
+        Properties config = opts.commandConfig();
+        config.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, 
opts.bootstrapServer());
+
+        ClientMetricsService service = new ClientMetricsService(config);
+        int exitCode = 0;
+        try {
+            if (opts.hasAlterOption()) {
+                service.alterClientMetrics(opts);
+            } else if (opts.hasDescribeOption()) {
+                service.describeClientMetrics(opts);
+            } else if (opts.hasDeleteOption()) {
+                service.deleteClientMetrics(opts);
+            } else if (opts.hasListOption()) {
+                service.listClientMetrics(opts);
+            }
+        } catch (ExecutionException e) {
+            Throwable cause = e.getCause();
+            if (cause != null) {
+                printException(cause);
+            } else {
+                printException(e);
+            }
+            exitCode = 1;
+        } catch (Throwable t) {
+            printException(t);
+            exitCode = 1;
+        } finally {
+            service.close();
+            Exit.exit(exitCode);
+        }
+    }
+
+    public static class ClientMetricsService implements AutoCloseable {
+        private Admin adminClient;
+
+        public ClientMetricsService(Properties config) {
+            this.adminClient = Admin.create(config);
+        }
+
+        public ClientMetricsService(Admin adminClient) {

Review Comment:
   The constructor seems only be called from tests, shall we remove `public` 
scope and write `//Visible for testing` on top of constructor? 



##########
tools/src/main/java/org/apache/kafka/tools/ClientMetricsCommand.java:
##########
@@ -0,0 +1,379 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.tools;
+
+import joptsimple.ArgumentAcceptingOptionSpec;
+import joptsimple.OptionSpec;
+import joptsimple.OptionSpecBuilder;
+
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.AdminClientConfig;
+import org.apache.kafka.clients.admin.AlterConfigOp;
+import org.apache.kafka.clients.admin.AlterConfigsOptions;
+import org.apache.kafka.clients.admin.ClientMetricsResourceListing;
+import org.apache.kafka.clients.admin.Config;
+import org.apache.kafka.clients.admin.ConfigEntry;
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.config.ConfigResource;
+import org.apache.kafka.common.utils.Exit;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+public class ClientMetricsCommand {
+    private static final Logger LOG = 
LoggerFactory.getLogger(ClientMetricsCommand.class);
+
+    public static void main(String... args) {
+        Exit.exit(mainNoExit(args));
+    }
+
+     static int mainNoExit(String... args) {
+        try {
+            execute(args);
+            return 0;
+        } catch (Throwable e) {
+            System.err.println(e.getMessage());
+            System.err.println(Utils.stackTrace(e));
+            return 1;
+        }
+    }
+
+    static void execute(String... args) throws Exception {
+        ClientMetricsCommandOptions opts = new 
ClientMetricsCommandOptions(args);
+
+        Properties config = opts.commandConfig();
+        config.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, 
opts.bootstrapServer());
+
+        ClientMetricsService service = new ClientMetricsService(config);
+        int exitCode = 0;
+        try {
+            if (opts.hasAlterOption()) {
+                service.alterClientMetrics(opts);
+            } else if (opts.hasDescribeOption()) {
+                service.describeClientMetrics(opts);
+            } else if (opts.hasDeleteOption()) {
+                service.deleteClientMetrics(opts);
+            } else if (opts.hasListOption()) {
+                service.listClientMetrics(opts);
+            }
+        } catch (ExecutionException e) {
+            Throwable cause = e.getCause();
+            if (cause != null) {
+                printException(cause);
+            } else {
+                printException(e);
+            }
+            exitCode = 1;
+        } catch (Throwable t) {
+            printException(t);
+            exitCode = 1;
+        } finally {
+            service.close();
+            Exit.exit(exitCode);
+        }
+    }
+
+    public static class ClientMetricsService implements AutoCloseable {
+        private Admin adminClient;
+
+        public ClientMetricsService(Properties config) {
+            this.adminClient = Admin.create(config);
+        }
+
+        public ClientMetricsService(Admin adminClient) {
+            this.adminClient = adminClient;
+        }
+
+        public void alterClientMetrics(ClientMetricsCommandOptions opts) 
throws Exception {
+            String entityName = opts.hasGenerateNameOption() ? 
Uuid.randomUuid().toString() : opts.name().get();
+
+            HashMap<String, String> configsToBeSet = new HashMap<>();

Review Comment:
   nit
   ```suggestion
               Map<String, String> configsToBeSet = new HashMap<>();
   ```



##########
tools/src/main/java/org/apache/kafka/tools/ClientMetricsCommand.java:
##########
@@ -0,0 +1,379 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.tools;
+
+import joptsimple.ArgumentAcceptingOptionSpec;
+import joptsimple.OptionSpec;
+import joptsimple.OptionSpecBuilder;
+
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.AdminClientConfig;
+import org.apache.kafka.clients.admin.AlterConfigOp;
+import org.apache.kafka.clients.admin.AlterConfigsOptions;
+import org.apache.kafka.clients.admin.ClientMetricsResourceListing;
+import org.apache.kafka.clients.admin.Config;
+import org.apache.kafka.clients.admin.ConfigEntry;
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.config.ConfigResource;
+import org.apache.kafka.common.utils.Exit;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+public class ClientMetricsCommand {
+    private static final Logger LOG = 
LoggerFactory.getLogger(ClientMetricsCommand.class);
+
+    public static void main(String... args) {
+        Exit.exit(mainNoExit(args));
+    }
+
+     static int mainNoExit(String... args) {
+        try {
+            execute(args);
+            return 0;
+        } catch (Throwable e) {
+            System.err.println(e.getMessage());
+            System.err.println(Utils.stackTrace(e));
+            return 1;
+        }
+    }
+
+    static void execute(String... args) throws Exception {
+        ClientMetricsCommandOptions opts = new 
ClientMetricsCommandOptions(args);
+
+        Properties config = opts.commandConfig();
+        config.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, 
opts.bootstrapServer());
+
+        ClientMetricsService service = new ClientMetricsService(config);
+        int exitCode = 0;
+        try {
+            if (opts.hasAlterOption()) {
+                service.alterClientMetrics(opts);
+            } else if (opts.hasDescribeOption()) {
+                service.describeClientMetrics(opts);
+            } else if (opts.hasDeleteOption()) {
+                service.deleteClientMetrics(opts);
+            } else if (opts.hasListOption()) {
+                service.listClientMetrics(opts);
+            }
+        } catch (ExecutionException e) {
+            Throwable cause = e.getCause();
+            if (cause != null) {
+                printException(cause);
+            } else {
+                printException(e);
+            }
+            exitCode = 1;
+        } catch (Throwable t) {
+            printException(t);
+            exitCode = 1;
+        } finally {
+            service.close();
+            Exit.exit(exitCode);
+        }
+    }
+
+    public static class ClientMetricsService implements AutoCloseable {
+        private Admin adminClient;
+
+        public ClientMetricsService(Properties config) {
+            this.adminClient = Admin.create(config);
+        }
+
+        public ClientMetricsService(Admin adminClient) {
+            this.adminClient = adminClient;
+        }
+
+        public void alterClientMetrics(ClientMetricsCommandOptions opts) 
throws Exception {
+            String entityName = opts.hasGenerateNameOption() ? 
Uuid.randomUuid().toString() : opts.name().get();
+
+            HashMap<String, String> configsToBeSet = new HashMap<>();
+            opts.metrics().ifPresent(
+                    metricslist -> configsToBeSet.put("metrics", 
metricslist.stream().collect(Collectors.joining(","))));
+            opts.interval().map(intervalVal -> 
configsToBeSet.put("interval.ms", intervalVal.toString()));
+            opts.metrics().map(metricslist -> 
metricslist.stream().collect(Collectors.joining(",")));

Review Comment:
   Is it needed? I can see you processed the metrics above.



##########
tools/src/main/java/org/apache/kafka/tools/ClientMetricsCommand.java:
##########
@@ -0,0 +1,379 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.tools;
+
+import joptsimple.ArgumentAcceptingOptionSpec;
+import joptsimple.OptionSpec;
+import joptsimple.OptionSpecBuilder;
+
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.AdminClientConfig;
+import org.apache.kafka.clients.admin.AlterConfigOp;
+import org.apache.kafka.clients.admin.AlterConfigsOptions;
+import org.apache.kafka.clients.admin.ClientMetricsResourceListing;
+import org.apache.kafka.clients.admin.Config;
+import org.apache.kafka.clients.admin.ConfigEntry;
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.config.ConfigResource;
+import org.apache.kafka.common.utils.Exit;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.util.CommandDefaultOptions;
+import org.apache.kafka.server.util.CommandLineUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+public class ClientMetricsCommand {
+    private static final Logger LOG = 
LoggerFactory.getLogger(ClientMetricsCommand.class);
+
+    public static void main(String... args) {
+        Exit.exit(mainNoExit(args));
+    }
+
+     static int mainNoExit(String... args) {
+        try {
+            execute(args);
+            return 0;
+        } catch (Throwable e) {
+            System.err.println(e.getMessage());
+            System.err.println(Utils.stackTrace(e));
+            return 1;
+        }
+    }
+
+    static void execute(String... args) throws Exception {
+        ClientMetricsCommandOptions opts = new 
ClientMetricsCommandOptions(args);
+
+        Properties config = opts.commandConfig();
+        config.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, 
opts.bootstrapServer());
+
+        ClientMetricsService service = new ClientMetricsService(config);
+        int exitCode = 0;
+        try {
+            if (opts.hasAlterOption()) {
+                service.alterClientMetrics(opts);
+            } else if (opts.hasDescribeOption()) {
+                service.describeClientMetrics(opts);
+            } else if (opts.hasDeleteOption()) {
+                service.deleteClientMetrics(opts);
+            } else if (opts.hasListOption()) {
+                service.listClientMetrics(opts);
+            }
+        } catch (ExecutionException e) {
+            Throwable cause = e.getCause();
+            if (cause != null) {
+                printException(cause);
+            } else {
+                printException(e);
+            }
+            exitCode = 1;
+        } catch (Throwable t) {
+            printException(t);
+            exitCode = 1;
+        } finally {
+            service.close();
+            Exit.exit(exitCode);
+        }
+    }
+
+    public static class ClientMetricsService implements AutoCloseable {
+        private Admin adminClient;
+
+        public ClientMetricsService(Properties config) {
+            this.adminClient = Admin.create(config);
+        }
+
+        public ClientMetricsService(Admin adminClient) {
+            this.adminClient = adminClient;
+        }
+
+        public void alterClientMetrics(ClientMetricsCommandOptions opts) 
throws Exception {
+            String entityName = opts.hasGenerateNameOption() ? 
Uuid.randomUuid().toString() : opts.name().get();
+
+            HashMap<String, String> configsToBeSet = new HashMap<>();
+            opts.metrics().ifPresent(
+                    metricslist -> configsToBeSet.put("metrics", 
metricslist.stream().collect(Collectors.joining(","))));
+            opts.interval().map(intervalVal -> 
configsToBeSet.put("interval.ms", intervalVal.toString()));

Review Comment:
   If the returned value from `map` is not needed then should we use 
`ifPresent`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to