Re: [PR] KAFKA-15871: kafka-client-metrics.sh [kafka]
junrao merged PR #14926: URL: https://github.com/apache/kafka/pull/14926 -- 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
Re: [PR] KAFKA-15871: kafka-client-metrics.sh [kafka]
AndrewJSchofield commented on code in PR #14926: URL: https://github.com/apache/kafka/pull/14926#discussion_r1417454887 ## tools/src/main/java/org/apache/kafka/tools/ClientMetricsCommand.java: ## @@ -0,0 +1,377 @@ +/* + * 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); Review Comment: See comment above. This `Exit` class is used intentionally by command handlers in this package. -- 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
Re: [PR] KAFKA-15871: kafka-client-metrics.sh [kafka]
AndrewJSchofield commented on code in PR #14926: URL: https://github.com/apache/kafka/pull/14926#discussion_r1417454119 ## tools/src/main/java/org/apache/kafka/tools/ClientMetricsCommand.java: ## @@ -0,0 +1,377 @@ +/* + * 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; Review Comment: The slightly unusual `Exit.exit` is used in common with the other Java command handlers so that unit tests can intercept the exit behaviour. Look at `o.a.k.tools.TopicCommand.java` for example. So, yes, in the real world it will exit and not return, but having a return value is good for situations in which the process is not actually going to exit earlier. -- 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
Re: [PR] KAFKA-15871: kafka-client-metrics.sh [kafka]
soarez commented on code in PR #14926: URL: https://github.com/apache/kafka/pull/14926#discussion_r1417345140 ## tools/src/main/java/org/apache/kafka/tools/ClientMetricsCommand.java: ## @@ -0,0 +1,377 @@ +/* + * 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; Review Comment: This is odd. `execute()` runs `Exit.exit` in a `finally` block, so I think this return never happens? ## tools/src/main/java/org/apache/kafka/tools/ClientMetricsCommand.java: ## @@ -0,0 +1,377 @@ +/* + * 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
Re: [PR] KAFKA-15871: kafka-client-metrics.sh [kafka]
AndrewJSchofield commented on code in PR #14926: URL: https://github.com/apache/kafka/pull/14926#discussion_r1417070856 ## tools/src/test/java/org/apache/kafka/tools/ClientMetricsCommandTest.java: ## @@ -0,0 +1,273 @@ +/* + * 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 kafka.utils.Exit; +import org.apache.kafka.clients.admin.Admin; +import org.apache.kafka.clients.admin.AdminClientTestUtils; +import org.apache.kafka.clients.admin.AlterConfigsResult; +import org.apache.kafka.clients.admin.Config; +import org.apache.kafka.clients.admin.ConfigEntry; +import org.apache.kafka.clients.admin.DescribeConfigsResult; +import org.apache.kafka.clients.admin.ListClientMetricsResourcesResult; +import org.apache.kafka.common.config.ConfigResource; +import org.apache.kafka.common.protocol.Errors; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.concurrent.ExecutionException; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ClientMetricsCommandTest { +private String bootstrapServer = "localhost:9092"; +private String clientMetricsName = "cm"; + +@Test +public void testOptionsNoActionFails() { +assertInitializeInvalidOptionsExitCode(1, +new String[] {"--bootstrap-server", bootstrapServer}); +} + +@Test +public void testOptionsListSucceeds() { +ClientMetricsCommand.ClientMetricsCommandOptions opts = new ClientMetricsCommand.ClientMetricsCommandOptions( +new String[] {"--bootstrap-server", bootstrapServer, "--list"}); +assertTrue(opts.hasListOption()); +} + +@Test +public void testOptionsDescribeNoNameSucceeds() { +ClientMetricsCommand.ClientMetricsCommandOptions opts = new ClientMetricsCommand.ClientMetricsCommandOptions( +new String[] {"--bootstrap-server", bootstrapServer, "--describe"}); +assertTrue(opts.hasDescribeOption()); +} + +@Test +public void testOptionsDescribeWithNameSucceeds() { +ClientMetricsCommand.ClientMetricsCommandOptions opts = new ClientMetricsCommand.ClientMetricsCommandOptions( +new String[] {"--bootstrap-server", bootstrapServer, "--describe", "--name", clientMetricsName}); +assertTrue(opts.hasDescribeOption()); +} + +@Test +public void testOptionsDeleteNoNameFails() { +assertInitializeInvalidOptionsExitCode(1, +new String[] {"--bootstrap-server", bootstrapServer, "--delete"}); +} + +@Test +public void testOptionsDeleteWithNameSucceeds() { +ClientMetricsCommand.ClientMetricsCommandOptions opts = new ClientMetricsCommand.ClientMetricsCommandOptions( +new String[] {"--bootstrap-server", bootstrapServer, "--delete", "--name", clientMetricsName}); +assertTrue(opts.hasDeleteOption()); +} + +@Test +public void testOptionsAlterNoNameFails() { +assertInitializeInvalidOptionsExitCode(1, +new String[] {"--bootstrap-server", bootstrapServer, "--alter"}); +} + +@Test +public void testOptionsAlterGenerateNameSucceeds() { +ClientMetricsCommand.ClientMetricsCommandOptions opts = new ClientMetricsCommand.ClientMetricsCommandOptions( +new String[] {"--bootstrap-server", bootstrapServer, "--alter", "--generate-name"}); +assertTrue(opts.hasAlterOption()); +} + +@Test +public void testOptionsAlterWithNameSucceeds() { +ClientMetricsCommand.ClientMetricsCommandOptions opts = new ClientMetricsCommand.ClientMetricsCommandOptions( +new String[] {"--bootstrap-server", bootstrapServer, "--alter", "--name", clientMetricsName}); +assertTrue(opts.hasAlterOption()); +} + +@Test +public void testOptionsAlterAllOptionsSucceeds() { +
Re: [PR] KAFKA-15871: kafka-client-metrics.sh [kafka]
AndrewJSchofield commented on code in PR #14926: URL: https://github.com/apache/kafka/pull/14926#discussion_r1417065760 ## tools/src/main/java/org/apache/kafka/tools/ClientMetricsCommand.java: ## @@ -0,0 +1,377 @@ +/* + * 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); +} + +// Visible for testing +ClientMetricsService(Admin adminClient) { +this.adminClient = adminClient; +} + +public void alterClientMetrics(ClientMetricsCommandOptions opts) throws Exception { +String entityName = opts.hasGenerateNameOption() ? Uuid.randomUuid().toString() : opts.name().get(); + +Map configsToBeSet = new HashMap<>(); +opts.interval().map(intervalVal -> configsToBeSet.put("interval.ms", intervalVal.toString())); +opts.metrics().map(metricslist -> configsToBeSet.put("metrics",
Re: [PR] KAFKA-15871: kafka-client-metrics.sh [kafka]
junrao commented on code in PR #14926: URL: https://github.com/apache/kafka/pull/14926#discussion_r1416416581 ## tools/src/main/java/org/apache/kafka/tools/ClientMetricsCommand.java: ## @@ -0,0 +1,377 @@ +/* + * 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); +} + +// Visible for testing +ClientMetricsService(Admin adminClient) { +this.adminClient = adminClient; +} + +public void alterClientMetrics(ClientMetricsCommandOptions opts) throws Exception { +String entityName = opts.hasGenerateNameOption() ? Uuid.randomUuid().toString() : opts.name().get(); + +Map configsToBeSet = new HashMap<>(); +opts.interval().map(intervalVal -> configsToBeSet.put("interval.ms", intervalVal.toString())); +opts.metrics().map(metricslist -> configsToBeSet.put("metrics", metricslist.stream().collect(Collectors.joining(","; +
Re: [PR] KAFKA-15871: kafka-client-metrics.sh [kafka]
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");
[PR] KAFKA-15871: kafka-client-metrics.sh [kafka]
AndrewJSchofield opened a new pull request, #14926: URL: https://github.com/apache/kafka/pull/14926 Initial implementation of `kafka-client-metrics.sh` tools for KIP-714 and KIP-1000. ### Committer Checklist (excluded from commit message) - [ ] Verify design and implementation - [ ] Verify test coverage and CI build status - [ ] Verify documentation (including upgrade notes) -- 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