[ 
https://issues.apache.org/jira/browse/KNOX-940?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16008836#comment-16008836
 ] 

Mohammad Kamrul Islam commented on KNOX-940:
--------------------------------------------

I need recommendations for the implementation.

Knox currently utilizes dropwizard to support metrics collection and reporting.
Dropwizard also supports  framework to publish the metrics through REST , in 
addition of  other important endpoints.

Key part is to add 
[AdminServlet|http://metrics.dropwizard.io/3.1.0/manual/servlets/#adminservlet] 
into gateway service.
In addition, we need to add couple of ServletListeners.

My question is : What is the recommended way of adding new servlet in Knox? Any 
example or details will make things easier for me.

I did a POC by creating the following diff and I accessed through these cURL 
commands. This can show what we have to do ultimately. But I'm not sure : where 
this code fits the best.
 
{quote}
curl -i -k https://localhost:8443/gateway/sandbox//metrics/metrics
curl -i -k https://localhost:8443/gateway/sandbox//metrics/ping
curl -i -k 
https://localhost:8443/gateway/sandbox//metrics/healthcheck?pretty=true
{quote}

{quote}
diff --git a/gateway-server/pom.xml b/gateway-server/pom.xml
index 652e5f3b..755e3a0e 100644
--- a/gateway-server/pom.xml
+++ b/gateway-server/pom.xml
@@ -328,6 +328,11 @@
             <artifactId>xmltool</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>io.dropwizard.metrics</groupId>
+            <artifactId>metrics-servlets</artifactId>
+            <version>3.1.2</version>
+        </dependency>
 
     </dependencies>
 
diff --git 
a/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/DeploymentFactory.java
 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/DeploymentFactory.java
index d834e740..07eaed69 100644
--- 
a/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/DeploymentFactory.java
+++ 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/DeploymentFactory.java
@@ -37,6 +37,7 @@ import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
 
+import com.codahale.metrics.servlets.AdminServlet;
 import org.apache.hadoop.gateway.GatewayMessages;
 import org.apache.hadoop.gateway.GatewayServlet;
 import org.apache.hadoop.gateway.config.GatewayConfig;
@@ -356,6 +357,10 @@ public abstract class DeploymentFactory {
       wad.createFilter().filterName( filterName ).filterClass( 
GatewayServlet.class.getName() );
       wad.createFilterMapping().filterName( filterName ).urlPattern( "/*" );
     }
+    String servletName = "metrics-admin";
+    System.out.println("registering AdminServlet");
+    wad.createServlet().servletName( servletName ).servletClass( 
AdminServlet.class.getName() );
+    wad.createServletMapping().servletName( servletName ).urlPattern( 
"/metrics/*" );
     if (gatewayServices != null) {
       gatewayServices.initializeContribution(context);
     } else {
diff --git 
a/gateway-server/src/main/java/org/apache/hadoop/gateway/services/DefaultGatewayServices.java
 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/services/DefaultGatewayServices.java
index 2cf043eb..8cd672e5 100644
--- 
a/gateway-server/src/main/java/org/apache/hadoop/gateway/services/DefaultGatewayServices.java
+++ 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/services/DefaultGatewayServices.java
@@ -218,6 +218,8 @@ public class DefaultGatewayServices implements 
GatewayServices {
   @Override
   public void finalizeContribution(DeploymentContext context) {
     // Tell the provider the location of the descriptor.
-    context.getWebAppDescriptor().createListener().listenerClass( 
GatewayServicesContextListener.class.getName() );
+    
context.getWebAppDescriptor().createListener().listenerClass(GatewayServicesContextListener.class.getName());
+    
context.getWebAppDescriptor().createListener().listenerClass(GatewayMetricsServletContextListener.class.getName());
+    
context.getWebAppDescriptor().createListener().listenerClass(GatewayHealthCheckServletContextListener.class.getName());
   }
 }
diff --git 
a/gateway-server/src/main/java/org/apache/hadoop/gateway/services/GatewayHealthCheckServletContextListener.java
 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/services/GatewayHealthCheckServletContextListener.java
new file mode 100644
index 00000000..1b0f3ef5
--- /dev/null
+++ 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/services/GatewayHealthCheckServletContextListener.java
@@ -0,0 +1,19 @@
+package org.apache.hadoop.gateway.services;
+
+import com.codahale.metrics.health.HealthCheckRegistry;
+import com.codahale.metrics.servlets.HealthCheckServlet;
+
+/**
+ * Created by mislam on 5/9/17.
+ */
+public class GatewayHealthCheckServletContextListener extends 
HealthCheckServlet.ContextListener {
+
+  public static final HealthCheckRegistry HEALTH_CHECK_REGISTRY = new 
HealthCheckRegistry();
+
+  @Override
+  protected HealthCheckRegistry getHealthCheckRegistry() {
+    System.out.println("GatewayHealthCheckServletContextListener CAlled");
+    return HEALTH_CHECK_REGISTRY;
+  }
+
+}
\ No newline at end of file
diff --git 
a/gateway-server/src/main/java/org/apache/hadoop/gateway/services/GatewayMetricsServletContextListener.java
 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/services/GatewayMetricsServletContextListener.java
new file mode 100644
index 00000000..334b0c4a
--- /dev/null
+++ 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/services/GatewayMetricsServletContextListener.java
@@ -0,0 +1,15 @@
+package org.apache.hadoop.gateway.services;
+
+import com.codahale.metrics.MetricRegistry;
+import com.codahale.metrics.servlets.MetricsServlet;
+import org.apache.hadoop.gateway.services.metrics.impl.DefaultMetricsService;
+
+public class GatewayMetricsServletContextListener extends 
MetricsServlet.ContextListener {
+
+  @Override
+  protected MetricRegistry getMetricRegistry() {
+    System.out.println("GatewayMetricsServletContextListener CAlled");
+    return DefaultMetricsService.metrics;
+  }
+
+}
diff --git 
a/gateway-server/src/main/java/org/apache/hadoop/gateway/services/metrics/impl/DefaultMetricsService.java
 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/services/metrics/impl/DefaultMetricsService.java
index 0fa8a44f..af08c209 100644
--- 
a/gateway-server/src/main/java/org/apache/hadoop/gateway/services/metrics/impl/DefaultMetricsService.java
+++ 
b/gateway-server/src/main/java/org/apache/hadoop/gateway/services/metrics/impl/DefaultMetricsService.java
@@ -38,7 +38,7 @@ import java.util.ServiceLoader;
 public class DefaultMetricsService implements MetricsService {
   private static GatewayMessages LOG = MessagesFactory.get( 
GatewayMessages.class );
 
-  private final MetricRegistry metrics = new MetricRegistry();
+  public static final MetricRegistry metrics = new MetricRegistry();
 
   public static final String METRICS_REGISTRY = "metrics-registry";
{quote}




> Support REST access exposing metrics
> ------------------------------------
>
>                 Key: KNOX-940
>                 URL: https://issues.apache.org/jira/browse/KNOX-940
>             Project: Apache Knox
>          Issue Type: Task
>            Reporter: Mohammad Kamrul Islam
>            Assignee: Mohammad Kamrul Islam
>
> KNOX-643 added the support to publish metrics. However, the metrics are not 
> accessible through REST. This JIRA is to add REST support.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to