Wicket Metrics - Updated API due to review

Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/25b1c9ea
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/25b1c9ea
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/25b1c9ea

Branch: refs/heads/master
Commit: 25b1c9eab19e9663d3fc737238b3272258029461
Parents: 4c0c2d5
Author: Tobias Soloschenko <[email protected]>
Authored: Sun Mar 13 22:35:30 2016 +0100
Committer: Tobias Soloschenko <[email protected]>
Committed: Wed Mar 16 17:55:13 2016 +0100

----------------------------------------------------------------------
 wicket-metrics/pom.xml                          |   4 +
 .../apache/wicket/metrics/WicketMetrics.java    | 113 +++++++++++--------
 .../wicket/metrics/WicketMetricsSettings.java   |  87 ++++++++++++++
 .../src/main/resources/META-INF/aop.xml         |  13 ---
 .../src/docs/guide/monitoring/monitoring_1.gdoc |  37 +++++-
 .../src/docs/guide/monitoring/monitoring_2.gdoc |   3 +-
 .../src/docs/guide/monitoring/monitoring_3.gdoc |   2 +-
 7 files changed, 195 insertions(+), 64 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/25b1c9ea/wicket-metrics/pom.xml
----------------------------------------------------------------------
diff --git a/wicket-metrics/pom.xml b/wicket-metrics/pom.xml
index 4ed79f1..a3a6a5b 100644
--- a/wicket-metrics/pom.xml
+++ b/wicket-metrics/pom.xml
@@ -33,6 +33,10 @@
        </description>
        <dependencies>
                <dependency>
+                       <groupId>org.apache.wicket</groupId>
+                       <artifactId>wicket-core</artifactId>
+               </dependency>
+               <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <scope>provided</scope>

http://git-wip-us.apache.org/repos/asf/wicket/blob/25b1c9ea/wicket-metrics/src/main/java/org/apache/wicket/metrics/WicketMetrics.java
----------------------------------------------------------------------
diff --git 
a/wicket-metrics/src/main/java/org/apache/wicket/metrics/WicketMetrics.java 
b/wicket-metrics/src/main/java/org/apache/wicket/metrics/WicketMetrics.java
index 52ca261..6331a78 100644
--- a/wicket-metrics/src/main/java/org/apache/wicket/metrics/WicketMetrics.java
+++ b/wicket-metrics/src/main/java/org/apache/wicket/metrics/WicketMetrics.java
@@ -16,9 +16,10 @@
  */
 package org.apache.wicket.metrics;
 
+import org.apache.wicket.Application;
+import org.apache.wicket.MetaDataKey;
 import org.aspectj.lang.ProceedingJoinPoint;
 
-import com.codahale.metrics.JmxReporter;
 import com.codahale.metrics.MetricRegistry;
 import com.codahale.metrics.Timer.Context;
 
@@ -31,24 +32,32 @@ import com.codahale.metrics.Timer.Context;
 public class WicketMetrics
 {
 
-       private static MetricRegistry metricRegistry;
-
-       private static boolean enabled = true;
-
-       private static final String PREFIX = "ApacheWicket/";
-
+       /** The key for metrics **/
+       public static final MetaDataKey<WicketMetrics> METRICS = new 
MetaDataKey<WicketMetrics>()
+       {
+               private static final long serialVersionUID = 1L;
+       };
+       
+       /** The key for metrics registry **/
+       public static final MetaDataKey<MetricRegistry> METRIC_REGISTRY = new 
MetaDataKey<MetricRegistry>()
+       {
+               private static final long serialVersionUID = 1L;
+       };
+       
+       /** The key for metrics registry **/
+       public static final MetaDataKey<WicketMetricsSettings> METRIC_SETTINGS 
= new MetaDataKey<WicketMetricsSettings>()
+       {
+               private static final long serialVersionUID = 1L;
+       };
+       
        /**
-        * Gets the metric registry
-        * 
-        * @return the metric registry
+        * Creates the wicket metrics
         */
-       public static MetricRegistry getMetricRegistry()
+       public WicketMetrics()
        {
-               if (metricRegistry == null)
-               {
-                       metricRegistry = new MetricRegistry();
-               }
-               return metricRegistry;
+               Application application = Application.get();
+               application.setMetaData(METRICS, this);
+               application.setMetaData(METRIC_SETTINGS, new 
WicketMetricsSettings());
        }
 
        /**
@@ -64,10 +73,13 @@ public class WicketMetrics
         */
        public Object measureTime(String name, ProceedingJoinPoint joinPoint) 
throws Throwable
        {
-               if (WicketMetrics.enabled)
+               WicketMetricsSettings settings = getSettings();
+               MetricRegistry registry = getMetricRegistry();
+               
+               if (settings.isEnabled())
                {
-                       Context context = getMetricRegistry().timer(PREFIX + 
name + renderClassName(joinPoint))
-                               .time();
+                       Context context = registry
+                               .timer(settings.getPrefix() + name + 
renderClassName(joinPoint)).time();
                        try
                        {
                                return joinPoint.proceed();
@@ -95,9 +107,12 @@ public class WicketMetrics
         */
        public Object mark(String name, ProceedingJoinPoint joinPoint) throws 
Throwable
        {
-               if (WicketMetrics.enabled)
+               WicketMetricsSettings settings = getSettings();
+               MetricRegistry registry = getMetricRegistry();
+               
+               if (settings.isEnabled())
                {
-                       getMetricRegistry().meter(PREFIX + name + 
renderClassName(joinPoint)).mark();
+                       registry.meter(settings.getPrefix() + name + 
renderClassName(joinPoint)).mark();
                }
                if (joinPoint != null)
                {
@@ -107,7 +122,7 @@ public class WicketMetrics
        }
 
        /**
-        * Stops the contex quietly
+        * Stops the context quietly
         * 
         * @param context
         *            the context to stop
@@ -121,42 +136,50 @@ public class WicketMetrics
        }
 
        /**
-        * Starts the jmx reporter
-        */
-       public static void startJmxReporter()
-       {
-               JmxReporter.forRegistry(getMetricRegistry()).build().start();
-       }
-
-       /**
-        * Stops the jmx reporter
+        * Renders the class name of the given join point
+        * 
+        * @param joinPoint
+        *            the join point to get the class of
+        * @return the class name representation
         */
-       public static void stopJmxReporter()
+       public String renderClassName(ProceedingJoinPoint joinPoint)
        {
-               JmxReporter.forRegistry(getMetricRegistry()).build().stop();
+               return joinPoint != null
+                       ? "/" + 
joinPoint.getTarget().getClass().getName().replace('.', '_') : "";
        }
 
        /**
-        * If the metrics should be enabled
+        * Gets the metric registry
         * 
-        * @param enabled
-        *            if the metrics should be enabled
+        * @return the metric registry
         */
-       public static void setEnabled(boolean enabled)
+       private MetricRegistry getMetricRegistry()
        {
-               WicketMetrics.enabled = enabled;
+               Application application = Application.get();
+               MetricRegistry metricRegistry = 
application.getMetaData(METRIC_REGISTRY);
+               if (metricRegistry == null)
+               {
+                       metricRegistry = new MetricRegistry();
+                       application.setMetaData(METRIC_REGISTRY, 
metricRegistry);
+               }
+               return metricRegistry;
        }
 
        /**
-        * Renders the class name of the given join point
+        * Gets the wicket metrics settings
         * 
-        * @param joinPoint
-        *            the join point to get the class of
-        * @return the class name representation
+        * @return the wicket metrics settings
         */
-       public String renderClassName(ProceedingJoinPoint joinPoint)
+       private WicketMetricsSettings getSettings()
        {
-               return joinPoint != null
-                       ? "/" + 
joinPoint.getTarget().getClass().getName().replace('.', '_') : "";
+               Application application = Application.get();
+               
+               WicketMetricsSettings metricRegistry = 
application.getMetaData(METRIC_SETTINGS);
+               if (metricRegistry == null)
+               {
+                       metricRegistry = new WicketMetricsSettings();
+                       Application.get().setMetaData(METRIC_SETTINGS, 
metricRegistry);
+               }
+               return metricRegistry;
        }
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/25b1c9ea/wicket-metrics/src/main/java/org/apache/wicket/metrics/WicketMetricsSettings.java
----------------------------------------------------------------------
diff --git 
a/wicket-metrics/src/main/java/org/apache/wicket/metrics/WicketMetricsSettings.java
 
b/wicket-metrics/src/main/java/org/apache/wicket/metrics/WicketMetricsSettings.java
new file mode 100644
index 0000000..1bbaef2
--- /dev/null
+++ 
b/wicket-metrics/src/main/java/org/apache/wicket/metrics/WicketMetricsSettings.java
@@ -0,0 +1,87 @@
+/*
+ * 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.wicket.metrics;
+
+import org.apache.wicket.Application;
+
+import com.codahale.metrics.JmxReporter;
+import com.codahale.metrics.MetricRegistry;
+
+/**
+ * Settings to configure wicket metrics
+ * 
+ * @author Tobias Soloschenko
+ *
+ */
+public class WicketMetricsSettings
+{
+
+       private boolean enabled = true;
+
+       private String prefix = "ApacheWicket/";
+
+       /**
+        * If the metrics should be enabled
+        * 
+        * @param enabled
+        *            if the metrics should be enabled
+        */
+       public void setEnabled(boolean enabled)
+       {
+               this.enabled = enabled;
+       }
+
+       /**
+        * If the wicket metrics are enabled
+        * 
+        * @return if the wicket metrics are enabled
+        */
+       public boolean isEnabled()
+       {
+               return enabled;
+       }
+
+       /**
+        * Gets the prefix.
+        * 
+        * @return the prefix
+        */
+       public String getPrefix()
+       {
+               return prefix;
+       }
+
+       /**
+        * Starts the jmx reporter
+        */
+       public void startJmxReporter()
+       {
+               MetricRegistry metricRegistry = Application.get()
+                       .getMetaData(WicketMetrics.METRIC_REGISTRY);
+               JmxReporter.forRegistry(metricRegistry).build().start();
+       }
+
+       /**
+        * Stops the jmx reporter
+        */
+       public void stopJmxReporter()
+       {
+               MetricRegistry metricRegistry = Application.get()
+                       .getMetaData(WicketMetrics.METRIC_REGISTRY);
+               JmxReporter.forRegistry(metricRegistry).build().stop();
+       }
+}

http://git-wip-us.apache.org/repos/asf/wicket/blob/25b1c9ea/wicket-metrics/src/main/resources/META-INF/aop.xml
----------------------------------------------------------------------
diff --git a/wicket-metrics/src/main/resources/META-INF/aop.xml 
b/wicket-metrics/src/main/resources/META-INF/aop.xml
deleted file mode 100644
index 0d8ee24..0000000
--- a/wicket-metrics/src/main/resources/META-INF/aop.xml
+++ /dev/null
@@ -1,13 +0,0 @@
-<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" 
"http://www.eclipse.org/aspectj/dtd/aspectj.dtd";>
-<aspectj>
-    <weaver options="-nowarn">
-        <include within="org.apache.wicket..*"/>
-    </weaver>
-    <aspects>
-               <aspect name="org.apache.wicket.metrics.aspects.BehaviorAspect" 
/>
-               <aspect 
name="org.apache.wicket.metrics.aspects.IPartialPageRequestHandlerAspect" />
-               <aspect 
name="org.apache.wicket.metrics.aspects.ComponentAspect" />
-               <aspect 
name="org.apache.wicket.metrics.aspects.WicketFilterAspect" />
-               <aspect 
name="org.apache.wicket.metrics.aspects.ResourceReferenceAspect" />
-    </aspects>
-</aspectj>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/wicket/blob/25b1c9ea/wicket-user-guide/src/docs/guide/monitoring/monitoring_1.gdoc
----------------------------------------------------------------------
diff --git a/wicket-user-guide/src/docs/guide/monitoring/monitoring_1.gdoc 
b/wicket-user-guide/src/docs/guide/monitoring/monitoring_1.gdoc
index 608b9c2..6d2dab5 100644
--- a/wicket-user-guide/src/docs/guide/monitoring/monitoring_1.gdoc
+++ b/wicket-user-guide/src/docs/guide/monitoring/monitoring_1.gdoc
@@ -1,4 +1,4 @@
-This is a little example how to setup wicket-metrics within a tomcat.
+This is a little example how to setup wicket-metrics within a Apache Tomcat.
 
 (1)  Add the maven dependency to your project
 {code}
@@ -13,7 +13,36 @@ This is a little example how to setup wicket-metrics within 
a tomcat.
 
 (3) Add the java agent to the jvm start options of your tomcat: 
-javaagent:/pathToServer/lib/aspectjweaver-x.x.x.jar
 
-(4) To enable the JMX measurement write the following line into your init 
method of your Application (Now you are able to connect with jvisualvm to your 
server and have a look at the data):
+(4) Add an aop.xml to your project with the metrics you want to use (aspect 
tags) - if you don't want to enable a metrics just remove the aspect tag:
 {code}
-WicketMetrics.startJmxReporter();
-{code}
\ No newline at end of file
+<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" 
"http://www.eclipse.org/aspectj/dtd/aspectj.dtd";>
+<aspectj>
+    <weaver options="-nowarn">
+        <include within="org.apache.wicket..*"/>
+    </weaver>
+    <aspects>
+               <aspect name="org.apache.wicket.metrics.aspects.BehaviorAspect" 
/>
+               <aspect 
name="org.apache.wicket.metrics.aspects.IPartialPageRequestHandlerAspect" />
+               <aspect 
name="org.apache.wicket.metrics.aspects.ComponentAspect" />
+               <aspect 
name="org.apache.wicket.metrics.aspects.WicketFilterAspect" />
+               <aspect 
name="org.apache.wicket.metrics.aspects.ResourceReferenceAspect" />
+    </aspects>
+</aspectj>
+{code}
+
+(5 - optional) To enable the JMX measurement write the following line into 
your init method of your Application (Now you are able to connect with 
jvisualvm to your server and have a look at the data):
+{code}
+Application.get().getMetaData(WicketMetrics.METRIC_SETTINGS).startJmxReporter();
+{code}
+
+To deactivate:
+{code}
+Application.get().getMetaData(WicketMetrics.METRIC_SETTINGS).stopJmxReporter();
+{code}
+
+To disable measurement:
+{code}
+Application.get().getMetaData(WicketMetrics.METRIC_SETTINGS).setEnable(false);
+{code}
+
+

http://git-wip-us.apache.org/repos/asf/wicket/blob/25b1c9ea/wicket-user-guide/src/docs/guide/monitoring/monitoring_2.gdoc
----------------------------------------------------------------------
diff --git a/wicket-user-guide/src/docs/guide/monitoring/monitoring_2.gdoc 
b/wicket-user-guide/src/docs/guide/monitoring/monitoring_2.gdoc
index b77e799..be330ce 100644
--- a/wicket-user-guide/src/docs/guide/monitoring/monitoring_2.gdoc
+++ b/wicket-user-guide/src/docs/guide/monitoring/monitoring_2.gdoc
@@ -11,8 +11,9 @@ To visualize the metrics with Graphite a little additional 
configuration is requ
 
 (2) Add the following code to your Application's init method:
 {code}
+MetricRegistry metricRegistry = 
Application.get().getMetaData(WicketMetrics.METRIC_REGISTRY)
 final Graphite graphite = new Graphite(new InetSocketAddress("127.0.0.1", 
2003));
-final GraphiteReporter reporter = 
GraphiteReporter.forRegistry(WicketMetrics.getMetricRegistry())
+final GraphiteReporter reporter = GraphiteReporter.forRegistry(metricRegistry)
                                                   
.prefixedWith("WebApplications")
                                                   
.convertRatesTo(TimeUnit.SECONDS)
                                                   
.convertDurationsTo(TimeUnit.MILLISECONDS)

http://git-wip-us.apache.org/repos/asf/wicket/blob/25b1c9ea/wicket-user-guide/src/docs/guide/monitoring/monitoring_3.gdoc
----------------------------------------------------------------------
diff --git a/wicket-user-guide/src/docs/guide/monitoring/monitoring_3.gdoc 
b/wicket-user-guide/src/docs/guide/monitoring/monitoring_3.gdoc
index 6760b6f..56fcd89 100644
--- a/wicket-user-guide/src/docs/guide/monitoring/monitoring_3.gdoc
+++ b/wicket-user-guide/src/docs/guide/monitoring/monitoring_3.gdoc
@@ -1,6 +1,6 @@
 The data which is going to be measured depends on the wicket-metrics 
implementation. So it doesn't make any sense to collect time data
 
-about setRepsonsePage, but it does for the constructor of components, to see 
if a component needs a long time to be created. You can
+about setResponsePage, but it does for the constructor of components, to see 
if a component needs a long time to be created. You can
 
 get the information about which data has been collected from out of the mbeans.
 

Reply via email to