You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bookkeeper.apache.org by eo...@apache.org on 2018/07/24 12:06:13 UTC

[bookkeeper] branch master updated: ISSUE #1538: Expose MetricsServer in http server

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 1655305  ISSUE #1538: Expose MetricsServer in http server
1655305 is described below

commit 16553057b0ddba53ac169c4fef81336e2bd26116
Author: Jia Zhai <zh...@apache.org>
AuthorDate: Tue Jul 24 14:06:08 2018 +0200

    ISSUE #1538: Expose MetricsServer in http server
    
    Descriptions of the changes in this PR:
    
    Expose MetricsServer in  http server under `/metrics`
    
    Author: Jia Zhai <zh...@apache.org>
    
    Reviewers: Enrico Olivelli <eo...@gmail.com>, Sijie Guo <si...@apache.org>
    
    This closes #1546 from jiazhai/issue_1538, closes #1538
---
 .../org/apache/bookkeeper/http/HttpRouter.java     | 120 +++++++++++----------
 .../bookkeeper/http/service/NullHttpService.java   |   8 +-
 .../bookkeeper/http/vertx/TestVertxHttpServer.java |  15 +++
 site/docs/latest/admin/http.md                     |  13 +++
 4 files changed, 95 insertions(+), 61 deletions(-)

diff --git a/bookkeeper-http/http-server/src/main/java/org/apache/bookkeeper/http/HttpRouter.java b/bookkeeper-http/http-server/src/main/java/org/apache/bookkeeper/http/HttpRouter.java
index 531e86a..a856c72 100644
--- a/bookkeeper-http/http-server/src/main/java/org/apache/bookkeeper/http/HttpRouter.java
+++ b/bookkeeper-http/http-server/src/main/java/org/apache/bookkeeper/http/HttpRouter.java
@@ -29,74 +29,76 @@ import java.util.Map;
  */
 public abstract class HttpRouter<Handler> {
 
-  // Define endpoints here.
-  public static final String HEARTBEAT                    = "/heartbeat";
-  public static final String SERVER_CONFIG                = "/api/v1/config/server_config";
+    // Define endpoints here.
+    public static final String HEARTBEAT                    = "/heartbeat";
+    public static final String SERVER_CONFIG                = "/api/v1/config/server_config";
+    public static final String METRICS                      = "/metrics";
 
-  // ledger
-  public static final String DELETE_LEDGER                = "/api/v1/ledger/delete";
-  public static final String LIST_LEDGER                  = "/api/v1/ledger/list";
-  public static final String GET_LEDGER_META              = "/api/v1/ledger/metadata";
-  public static final String READ_LEDGER_ENTRY            = "/api/v1/ledger/read";
-  // bookie
-  public static final String LIST_BOOKIES                 = "/api/v1/bookie/list_bookies";
-  public static final String LIST_BOOKIE_INFO             = "/api/v1/bookie/list_bookie_info";
-  public static final String LAST_LOG_MARK                = "/api/v1/bookie/last_log_mark";
-  public static final String LIST_DISK_FILE               = "/api/v1/bookie/list_disk_file";
-  public static final String EXPAND_STORAGE               = "/api/v1/bookie/expand_storage";
-  // autorecovery
-  public static final String RECOVERY_BOOKIE              = "/api/v1/autorecovery/bookie";
-  public static final String LIST_UNDER_REPLICATED_LEDGER = "/api/v1/autorecovery/list_under_replicated_ledger";
-  public static final String WHO_IS_AUDITOR               = "/api/v1/autorecovery/who_is_auditor";
-  public static final String TRIGGER_AUDIT                = "/api/v1/autorecovery/trigger_audit";
-  public static final String LOST_BOOKIE_RECOVERY_DELAY   = "/api/v1/autorecovery/lost_bookie_recovery_delay";
-  public static final String DECOMMISSION                 = "/api/v1/autorecovery/decommission";
+    // ledger
+    public static final String DELETE_LEDGER                = "/api/v1/ledger/delete";
+    public static final String LIST_LEDGER                  = "/api/v1/ledger/list";
+    public static final String GET_LEDGER_META              = "/api/v1/ledger/metadata";
+    public static final String READ_LEDGER_ENTRY            = "/api/v1/ledger/read";
+    // bookie
+    public static final String LIST_BOOKIES                 = "/api/v1/bookie/list_bookies";
+    public static final String LIST_BOOKIE_INFO             = "/api/v1/bookie/list_bookie_info";
+    public static final String LAST_LOG_MARK                = "/api/v1/bookie/last_log_mark";
+    public static final String LIST_DISK_FILE               = "/api/v1/bookie/list_disk_file";
+    public static final String EXPAND_STORAGE               = "/api/v1/bookie/expand_storage";
+    // autorecovery
+    public static final String RECOVERY_BOOKIE              = "/api/v1/autorecovery/bookie";
+    public static final String LIST_UNDER_REPLICATED_LEDGER = "/api/v1/autorecovery/list_under_replicated_ledger";
+    public static final String WHO_IS_AUDITOR               = "/api/v1/autorecovery/who_is_auditor";
+    public static final String TRIGGER_AUDIT                = "/api/v1/autorecovery/trigger_audit";
+    public static final String LOST_BOOKIE_RECOVERY_DELAY   = "/api/v1/autorecovery/lost_bookie_recovery_delay";
+    public static final String DECOMMISSION                 = "/api/v1/autorecovery/decommission";
 
 
-  private final Map<String, Handler> endpointHandlers = new HashMap<>();
+    private final Map<String, Handler> endpointHandlers = new HashMap<>();
 
-  public HttpRouter(AbstractHttpHandlerFactory<Handler> handlerFactory) {
-    this.endpointHandlers.put(HEARTBEAT, handlerFactory.newHandler(HttpServer.ApiType.HEARTBEAT));
-    this.endpointHandlers.put(SERVER_CONFIG, handlerFactory.newHandler(HttpServer.ApiType.SERVER_CONFIG));
+    public HttpRouter(AbstractHttpHandlerFactory<Handler> handlerFactory) {
+        this.endpointHandlers.put(HEARTBEAT, handlerFactory.newHandler(HttpServer.ApiType.HEARTBEAT));
+        this.endpointHandlers.put(SERVER_CONFIG, handlerFactory.newHandler(HttpServer.ApiType.SERVER_CONFIG));
+        this.endpointHandlers.put(METRICS, handlerFactory.newHandler(HttpServer.ApiType.METRICS));
 
-    // ledger
-    this.endpointHandlers.put(DELETE_LEDGER, handlerFactory.newHandler(HttpServer.ApiType.DELETE_LEDGER));
-    this.endpointHandlers.put(LIST_LEDGER, handlerFactory.newHandler(HttpServer.ApiType.LIST_LEDGER));
-    this.endpointHandlers.put(GET_LEDGER_META, handlerFactory.newHandler(HttpServer.ApiType.GET_LEDGER_META));
-    this.endpointHandlers.put(READ_LEDGER_ENTRY, handlerFactory.newHandler(HttpServer.ApiType.READ_LEDGER_ENTRY));
+        // ledger
+        this.endpointHandlers.put(DELETE_LEDGER, handlerFactory.newHandler(HttpServer.ApiType.DELETE_LEDGER));
+        this.endpointHandlers.put(LIST_LEDGER, handlerFactory.newHandler(HttpServer.ApiType.LIST_LEDGER));
+        this.endpointHandlers.put(GET_LEDGER_META, handlerFactory.newHandler(HttpServer.ApiType.GET_LEDGER_META));
+        this.endpointHandlers.put(READ_LEDGER_ENTRY, handlerFactory.newHandler(HttpServer.ApiType.READ_LEDGER_ENTRY));
 
-    // bookie
-    this.endpointHandlers.put(LIST_BOOKIES, handlerFactory.newHandler(HttpServer.ApiType.LIST_BOOKIES));
-    this.endpointHandlers.put(LIST_BOOKIE_INFO, handlerFactory.newHandler(HttpServer.ApiType.LIST_BOOKIE_INFO));
-    this.endpointHandlers.put(LAST_LOG_MARK, handlerFactory.newHandler(HttpServer.ApiType.LAST_LOG_MARK));
-    this.endpointHandlers.put(LIST_DISK_FILE, handlerFactory.newHandler(HttpServer.ApiType.LIST_DISK_FILE));
-    this.endpointHandlers.put(EXPAND_STORAGE, handlerFactory.newHandler(HttpServer.ApiType.EXPAND_STORAGE));
+        // bookie
+        this.endpointHandlers.put(LIST_BOOKIES, handlerFactory.newHandler(HttpServer.ApiType.LIST_BOOKIES));
+        this.endpointHandlers.put(LIST_BOOKIE_INFO, handlerFactory.newHandler(HttpServer.ApiType.LIST_BOOKIE_INFO));
+        this.endpointHandlers.put(LAST_LOG_MARK, handlerFactory.newHandler(HttpServer.ApiType.LAST_LOG_MARK));
+        this.endpointHandlers.put(LIST_DISK_FILE, handlerFactory.newHandler(HttpServer.ApiType.LIST_DISK_FILE));
+        this.endpointHandlers.put(EXPAND_STORAGE, handlerFactory.newHandler(HttpServer.ApiType.EXPAND_STORAGE));
 
-    // autorecovery
-    this.endpointHandlers.put(RECOVERY_BOOKIE, handlerFactory.newHandler(HttpServer.ApiType.RECOVERY_BOOKIE));
-    this.endpointHandlers.put(LIST_UNDER_REPLICATED_LEDGER,
-      handlerFactory.newHandler(HttpServer.ApiType.LIST_UNDER_REPLICATED_LEDGER));
-    this.endpointHandlers.put(WHO_IS_AUDITOR, handlerFactory.newHandler(HttpServer.ApiType.WHO_IS_AUDITOR));
-    this.endpointHandlers.put(TRIGGER_AUDIT, handlerFactory.newHandler(HttpServer.ApiType.TRIGGER_AUDIT));
-    this.endpointHandlers.put(LOST_BOOKIE_RECOVERY_DELAY,
-      handlerFactory.newHandler(HttpServer.ApiType.LOST_BOOKIE_RECOVERY_DELAY));
-    this.endpointHandlers.put(DECOMMISSION, handlerFactory.newHandler(HttpServer.ApiType.DECOMMISSION));
-  }
+        // autorecovery
+        this.endpointHandlers.put(RECOVERY_BOOKIE, handlerFactory.newHandler(HttpServer.ApiType.RECOVERY_BOOKIE));
+        this.endpointHandlers.put(LIST_UNDER_REPLICATED_LEDGER,
+            handlerFactory.newHandler(HttpServer.ApiType.LIST_UNDER_REPLICATED_LEDGER));
+        this.endpointHandlers.put(WHO_IS_AUDITOR, handlerFactory.newHandler(HttpServer.ApiType.WHO_IS_AUDITOR));
+        this.endpointHandlers.put(TRIGGER_AUDIT, handlerFactory.newHandler(HttpServer.ApiType.TRIGGER_AUDIT));
+        this.endpointHandlers.put(LOST_BOOKIE_RECOVERY_DELAY,
+            handlerFactory.newHandler(HttpServer.ApiType.LOST_BOOKIE_RECOVERY_DELAY));
+        this.endpointHandlers.put(DECOMMISSION, handlerFactory.newHandler(HttpServer.ApiType.DECOMMISSION));
+    }
 
-  /**
-   * Bind all endpoints to corresponding handlers.
-   */
-  public void bindAll() {
-    for (Map.Entry<String, Handler> entry : endpointHandlers.entrySet()) {
-      bindHandler(entry.getKey(), entry.getValue());
+    /**
+     * Bind all endpoints to corresponding handlers.
+     */
+    public void bindAll() {
+        for (Map.Entry<String, Handler> entry : endpointHandlers.entrySet()) {
+            bindHandler(entry.getKey(), entry.getValue());
+        }
     }
-  }
 
-  /**
-   * Bind the given endpoint to its corresponding handlers.
-   * @param endpoint http endpoint
-   * @param handler http handler
-   */
-  public abstract void bindHandler(String endpoint, Handler handler);
+    /**
+     * Bind the given endpoint to its corresponding handlers.
+     * @param endpoint http endpoint
+     * @param handler http handler
+     */
+    public abstract void bindHandler(String endpoint, Handler handler);
 
 }
diff --git a/bookkeeper-http/http-server/src/main/java/org/apache/bookkeeper/http/service/NullHttpService.java b/bookkeeper-http/http-server/src/main/java/org/apache/bookkeeper/http/service/NullHttpService.java
index 13c607d..2f93f34 100644
--- a/bookkeeper-http/http-server/src/main/java/org/apache/bookkeeper/http/service/NullHttpService.java
+++ b/bookkeeper-http/http-server/src/main/java/org/apache/bookkeeper/http/service/NullHttpService.java
@@ -20,12 +20,16 @@
  */
 package org.apache.bookkeeper.http.service;
 
+import org.apache.bookkeeper.http.HttpServer;
+
 /**
- * HttpEndpointService that return empty content.
+ * HttpEndpointService that return fixed content.
  */
 public class NullHttpService implements HttpEndpointService {
+    public static final String CONTENT = "NullHttpService\n";
+
     @Override
     public HttpServiceResponse handle(HttpServiceRequest request) {
-        return new HttpServiceResponse();
+        return new HttpServiceResponse(CONTENT, HttpServer.StatusCode.OK);
     }
 }
diff --git a/bookkeeper-http/vertx-http-server/src/test/java/org/apache/bookkeeper/http/vertx/TestVertxHttpServer.java b/bookkeeper-http/vertx-http-server/src/test/java/org/apache/bookkeeper/http/vertx/TestVertxHttpServer.java
index 732aa92..59fcfd8 100644
--- a/bookkeeper-http/vertx-http-server/src/test/java/org/apache/bookkeeper/http/vertx/TestVertxHttpServer.java
+++ b/bookkeeper-http/vertx-http-server/src/test/java/org/apache/bookkeeper/http/vertx/TestVertxHttpServer.java
@@ -58,6 +58,21 @@ public class TestVertxHttpServer {
         httpServer.stopServer();
     }
 
+    @Test
+    public void testStartMetricsServiceOnRouterPath() throws Exception {
+        VertxHttpServer httpServer = new VertxHttpServer();
+        HttpServiceProvider httpServiceProvider = NullHttpServiceProvider.getInstance();
+        httpServer.initialize(httpServiceProvider);
+        int port = getNextPort();
+        while (!httpServer.startServer(port)) {
+            httpServer.stopServer();
+            port = getNextPort();
+        }
+        HttpResponse httpResponse = sendGet(getUrl(port, HttpRouter.METRICS));
+        assertEquals(HttpServer.StatusCode.OK.getValue(), httpResponse.responseCode);
+        httpServer.stopServer();
+    }
+
     // HTTP GET request
     private HttpResponse sendGet(String url) throws IOException {
         URL obj = new URL(url);
diff --git a/site/docs/latest/admin/http.md b/site/docs/latest/admin/http.md
index 0097adc..dc64744 100644
--- a/site/docs/latest/admin/http.md
+++ b/site/docs/latest/admin/http.md
@@ -60,6 +60,19 @@ Currently all the HTTP endpoints could be divided into these 4 components:
         |403 | Permission denied |
         |404 | Not found |
 
+## Config
+
+### Endpoint: /metrics
+1. Method: GET
+    * Description: Get all metrics by calling `writeAllMetrics()` of `statsProvider` internally
+    * Response:  
+    
+        | Code   | Description |
+        |:-------|:------------|
+        |200 | Successful operation |
+        |403 | Permission denied |
+        |404 | Not found |
+
 ## Ledger
 
 ### Endpoint: /api/v1/ledger/delete/?ledger_id=&lt;ledger_id&gt;