commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nico...@apache.org
Subject svn commit: r616258 - in /commons/sandbox/monitoring/trunk/src: main/java/org/apache/commons/monitoring/ main/java/org/apache/commons/monitoring/impl/ main/java/org/apache/commons/monitoring/reporting/ site/xdoc/ test/java/org/apache/commons/monitoring...
Date Tue, 29 Jan 2008 11:15:04 GMT
Author: nicolas
Date: Tue Jan 29 03:15:00 2008
New Revision: 616258

URL: http://svn.apache.org/viewvc?rev=616258&view=rev
Log:
- Event based notification for StatValue threshold
- some tiny doc
- simplified ExecutionStack usage via StopWatches auto-registration and auto-cleanup

Added:
    commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Composite.java
  (with props)
Modified:
    commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Counter.java
    commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/ExecutionStack.java
    commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Gauge.java
    commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Monitor.java
    commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Monitoring.java
    commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Repository.java
    commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/StatValue.java
    commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/StopWatch.java
    commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/CompositeCounter.java
    commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/CompositeGauge.java
    commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/RepositoryBase.java
    commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleCounter.java
    commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleGauge.java
    commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleMonitor.java
    commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleValue.java
    commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/reporting/Renderer.java
    commons/sandbox/monitoring/trunk/src/site/xdoc/index.xml
    commons/sandbox/monitoring/trunk/src/test/java/org/apache/commons/monitoring/MonitoringTest.java
    commons/sandbox/monitoring/trunk/src/test/java/org/apache/commons/monitoring/reporting/RendererTest.java

Added: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Composite.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Composite.java?rev=616258&view=auto
==============================================================================
--- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Composite.java
(added)
+++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Composite.java
Tue Jan 29 03:15:00 2008
@@ -0,0 +1,51 @@
+/*
+ * 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.commons.monitoring;
+
+import java.util.Collection;
+
+/**
+ * A composite component that delegates to a primary implementation and
+ * maintains a set of secondary instances.
+ *
+ * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
+ */
+public interface Composite<T>
+{
+    /**
+     * @return the primary instance
+     */
+    T getPrimary();
+
+    /**
+     * @return an (unmodifiable) collection of secondary instances
+     */
+    Collection<T> getSecondary();
+
+    /**
+     * Register a secondary instance
+     * @param secondary
+     */
+    public void addSecondary( T secondary );
+
+    /**
+     * Deregister a secondary instance
+     * @param secondary
+     */
+    public void removeSecondary( T secondary );
+}

Propchange: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Composite.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Composite.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Composite.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Counter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Counter.java?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Counter.java
(original)
+++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Counter.java
Tue Jan 29 03:15:00 2008
@@ -18,12 +18,19 @@
 package org.apache.commons.monitoring;
 
 /**
+ * A counter to collect application processed items (bytes received, lines processed by a
batch,
+ * time elapsed by some processing ...).
+ *
  * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
  */
 public interface Counter
     extends StatValue
 {
 
+    /**
+     * Add value to the counter. Delta should not be negative (in such case a Gauge should
be used).
+     * @param delta
+     */
     void add( long delta );
 
     /**

Modified: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/ExecutionStack.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/ExecutionStack.java?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/ExecutionStack.java
(original)
+++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/ExecutionStack.java
Tue Jan 29 03:15:00 2008
@@ -71,6 +71,22 @@
     }
 
     /**
+     *
+     * @return <code>true</code> if all stopWatches are stopped (execution is
finished)
+     */
+    public static boolean isFinished()
+    {
+        for ( StopWatch stopWatch : getExecution() )
+        {
+            if (!stopWatch.isStoped())
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
      * @return the ordered list of StopWatches used during execution
      */
     public static List<StopWatch> getExecution()

Modified: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Gauge.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Gauge.java?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Gauge.java
(original)
+++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Gauge.java
Tue Jan 29 03:15:00 2008
@@ -18,7 +18,7 @@
 package org.apache.commons.monitoring;
 
 /**
- *
+ * A StatValue to expose application state, or resource consumption (open connections, active
threads, ...)
  *
  * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
  */
@@ -26,5 +26,8 @@
 {
     void increment();
 
+    void add( long delta );
+
     void decrement();
+
 }

Modified: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Monitor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Monitor.java?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Monitor.java
(original)
+++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Monitor.java
Tue Jan 29 03:15:00 2008
@@ -44,7 +44,7 @@
  * based on a "role" String. The monitor can handle as many values as needed,
  * until any of them has a dedicated role. This allows to easily extend the
  * monitor by registering custom values.
- * 
+ *
  * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
  */
 public interface Monitor
@@ -79,10 +79,15 @@
     StatValue getValue( String role );
 
     /**
+     * Reset all StatValues (don't remove them)
+     */
+    void reset();
+
+    /**
      * Register a StatValue to the monitor with the specified role. If the
      * monitor already had a StatValue for the specified role, the registration
      * is rejected and the method returns <code>false</code>
-     * 
+     *
      * @param value the StatValue
      * @param role the StatValue role in the monitor.
      * @return <code>false</code> if there is already a StatValue for this

Modified: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Monitoring.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Monitoring.java?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Monitoring.java
(original)
+++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Monitoring.java
Tue Jan 29 03:15:00 2008
@@ -21,7 +21,7 @@
 
 /**
  * Utility class for simpified application instrumentation
- * 
+ *
  * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
  */
 public final class Monitoring
@@ -66,6 +66,11 @@
     public static void setRepository( Repository repository )
     {
         Monitoring.repository = repository;
+    }
+
+    public static Repository getRepository()
+    {
+        return repository;
     }
 
 }

Modified: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Repository.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Repository.java?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Repository.java
(original)
+++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/Repository.java
Tue Jan 29 03:15:00 2008
@@ -34,30 +34,35 @@
     /**
      * @return all monitors registered in the repository
      */
-    public Collection<Monitor> getMonitors();
+    Collection<Monitor> getMonitors();
 
     /**
      * @param category a category name
      * @return all monitors in the repository that declare this category in
      * there Key
      */
-    public Collection<Monitor> getMonitorsFromCategory( String category );
+    Collection<Monitor> getMonitorsFromCategory( String category );
 
     /**
      * @param subsystem a subsystem name
      * @return all monitors in the repository that declare this subsystem in
      * there Key
      */
-    public Collection<Monitor> getMonitorsFromSubSystem( String subsystem );
+    Collection<Monitor> getMonitorsFromSubSystem( String subsystem );
 
     /**
      * @return the categories declared by monitors in the repository
      */
-    public Set<String> getCategories();
+    Set<String> getCategories();
 
     /**
      * @return the subsystems declared by monitors in the repository
      */
-    public Set<String> getSubSystems();
+    Set<String> getSubSystems();
+
+    /**
+     * Reset all monitors (don't remove them)
+     */
+    void reset();
 
 }

Modified: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/StatValue.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/StatValue.java?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/StatValue.java
(original)
+++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/StatValue.java
Tue Jan 29 03:15:00 2008
@@ -17,6 +17,7 @@
 
 package org.apache.commons.monitoring;
 
+
 /**
  * A <code>StatValue</code> is a numerical indicator of some monitored
  * application state with support for simple statistics.
@@ -38,6 +39,11 @@
     void set( long l );
 
     /**
+     * reset the statValue
+     */
+    void reset();
+
+    /**
      * @return the minimum value
      */
     long getMin();
@@ -60,4 +66,22 @@
      */
     double getStandardDeviation();
 
+    void setMonitor( Monitor monitor );
+
+    Monitor getMonitor();
+
+    void setRole( String role );
+
+    String getRole();
+
+    void addListener( Listener listener );
+
+    void removeListener( Listener listener );
+
+    public static interface Listener
+    {
+        long getThreshold();
+
+        void exceeded( StatValue value, long l );
+    }
 }

Modified: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/StopWatch.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/StopWatch.java?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/StopWatch.java
(original)
+++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/StopWatch.java
Tue Jan 29 03:15:00 2008
@@ -43,6 +43,8 @@
     /** Monitor that is notified of process execution state */
     private final Monitor monitor;
 
+    private static boolean useExecutionStack;
+
     /**
      * Constructor.
      * <p>
@@ -60,6 +62,10 @@
         {
             monitor.getGauge( Monitor.CONCURRENCY ).increment();
         }
+        if ( useExecutionStack )
+        {
+            ExecutionStack.push( this );
+        }
     }
 
     /**
@@ -127,6 +133,10 @@
                 monitor.getCounter( Monitor.PERFORMANCES ).add( getElapsedTime() );
             }
         }
+        if ( useExecutionStack && ExecutionStack.isFinished() )
+        {
+            ExecutionStack.clear();
+        }
     }
 
     /**
@@ -171,6 +181,10 @@
                 monitor.getGauge( Monitor.CONCURRENCY ).decrement();
             }
         }
+        if ( useExecutionStack && ExecutionStack.isFinished() )
+        {
+            ExecutionStack.clear();
+        }
     }
 
     /**
@@ -247,5 +261,16 @@
         }
         return stb.toString();
 
+    }
+
+    /**
+     * Enable automatic registration to the ExecutionStack and cleanup after the
+     * last stopWatch has been stopped.
+     *
+     * @param useExecutionStack
+     */
+    public static void setUseExecutionStack( boolean useExecutionStack )
+    {
+        StopWatch.useExecutionStack = useExecutionStack;
     }
 }

Modified: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/CompositeCounter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/CompositeCounter.java?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/CompositeCounter.java
(original)
+++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/CompositeCounter.java
Tue Jan 29 03:15:00 2008
@@ -18,9 +18,13 @@
 package org.apache.commons.monitoring.impl;
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.concurrent.CopyOnWriteArrayList;
 
+import org.apache.commons.monitoring.Composite;
 import org.apache.commons.monitoring.Counter;
+import org.apache.commons.monitoring.Monitor;
+import org.apache.commons.monitoring.StatValue.Listener;
 
 /**
  * A composite implementation of {@link Counter} that delegates to a primary
@@ -32,12 +36,22 @@
  *
  * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
  */
-public class CompositeCounter implements Counter
+public class CompositeCounter implements Counter, Composite<Counter>
 {
     private Counter primary;
 
     private Collection<Counter> secondary;
 
+    public Counter getPrimary()
+    {
+        return primary;
+    }
+
+    public Collection<Counter> getSecondary()
+    {
+        return Collections.unmodifiableCollection( secondary );
+    }
+
     public CompositeCounter( Counter primary )
     {
         super();
@@ -111,6 +125,41 @@
     public String getUnit()
     {
         return primary.getUnit();
+    }
+
+    public void reset()
+    {
+        primary.reset();
+    }
+
+    public Monitor getMonitor()
+    {
+        return primary.getMonitor();
+    }
+
+    public String getRole()
+    {
+        return primary.getRole();
+    }
+
+    public void setMonitor( Monitor monitor )
+    {
+        primary.setMonitor( monitor );
+    }
+
+    public void setRole( String role )
+    {
+        primary.setRole( role );
+    }
+
+    public void addListener( Listener listener )
+    {
+        primary.addListener( listener );
+    }
+
+    public void removeListener( Listener listener )
+    {
+        primary.removeListener( listener );
     }
 
 

Modified: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/CompositeGauge.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/CompositeGauge.java?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/CompositeGauge.java
(original)
+++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/CompositeGauge.java
Tue Jan 29 03:15:00 2008
@@ -18,9 +18,13 @@
 package org.apache.commons.monitoring.impl;
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.concurrent.CopyOnWriteArrayList;
 
+import org.apache.commons.monitoring.Counter;
 import org.apache.commons.monitoring.Gauge;
+import org.apache.commons.monitoring.Monitor;
+import org.apache.commons.monitoring.StatValue.Listener;
 
 /**
  * A composite implementation of {@link Gauge} that delegates to a primary
@@ -38,7 +42,15 @@
 
     private Collection<Gauge> secondary;
 
+    public Gauge getPrimary()
+    {
+        return primary;
+    }
 
+    public Collection<Gauge> getSecondary()
+    {
+        return Collections.unmodifiableCollection( secondary );
+    }
 
     public CompositeGauge( Gauge primary )
     {
@@ -67,6 +79,15 @@
         }
     }
 
+    public void add( long delta )
+    {
+        primary.add( delta );
+        for ( Gauge gauge : secondary )
+        {
+            gauge.add( delta );
+        }
+    }
+
     public synchronized void decrement()
     {
         primary.decrement();
@@ -113,6 +134,41 @@
     public String getUnit()
     {
         return primary.getUnit();
+    }
+
+    public void reset()
+    {
+        primary.reset();
+    }
+
+    public Monitor getMonitor()
+    {
+        return primary.getMonitor();
+    }
+
+    public String getRole()
+    {
+        return primary.getRole();
+    }
+
+    public void setMonitor( Monitor monitor )
+    {
+        primary.setMonitor( monitor );
+    }
+
+    public void setRole( String role )
+    {
+        primary.setRole( role );
+    }
+
+    public void addListener( Listener listener )
+    {
+        primary.addListener( listener );
+    }
+
+    public void removeListener( Listener listener )
+    {
+        primary.removeListener( listener );
     }
 
 }

Modified: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/RepositoryBase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/RepositoryBase.java?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/RepositoryBase.java
(original)
+++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/RepositoryBase.java
Tue Jan 29 03:15:00 2008
@@ -91,8 +91,8 @@
     {
         Monitor monitor;
         monitor = new SimpleMonitor( key );
-        monitor.setValue( new SimpleCounter(), Monitor.PERFORMANCES );
-        monitor.setValue( new SimpleGauge(), Monitor.CONCURRENCY );
+        monitor.setValue( new CompositeCounter( new SimpleCounter() ), Monitor.PERFORMANCES
);
+        monitor.setValue( new CompositeGauge( new SimpleGauge() ), Monitor.CONCURRENCY );
         return monitor;
     }
 
@@ -125,5 +125,14 @@
             }
         }
         return filtered;
+    }
+
+
+    public void reset()
+    {
+        for ( Monitor monitor : monitors.values() )
+        {
+            monitor.reset();
+        }
     }
 }

Modified: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleCounter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleCounter.java?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleCounter.java
(original)
+++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleCounter.java
Tue Jan 29 03:15:00 2008
@@ -18,9 +18,11 @@
 package org.apache.commons.monitoring.impl;
 
 import org.apache.commons.monitoring.Counter;
+import org.apache.commons.monitoring.Monitor;
 
 public class SimpleCounter
-    extends SimpleValue implements Counter
+    extends SimpleValue
+    implements Counter
 {
     private long value;
 
@@ -36,19 +38,34 @@
         return value;
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    public synchronized void set( long l )
+    public synchronized void reset()
     {
-        value = l;
-        computeStats( l );
+        sum = 0;
+        sumOfSquares = 0;
+        value = 0;
     }
 
-    public synchronized void add( long delta )
+    /**
+     * {@inheritDoc}
+     */
+    public void set( long l )
     {
-        value += delta;
-        computeStats( delta );
+        synchronized ( this )
+        {
+            value = l;
+            computeStats( l );
+        }
+        notifyValueChanged( l );
+    }
+
+    public void add( long delta )
+    {
+        synchronized ( this )
+        {
+            value += delta;
+            computeStats( delta );
+        }
+        notifyValueChanged( delta );
     }
 
     @Override
@@ -62,7 +79,7 @@
     @Override
     public double getMean()
     {
-        return ((double) sum) / getHits();
+        return ( (double) sum ) / getHits();
     }
 
     @Override
@@ -77,7 +94,5 @@
         // TODO Auto-generated method stub
         return 0;
     }
-
-
 
 }

Modified: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleGauge.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleGauge.java?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleGauge.java
(original)
+++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleGauge.java
Tue Jan 29 03:15:00 2008
@@ -18,6 +18,7 @@
 package org.apache.commons.monitoring.impl;
 
 import org.apache.commons.monitoring.Gauge;
+import org.apache.commons.monitoring.Monitor;
 
 /**
  * Simple implementation of a Gauge. Maintains a total of (value * time) on each
@@ -40,16 +41,55 @@
     // Use a double so that unset can be detected as "Not a Number"
     private double firstUse = Double.NaN;
 
-    public synchronized void increment()
+    public synchronized void reset()
     {
-        long now = nanoTime();
-        computeSums( now );
-        computeStats( ++value );
-        lastUse = now;
+        // Don't reset value !
+        sum = 0;
+        sumOfSquares = 0;
+        lastUse = 0;
+        firstUse = Double.NaN;
+    }
+
+    public void increment()
+    {
+        long l;
+        synchronized ( this )
+        {
+            computeSums();
+            l = ++value;
+            computeStats( l );
+        }
+        notifyValueChanged( l );
     }
 
-    private void computeSums( long now )
+    public void add( long delta )
     {
+        long l;
+        synchronized ( this )
+        {
+            computeSums();
+            value += delta;
+            l = value;
+            computeStats( value );
+        }
+        notifyValueChanged( l );
+    }
+
+    public void decrement()
+    {
+        long l;
+        synchronized ( this )
+        {
+            computeSums();
+            l = --value;
+            computeStats( l );
+        }
+        notifyValueChanged( l );
+    }
+
+    protected void computeSums()
+    {
+        long now = nanoTime();
         if ( Double.isNaN( firstUse ) )
         {
             firstUse = now;
@@ -57,10 +97,11 @@
         else
         {
             long delta = now - lastUse;
-            long s = get() * delta;
+            long s = value * delta;
             sum += s;
             sumOfSquares += s * s;
         }
+        lastUse = now;
     }
 
     protected long nanoTime()
@@ -68,14 +109,6 @@
         return System.nanoTime();
     }
 
-    public synchronized void decrement()
-    {
-        long now = nanoTime();
-        computeSums( now );
-        computeStats( --value );
-        lastUse = now;
-    }
-
     @Override
     public synchronized double getMean()
     {
@@ -100,14 +133,11 @@
         return value;
     }
 
-    public void set( long l )
+    public synchronized void set( long l )
     {
-        long now = nanoTime();
-        computeSums( now );
-        computeStats( ++value );
+        computeSums();
         value = l;
-        lastUse = now;
+        computeStats( value );
     }
-
 
 }

Modified: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleMonitor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleMonitor.java?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleMonitor.java
(original)
+++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleMonitor.java
Tue Jan 29 03:15:00 2008
@@ -71,7 +71,17 @@
 
     public boolean setValue( StatValue value, String role )
     {
+        value.setMonitor( this );
+        value.setRole( role );
         return values.putIfAbsent( role, value ) != null;
+    }
+
+    public void reset()
+    {
+        for ( StatValue value : values.values() )
+        {
+            value.reset();
+        }
     }
 
 }

Modified: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleValue.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleValue.java?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleValue.java
(original)
+++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/impl/SimpleValue.java
Tue Jan 29 03:15:00 2008
@@ -17,6 +17,10 @@
 
 package org.apache.commons.monitoring.impl;
 
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.commons.monitoring.Monitor;
 import org.apache.commons.monitoring.StatValue;
 
 /**
@@ -28,6 +32,10 @@
 public abstract class SimpleValue
        implements StatValue
 {
+    private Monitor monitor;
+
+    private String role;
+
     private int hits;
 
     private long max;
@@ -36,12 +44,23 @@
 
     private String unit;
 
+    private List<Listener> listeners = new CopyOnWriteArrayList<Listener>();
+
+    public void addListener( Listener listener )
+    {
+        listeners.add( listener );
+    }
+
+    public void removeListener( Listener listener )
+    {
+        listeners.remove( listener );
+    }
+
     /**
      * {@inheritDoc}
      */
     public abstract double getMean();
 
-
     /**
      * {@inheritDoc}
      */
@@ -71,6 +90,18 @@
         hits++;
     }
 
+    protected void notifyValueChanged( long l )
+    {
+        // Notify listeners if Threshold exceeded
+        for ( Listener listener : listeners )
+        {
+            if ( listener.getThreshold() < l )
+            {
+                listener.exceeded( this, l );
+            }
+        }
+    }
+
     /**
      * Computes the
      * {@link http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance}
@@ -103,5 +134,25 @@
     public int getHits()
     {
         return hits;
+    }
+
+    public Monitor getMonitor()
+    {
+        return monitor;
+    }
+
+    public String getRole()
+    {
+        return role;
+    }
+
+    public void setMonitor( Monitor monitor )
+    {
+        this.monitor = monitor;
+    }
+
+    public void setRole( String role )
+    {
+        this.role = role;
     }
 }

Modified: commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/reporting/Renderer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/reporting/Renderer.java?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/reporting/Renderer.java
(original)
+++ commons/sandbox/monitoring/trunk/src/main/java/org/apache/commons/monitoring/reporting/Renderer.java
Tue Jan 29 03:15:00 2008
@@ -17,16 +17,19 @@
 
 package org.apache.commons.monitoring.reporting;
 
+import java.util.Arrays;
 import java.util.Collection;
 
 import org.apache.commons.monitoring.Monitor;
 
 /**
  * Render a collection of monitor for reporting
- * 
+ *
  * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
  */
 public interface Renderer
 {
+    Collection<String> DEFAULT_ROLES = Arrays.asList( new String[] { Monitor.CONCURRENCY,
Monitor.PERFORMANCES } );
+
     void render( Collection<Monitor> monitors );
 }

Modified: commons/sandbox/monitoring/trunk/src/site/xdoc/index.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/site/xdoc/index.xml?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/site/xdoc/index.xml (original)
+++ commons/sandbox/monitoring/trunk/src/site/xdoc/index.xml Tue Jan 29 03:15:00 2008
@@ -25,9 +25,31 @@
   <body>
 
     <section name="Commons Monitoring">
+      <p>
+        Commons Monitoring provides a simple, lightweight toolkit to instrument java application.
+        Default monitoring capabilities includes performances and thread concurrency, but
can easily be
+        extended to expose application state and resource monitoring, based on Counters and
Gauges.
+      </p>
+    </section>
 
+    <section name="Concepts">
+      <p>
+        A <b>Monitor</b> defines a control point in the application. It can be
associated to a resource,
+        a code fragment or anything relevant for the application. The monitor is unique and
retrieved from
+        a <b>Repository</b>. It is identified by a name, a category (technical
description of the application
+        component) and a subsystem (functional description).
+      </p>
+      <p>
+        The monitor maintains a set of StatValues, that can be either <b>Counter</b>s
or <b>Gauge</b>s. A
+        Counter will get incremented any time the application does its work and expose the
result to
+        the monitoring backbone (byte received, lines processed, ...). A Gauge allows the
application to
+        expose how a resource is used (active connections). Each StatValue is identified
by a ROLE in the
+        monitor.
+      </p>
       <p>
-        Commons Monitoring.
+        Commons Monitoring provides Helpers to instrument application, for example to monitor
web application
+        Request or JDBC operations, so that in many case you don't have to know commons-monitoring
API to
+        get basic instrumentation on your application.
       </p>
     </section>
 

Modified: commons/sandbox/monitoring/trunk/src/test/java/org/apache/commons/monitoring/MonitoringTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/test/java/org/apache/commons/monitoring/MonitoringTest.java?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/test/java/org/apache/commons/monitoring/MonitoringTest.java
(original)
+++ commons/sandbox/monitoring/trunk/src/test/java/org/apache/commons/monitoring/MonitoringTest.java
Tue Jan 29 03:15:00 2008
@@ -44,4 +44,47 @@
         stopWatch1.stop();
         assertEquals( 0, concurrency.get() );
     }
+
+    public void testListeners()
+        throws Exception
+    {
+        Repository repository = new RepositoryBase();
+        final Monitor monitor = repository.getMonitor( "MonitoringTest.testMonitoring", "test",
"utils" );
+        Counter counter = monitor.getCounter( Monitor.PERFORMANCES );
+
+        TestListener listener = new TestListener( monitor );
+        counter.addListener( listener );
+
+        counter.add( 1 );
+        assertEquals( "unexpected listener notification", 0, listener.count );
+        counter.add( 10 );
+        assertEquals( "listener didn't get notified", 1, listener.count );
+    }
+
+    private final class TestListener
+        implements StatValue.Listener
+    {
+        private final Monitor monitor;
+
+        long count = 0;
+
+        private TestListener( Monitor monitor )
+        {
+            this.monitor = monitor;
+        }
+
+        public void exceeded( StatValue value, long l )
+        {
+            count++;
+            assertEquals( 10, l );
+            assertEquals( Monitor.PERFORMANCES, value.getRole() );
+            assertEquals( monitor, value.getMonitor() );
+        }
+
+        public long getThreshold()
+        {
+            return 5;
+        }
+    }
+
 }

Modified: commons/sandbox/monitoring/trunk/src/test/java/org/apache/commons/monitoring/reporting/RendererTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/test/java/org/apache/commons/monitoring/reporting/RendererTest.java?rev=616258&r1=616257&r2=616258&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/test/java/org/apache/commons/monitoring/reporting/RendererTest.java
(original)
+++ commons/sandbox/monitoring/trunk/src/test/java/org/apache/commons/monitoring/reporting/RendererTest.java
Tue Jan 29 03:15:00 2008
@@ -20,7 +20,6 @@
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.LinkedList;
 
@@ -36,8 +35,6 @@
 public class RendererTest
     extends TestCase
 {
-    Collection<String> roles = Arrays.asList( new String[] { Monitor.CONCURRENCY, Monitor.PERFORMANCES
} );
-
     Collection<Monitor> monitors;
 
     @Override
@@ -62,7 +59,7 @@
         throws Exception
     {
         StringWriter out = new StringWriter();
-        Renderer renderer = new JsonRenderer( new PrintWriter( out ), roles );
+        Renderer renderer = new JsonRenderer( new PrintWriter( out ), Renderer.DEFAULT_ROLES
);
         renderer.render( monitors );
         assertEqualsIgnoreLineEnds( expected( "js" ), out.toString() );
     }
@@ -71,7 +68,7 @@
         throws Exception
     {
         StringWriter out = new StringWriter();
-        Renderer renderer = new XmlRenderer( new PrintWriter( out ), roles );
+        Renderer renderer = new XmlRenderer( new PrintWriter( out ), Renderer.DEFAULT_ROLES
);
         renderer.render( monitors );
         assertEqualsIgnoreLineEnds( expected( "xml" ), out.toString() );
     }
@@ -80,7 +77,7 @@
         throws Exception
     {
         StringWriter out = new StringWriter();
-        Renderer renderer = new TxtRenderer( new PrintWriter( out ), roles );
+        Renderer renderer = new TxtRenderer( new PrintWriter( out ), Renderer.DEFAULT_ROLES
);
         renderer.render( monitors );
         assertEqualsIgnoreLineEnds( expected( "txt" ), out.toString() );
     }



Mime
View raw message