You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:22:36 UTC

[sling-org-apache-sling-commons-metrics] 16/23: SLING-4080 - API to capture/measure application-level metrics

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

rombert pushed a commit to annotated tag org.apache.sling.commons.metrics-0.0.2
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-metrics.git

commit f332c2e8329aa5b1475cb7d8ebbdca99ef28b96f
Author: Chetan Mehrotra <ch...@apache.org>
AuthorDate: Thu Jan 7 06:50:56 2016 +0000

    SLING-4080 - API to capture/measure application-level metrics
    
    Add testcase for webconsole plugin
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/metrics@1723470 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  11 ++
 .../metrics/internal/MetricWebConsolePlugin.java   |  12 +-
 .../internal/MetricWebConsolePluginTest.java       | 163 +++++++++++++++++++++
 3 files changed, 180 insertions(+), 6 deletions(-)

diff --git a/pom.xml b/pom.xml
index 281b84f..aec2637 100644
--- a/pom.xml
+++ b/pom.xml
@@ -103,6 +103,11 @@
     </dependency>
 
     <dependency>
+      <groupId>org.hamcrest</groupId>
+      <artifactId>hamcrest-all</artifactId>
+      <version>1.3</version>
+    </dependency>
+    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <scope>test</scope>
@@ -123,6 +128,12 @@
       <artifactId>slf4j-simple</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>net.sourceforge.htmlunit</groupId>
+      <artifactId>htmlunit</artifactId>
+      <version>2.12</version>
+      <scope>test</scope>
+    </dependency>
 
   </dependencies>
 </project>
diff --git a/src/main/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePlugin.java b/src/main/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePlugin.java
index 8796168..f5261c5 100644
--- a/src/main/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePlugin.java
+++ b/src/main/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePlugin.java
@@ -169,7 +169,7 @@ public class MetricWebConsolePlugin extends HttpServlet implements
         pw.println("<br>");
         pw.println("<div class='table'>");
         pw.println("<div class='ui-widget-header ui-corner-top buttonGroup'>Meters</div>");
-        pw.println("<table class='nicetable'>");
+        pw.println("<table class='nicetable' id='data-meters'>");
         pw.println("<thead>");
         pw.println("<tr>");
         pw.println("<th class='header'>Name</th>");
@@ -217,7 +217,7 @@ public class MetricWebConsolePlugin extends HttpServlet implements
         pw.println("<br>");
         pw.println("<div class='table'>");
         pw.println("<div class='ui-widget-header ui-corner-top buttonGroup'>Timers</div>");
-        pw.println("<table class='nicetable'>");
+        pw.println("<table class='nicetable' id='data-timers'>");
         pw.println("<thead>");
         pw.println("<tr>");
         pw.println("<th class='header'>Name</th>");
@@ -295,7 +295,7 @@ public class MetricWebConsolePlugin extends HttpServlet implements
         pw.println("<br>");
         pw.println("<div class='table'>");
         pw.println("<div class='ui-widget-header ui-corner-top buttonGroup'>Histograms</div>");
-        pw.println("<table class='nicetable'>");
+        pw.println("<table class='nicetable' id='data-histograms'>");
         pw.println("<thead>");
         pw.println("<tr>");
         pw.println("<th class='header'>Name</th>");
@@ -357,7 +357,7 @@ public class MetricWebConsolePlugin extends HttpServlet implements
         pw.println("<br>");
         pw.println("<div class='table'>");
         pw.println("<div class='ui-widget-header ui-corner-top buttonGroup'>Counters</div>");
-        pw.println("<table class='nicetable'>");
+        pw.println("<table class='nicetable' id='data-counters'>");
         pw.println("<thead>");
         pw.println("<tr>");
         pw.println("<th class='header'>Name</th>");
@@ -393,7 +393,7 @@ public class MetricWebConsolePlugin extends HttpServlet implements
         pw.println("<br>");
         pw.println("<div class='table'>");
         pw.println("<div class='ui-widget-header ui-corner-top buttonGroup'>Guages</div>");
-        pw.println("<table class='nicetable'>");
+        pw.println("<table class='nicetable' id='data-guages'>");
         pw.println("<thead>");
         pw.println("<tr>");
         pw.println("<th class='header'>Name</th>");
@@ -424,7 +424,7 @@ public class MetricWebConsolePlugin extends HttpServlet implements
 
     //~----------------------------------------------< internal >
 
-    private MetricRegistry getConsolidatedRegistry() {
+    MetricRegistry getConsolidatedRegistry() {
         MetricRegistry registry = new MetricRegistry();
         for (Map.Entry<ServiceReference, MetricRegistry> registryEntry : registries.entrySet()){
             String metricRegistryName = (String) registryEntry.getKey().getProperty(METRIC_REGISTRY_NAME);
diff --git a/src/test/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePluginTest.java b/src/test/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePluginTest.java
new file mode 100644
index 0000000..296c9e6
--- /dev/null
+++ b/src/test/java/org/apache/sling/commons/metrics/internal/MetricWebConsolePluginTest.java
@@ -0,0 +1,163 @@
+/*
+ * 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.sling.commons.metrics.internal;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import com.codahale.metrics.JvmAttributeGaugeSet;
+import com.codahale.metrics.MetricRegistry;
+import com.gargoylesoftware.htmlunit.StringWebResponse;
+import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.WebResponse;
+import com.gargoylesoftware.htmlunit.html.HTMLParser;
+import com.gargoylesoftware.htmlunit.html.HtmlPage;
+import com.gargoylesoftware.htmlunit.html.HtmlTable;
+import org.apache.felix.inventory.Format;
+import org.apache.sling.testing.mock.osgi.MockOsgi;
+import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
+import org.junit.Rule;
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class MetricWebConsolePluginTest {
+    @Rule
+    public final OsgiContext context = new OsgiContext();
+
+    private MetricWebConsolePlugin plugin = new MetricWebConsolePlugin();
+
+    private static Map<String, Object> regProps(String name) {
+        Map<String, Object> props = new HashMap<String, Object>();
+        props.put("name", name);
+        return props;
+    }
+
+    @Test
+    public void consolidatedRegistry() throws Exception {
+        MetricRegistry reg1 = new MetricRegistry();
+        reg1.meter("test1");
+        context.registerService(MetricRegistry.class, reg1, regProps("foo"));
+
+        activatePlugin();
+
+        MetricRegistry consolidated = plugin.getConsolidatedRegistry();
+
+        //Check name decoration
+        assertEquals(1, consolidated.getMetrics().size());
+        assertTrue(consolidated.getMeters().containsKey("foo:test1"));
+
+        MetricRegistry reg2 = new MetricRegistry();
+        reg2.meter("test2");
+        context.registerService(MetricRegistry.class, reg2);
+
+        //Metric Registry without name would not be decorated
+        consolidated = plugin.getConsolidatedRegistry();
+        assertEquals(2, consolidated.getMetrics().size());
+        assertTrue(consolidated.getMeters().containsKey("test2"));
+
+        //Duplicate metric in other registry should not fail. Warning log
+        //should be generated
+        MetricRegistry reg3 = new MetricRegistry();
+        reg3.meter("test2");
+        context.registerService(MetricRegistry.class, reg3);
+        consolidated = plugin.getConsolidatedRegistry();
+        assertEquals(2, consolidated.getMetrics().size());
+
+        MetricRegistry reg4 = new MetricRegistry();
+        reg4.meter("test1");
+        context.registerService(MetricRegistry.class, reg4, regProps("bar"));
+        consolidated = plugin.getConsolidatedRegistry();
+        assertTrue(consolidated.getMeters().containsKey("foo:test1"));
+        assertTrue(consolidated.getMeters().containsKey("bar:test1"));
+    }
+
+    @Test
+    public void inventory_text() throws Exception {
+        MetricRegistry reg1 = new MetricRegistry();
+        reg1.meter("test1").mark(5);
+        context.registerService(MetricRegistry.class, reg1, regProps("foo"));
+
+        activatePlugin();
+
+        StringWriter sw = new StringWriter();
+        plugin.print(new PrintWriter(sw), Format.TEXT, false);
+
+        String out = sw.toString();
+        assertThat(out, containsString("foo:test1"));
+        assertThat(out, containsString("Meters"));
+    }
+
+    @Test
+    public void webConsolePlugin() throws Exception {
+        MetricRegistry reg1 = new MetricRegistry();
+        reg1.meter("test1").mark(5);
+        reg1.timer("test2").time().close();
+        reg1.histogram("test3").update(743);
+        reg1.counter("test4").inc(9);
+        reg1.registerAll(new JvmAttributeGaugeSet());
+        context.registerService(MetricRegistry.class, reg1, regProps("foo"));
+
+        activatePlugin();
+
+        StringWriter sw = new StringWriter();
+
+        HttpServletResponse response = mock(HttpServletResponse.class);
+        when(response.getWriter()).thenReturn(new PrintWriter(sw));
+
+        plugin.doGet(mock(HttpServletRequest.class), response);
+
+        WebClient client = new WebClient();
+        WebResponse resp = new StringWebResponse(sw.toString(), WebClient.URL_ABOUT_BLANK);
+        HtmlPage page = HTMLParser.parseHtml(resp, client.getCurrentWindow());
+
+        assertTable("data-meters", page);
+        assertTable("data-counters", page);
+        assertTable("data-timers", page);
+        assertTable("data-histograms", page);
+        assertTable("data-guages", page);
+    }
+
+    private void assertTable(String name, HtmlPage page) {
+        HtmlTable table = page.getHtmlElementById(name);
+        assertNotNull(table);
+
+        //1 for header and 1 for actual metric row
+        assertThat(table.getRowCount(), greaterThanOrEqualTo(2));
+
+    }
+
+    private void activatePlugin() {
+        MockOsgi.activate(plugin, context.bundleContext(), Collections.<String, Object>emptyMap());
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.