You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by pe...@apache.org on 2020/04/22 09:20:18 UTC

[pulsar] branch master updated: [pulsar-proxy] Add CLI support to get pulsar-proxy stats (#6516)

This is an automated email from the ASF dual-hosted git repository.

penghui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new f9fd59f  [pulsar-proxy] Add CLI support to get pulsar-proxy stats (#6516)
f9fd59f is described below

commit f9fd59f72dd43b45373a37d0489170b5e2765068
Author: Rajan Dhabalia <rd...@apache.org>
AuthorDate: Wed Apr 22 02:20:06 2020 -0700

    [pulsar-proxy] Add CLI support to get pulsar-proxy stats (#6516)
    
    ### Motivation
    Note: This PR is top on #6473
    In PR: #6473 we have added REST-API to get stats for the pulsar-proxy.
    However, we also need cli tool to call such api and retrieve stats.
    
    ### Modification
    Added CLI for proxy-stats.
    
    **Note:**
    I will rebase this PR once, #6473 changes are reviewed and merged.
---
 .../org/apache/pulsar/client/admin/ProxyStats.java | 45 ++++++++++++++
 .../apache/pulsar/client/admin/PulsarAdmin.java    | 10 +++
 .../client/admin/internal/ProxyStatsImpl.java      | 64 +++++++++++++++++++
 .../pulsar/admin/cli/PulsarAdminToolTest.java      | 16 +++++
 .../org/apache/pulsar/admin/cli/CmdProxyStats.java | 71 ++++++++++++++++++++++
 .../apache/pulsar/admin/cli/PulsarAdminTool.java   |  3 +
 6 files changed, 209 insertions(+)

diff --git a/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/ProxyStats.java b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/ProxyStats.java
new file mode 100644
index 0000000..d5eeee7
--- /dev/null
+++ b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/ProxyStats.java
@@ -0,0 +1,45 @@
+/**
+ * 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.pulsar.client.admin;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+
+/**
+ * Admin interface for proxy stats management.
+ */
+public interface ProxyStats {
+
+    /**
+     * Returns Connections metrics.
+     *
+     * @return
+     * @throws PulsarAdminException
+     */
+
+    JsonArray getConnections() throws PulsarAdminException;
+
+    /**
+     * Returns Topics metrics.
+     *
+     * @return
+     * @throws PulsarAdminException
+     */
+    JsonObject getTopics() throws PulsarAdminException;
+}
diff --git a/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/PulsarAdmin.java b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/PulsarAdmin.java
index 18be53d..561e72f 100644
--- a/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/PulsarAdmin.java
+++ b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/PulsarAdmin.java
@@ -38,6 +38,7 @@ import org.apache.pulsar.client.admin.internal.JacksonConfigurator;
 import org.apache.pulsar.client.admin.internal.LookupImpl;
 import org.apache.pulsar.client.admin.internal.NamespacesImpl;
 import org.apache.pulsar.client.admin.internal.NonPersistentTopicsImpl;
+import org.apache.pulsar.client.admin.internal.ProxyStatsImpl;
 import org.apache.pulsar.client.admin.internal.PulsarAdminBuilderImpl;
 import org.apache.pulsar.client.admin.internal.ResourceQuotasImpl;
 import org.apache.pulsar.client.admin.internal.SchemasImpl;
@@ -75,6 +76,7 @@ public class PulsarAdmin implements Closeable {
     private final Clusters clusters;
     private final Brokers brokers;
     private final BrokerStats brokerStats;
+    private final ProxyStats proxyStats;
     private final Tenants tenants;
     private final Properties properties;
     private final Namespaces namespaces;
@@ -191,6 +193,7 @@ public class PulsarAdmin implements Closeable {
         this.clusters = new ClustersImpl(root, auth, readTimeoutMs);
         this.brokers = new BrokersImpl(root, auth, readTimeoutMs);
         this.brokerStats = new BrokerStatsImpl(root, auth, readTimeoutMs);
+        this.proxyStats = new ProxyStatsImpl(root, auth, readTimeoutMs);
         this.tenants = new TenantsImpl(root, auth, readTimeoutMs);
         this.properties = new TenantsImpl(root, auth, readTimeoutMs);
         this.namespaces = new NamespacesImpl(root, auth, readTimeoutMs);
@@ -389,6 +392,13 @@ public class PulsarAdmin implements Closeable {
     }
 
     /**
+     * @return the proxy statics
+     */
+    public ProxyStats proxyStats() {
+        return proxyStats;
+    }
+
+    /**
      * @return the service HTTP URL that is being used
      */
     public String getServiceUrl() {
diff --git a/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/ProxyStatsImpl.java b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/ProxyStatsImpl.java
new file mode 100644
index 0000000..9652ec2
--- /dev/null
+++ b/pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/ProxyStatsImpl.java
@@ -0,0 +1,64 @@
+/**
+ * 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.pulsar.client.admin.internal;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+
+import javax.ws.rs.client.WebTarget;
+
+import org.apache.pulsar.client.admin.ProxyStats;
+import org.apache.pulsar.client.admin.PulsarAdminException;
+import org.apache.pulsar.client.api.Authentication;
+
+/**
+ * Pulsar Admin API client.
+ *
+ *
+ */
+public class ProxyStatsImpl extends BaseResource implements ProxyStats {
+
+    private final WebTarget adminProxyStats;
+
+    public ProxyStatsImpl(WebTarget target, Authentication auth, long readTimeoutMs) {
+        super(auth, readTimeoutMs);
+        adminProxyStats = target.path("/proxy-stats");
+    }
+
+    @Override
+    public JsonArray getConnections() throws PulsarAdminException {
+        try {
+            String json = request(adminProxyStats.path("/connections")).get(String.class);
+            return new Gson().fromJson(json, JsonArray.class);
+        } catch (Exception e) {
+            throw getApiException(e);
+        }
+    }
+
+    @Override
+    public JsonObject getTopics() throws PulsarAdminException {
+        try {
+            String json = request(adminProxyStats.path("/topics")).get(String.class);
+            return new Gson().fromJson(json, JsonObject.class);
+        } catch (Exception e) {
+            throw getApiException(e);
+        }
+    }
+}
diff --git a/pulsar-client-tools-test/src/test/java/org/apache/pulsar/admin/cli/PulsarAdminToolTest.java b/pulsar-client-tools-test/src/test/java/org/apache/pulsar/admin/cli/PulsarAdminToolTest.java
index 99f8aad..a68f7bf 100644
--- a/pulsar-client-tools-test/src/test/java/org/apache/pulsar/admin/cli/PulsarAdminToolTest.java
+++ b/pulsar-client-tools-test/src/test/java/org/apache/pulsar/admin/cli/PulsarAdminToolTest.java
@@ -45,6 +45,7 @@ import org.apache.pulsar.client.admin.Clusters;
 import org.apache.pulsar.client.admin.Lookup;
 import org.apache.pulsar.client.admin.Namespaces;
 import org.apache.pulsar.client.admin.NonPersistentTopics;
+import org.apache.pulsar.client.admin.ProxyStats;
 import org.apache.pulsar.client.admin.PulsarAdmin;
 import org.apache.pulsar.client.admin.ResourceQuotas;
 import org.apache.pulsar.client.admin.Schemas;
@@ -902,6 +903,21 @@ public class PulsarAdminToolTest {
         assertNull(atuh.getKeyFilePath());
     }
 
+    @Test
+    void proxy() throws Exception {
+        PulsarAdmin admin = Mockito.mock(PulsarAdmin.class);
+        ProxyStats mockProxyStats = mock(ProxyStats.class);
+        doReturn(mockProxyStats).when(admin).proxyStats();
+
+        CmdProxyStats proxyStats = new CmdProxyStats(admin);
+
+        proxyStats.run(split("connections"));
+        verify(mockProxyStats).getConnections();
+
+        proxyStats.run(split("topics"));
+        verify(mockProxyStats).getTopics();
+    }
+
     String[] split(String s) {
         return s.split(" ");
     }
diff --git a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdProxyStats.java b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdProxyStats.java
new file mode 100644
index 0000000..0f9486c
--- /dev/null
+++ b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdProxyStats.java
@@ -0,0 +1,71 @@
+/**
+ * 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.pulsar.admin.cli;
+
+import java.io.IOException;
+
+import org.apache.pulsar.client.admin.PulsarAdmin;
+
+import com.beust.jcommander.Parameter;
+import com.beust.jcommander.Parameters;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+
+@Parameters(commandDescription = "Operations to collect Proxy statistics")
+public class CmdProxyStats extends CmdBase {
+
+    @Parameters(commandDescription = "dump connections metrics for Monitoring")
+    private class CmdConnectionMetrics extends CliCommand {
+        @Parameter(names = { "-i", "--indent" }, description = "Indent JSON output", required = false)
+        private boolean indent = false;
+
+        @Override
+        void run() throws Exception {
+            JsonArray stats = admin.proxyStats().getConnections();
+            printStats(stats, indent);
+        }
+    }
+
+    @Parameters(commandDescription = "dump topics metrics for Monitoring")
+    private class CmdTopicsMetrics extends CliCommand {
+        @Parameter(names = { "-i", "--indent" }, description = "Indent JSON output", required = false)
+        private boolean indent = false;
+
+        @Override
+        void run() throws Exception {
+            JsonObject stats = admin.proxyStats().getTopics();
+            printStats(stats, indent);
+        }
+    }
+
+    public void printStats(JsonElement json, boolean indent) throws IOException {
+        GsonBuilder builder = new GsonBuilder();
+        Gson gson = indent ? builder.setPrettyPrinting().create() : builder.create();
+        System.out.println(gson.toJson(json));
+    }
+
+    public CmdProxyStats(PulsarAdmin admin) {
+        super("proxy-stats", admin);
+        jcommander.addCommand("connections", new CmdConnectionMetrics());
+        jcommander.addCommand("topics", new CmdTopicsMetrics());
+    }
+}
\ No newline at end of file
diff --git a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/PulsarAdminTool.java b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/PulsarAdminTool.java
index 73f4c8d..148fd6b 100644
--- a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/PulsarAdminTool.java
+++ b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/PulsarAdminTool.java
@@ -106,6 +106,9 @@ public class PulsarAdminTool {
 
 
         commandMap.put("resource-quotas", CmdResourceQuotas.class);
+        // pulsar-proxy cli
+        commandMap.put("proxy-stats", CmdProxyStats.class);
+        
         commandMap.put("functions", CmdFunctions.class);
         commandMap.put("functions-worker", CmdFunctionWorker.class);
         commandMap.put("sources", CmdSources.class);