Author: nicolas
Date: Wed Jan  7 06:09:33 2009
New Revision: 732346

URL: http://svn.apache.org/viewvc?rev=732346&view=rev
Log:
reporting module

Added:
    commons/sandbox/monitoring/branches/modules/reporting/   (with props)
    commons/sandbox/monitoring/branches/modules/reporting/pom.xml
    commons/sandbox/monitoring/branches/modules/reporting/src/
    commons/sandbox/monitoring/branches/modules/reporting/src/main/
    commons/sandbox/monitoring/branches/modules/reporting/src/main/java/
    commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/
    
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/
    
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/
    
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/
    
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/
    
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLogger.java
    
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/MetricData.java
    
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/Selector.java
    commons/sandbox/monitoring/branches/modules/reporting/src/main/resources/
    commons/sandbox/monitoring/branches/modules/reporting/src/test/
    commons/sandbox/monitoring/branches/modules/reporting/src/test/java/
    commons/sandbox/monitoring/branches/modules/reporting/src/test/java/org/
    
commons/sandbox/monitoring/branches/modules/reporting/src/test/java/org/apache/
    
commons/sandbox/monitoring/branches/modules/reporting/src/test/java/org/apache/commons/
    
commons/sandbox/monitoring/branches/modules/reporting/src/test/java/org/apache/commons/monitoring/
    
commons/sandbox/monitoring/branches/modules/reporting/src/test/java/org/apache/commons/monitoring/reporting/
    commons/sandbox/monitoring/branches/modules/reporting/src/test/resources/
Modified:
    commons/sandbox/monitoring/branches/modules/pom.xml

Modified: commons/sandbox/monitoring/branches/modules/pom.xml
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/pom.xml?rev=732346&r1=732345&r2=732346&view=diff
==============================================================================
--- commons/sandbox/monitoring/branches/modules/pom.xml (original)
+++ commons/sandbox/monitoring/branches/modules/pom.xml Wed Jan  7 06:09:33 2009
@@ -39,6 +39,7 @@
     <module>core</module>
     <module>instrumentation</module>
     <module>spring</module>
+    <module>reporting</module>
   </modules>
 
   <developers>

Propchange: commons/sandbox/monitoring/branches/modules/reporting/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Wed Jan  7 06:09:33 2009
@@ -0,0 +1,5 @@
+.settings
+bin
+target
+.classpath
+.project

Added: commons/sandbox/monitoring/branches/modules/reporting/pom.xml
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/reporting/pom.xml?rev=732346&view=auto
==============================================================================
--- commons/sandbox/monitoring/branches/modules/reporting/pom.xml (added)
+++ commons/sandbox/monitoring/branches/modules/reporting/pom.xml Wed Jan  7 
06:09:33 2009
@@ -0,0 +1,16 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+  <parent>
+    <artifactId>commons-monitoring-parent</artifactId>
+    <groupId>org.apache.commons.monitoring</groupId>
+    <version>1.0-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <artifactId>commons-monitoring-reporting</artifactId>
+  <dependencies>
+       <dependency>
+               <groupId>org.apache.commons.monitoring</groupId>
+               <artifactId>commons-monitoring</artifactId>
+               <version>${project.version}</version>
+       </dependency>
+  </dependencies>
+</project>
\ No newline at end of file

Added: 
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLogger.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLogger.java?rev=732346&view=auto
==============================================================================
--- 
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLogger.java
 (added)
+++ 
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLogger.java
 Wed Jan  7 06:09:33 2009
@@ -0,0 +1,126 @@
+/*
+ * 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.reporting;
+
+import java.util.Timer;
+import java.util.TimerTask;
+
+import org.apache.commons.monitoring.Repository;
+import org.apache.commons.monitoring.repositories.ObserverRepository;
+
+/**
+ * Base class to periodically log a fixed set of monitored data.
+ * 
+ * @author <a href="mailto:nico...@apache.org";>Nicolas De Loof</a>
+ */
+public abstract class AbstractPeriodicLogger
+    extends TimerTask
+{
+    /** The timer that handles the period */
+    private Timer timer;
+
+    /** The observed repository */
+    private Repository.Observable repository;
+
+    /** The observed repository */
+    private ObserverRepository secondary;
+
+    /** The interval for periodic logging of monitored state */
+    private int period;
+
+    /**
+     * @param period the period (in ms) to log the monitoring state
+     * @param repository the observed repository
+     */
+    public AbstractPeriodicLogger( int period, Repository.Observable 
repository )
+    {
+        this.repository = repository;
+        this.timer = new Timer();
+        this.period = period;
+    }
+
+    public void init()
+    {
+        observeRepositoryForPeriod();
+        timer.scheduleAtFixedRate( this, period, period );
+    }
+
+    private Repository observeRepositoryForPeriod()
+    {
+        ObserverRepository previous = this.secondary;
+        this.secondary = new ObserverRepository( repository );
+        if ( previous != null )
+        {
+            previous.detach();
+        }
+        return previous;
+    }
+
+    public void stop()
+    {
+        timer.cancel();
+    }
+
+    /**
+     * {...@inheritdoc}
+     *
+     * @see java.util.TimerTask#run()
+     */
+    @Override
+    public void run()
+    {
+        try
+        {
+            Repository observedPeriod = observeRepositoryForPeriod();
+            log( observedPeriod );
+        }
+        catch ( Throwable t )
+        {
+            // catch any exception, as throwing it will stop the timer
+            handleError( t );
+        }
+    }
+
+    /**
+     * Use the data from the (observer) repository generated during the last 
period
+     *
+     * @param observeRepositoryForPeriod
+     */
+    protected abstract void log( Repository repositoryForPeriod );
+
+    /**
+     * Warn when logging the repository failed.
+     * <p>
+     * This method is expected to be override by user to avoid System.err 
outputs and use the application logging
+     * strategy.
+     *
+     * @param t error during logging
+     */
+    protected void handleError( Throwable t )
+    {
+        System.err.println( "Failure to log observed repository : " + 
t.getMessage() );
+    }
+
+    /**
+     * @return the SecondaryRepository active for the current period.
+     */
+    protected Repository getRepositoryForActivePeriod()
+    {
+        return this.secondary;
+    }
+}

Added: 
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/MetricData.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/MetricData.java?rev=732346&view=auto
==============================================================================
--- 
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/MetricData.java
 (added)
+++ 
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/MetricData.java
 Wed Jan  7 06:09:33 2009
@@ -0,0 +1,132 @@
+/*
+ * 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.reporting;
+
+import org.apache.commons.monitoring.Gauge;
+import org.apache.commons.monitoring.Metric;
+
+/**
+ * An enum to acces data from a Metric based on the property name. Can be used 
to avoid reflection on Metric
+ * implementation when requesting data and undesirable exposure of internals.
+ * <p>
+ * example :
+ * 
+ * <pre>
+ * String property = httpServletRequest.getParameter( &quot;property&quot; );
+ * 
+ * Double data = MetricData.valueOf( property ).value( metric );
+ * </pre>
+ * 
+ * @author <a href="mailto:nico...@apache.org";>Nicolas De Loof</a>
+ */
+public enum MetricData
+{
+    Hits
+    {
+        @Override
+        public double value( Metric metric )
+        {
+            return metric.getHits();
+        }
+    },
+    Max
+    {
+        @Override
+        public double value( Metric metric )
+        {
+            return metric.getMax();
+        }
+    },
+    Mean
+    {
+        @Override
+        public double value( Metric metric )
+        {
+            return metric.getMean();
+        }
+    },
+    Min
+    {
+        @Override
+        public double value( Metric metric )
+        {
+            return metric.getMin();
+        }
+    },
+    StandardDeviation
+    {
+        @Override
+        public double value( Metric metric )
+        {
+            return metric.getStandardDeviation();
+        }
+    },
+    Sum
+    {
+        @Override
+        public double value( Metric metric )
+        {
+            return metric.getSum();
+        }
+    },
+    SumOfLogs
+    {
+        @Override
+        public double value( Metric metric )
+        {
+            return metric.getSumOfLogs();
+        }
+    },
+    SumOfSquares
+    {
+        @Override
+        public double value( Metric metric )
+        {
+            return metric.getSumOfSquares();
+        }
+    },
+    Variance
+    {
+        @Override
+        public double value( Metric metric )
+        {
+            return metric.getVariance();
+        }
+    },
+    GeometricMean
+    {
+        @Override
+        public double value( Metric metric )
+        {
+            return metric.getGeometricMean();
+        }
+    },
+    Value
+    {
+        @Override
+        public double value( Metric metric )
+        {
+            if ( metric instanceof Gauge )
+            {
+                return ( (Gauge) metric ).getValue();
+            }
+            return metric.getSum();
+        }
+    };
+    public abstract double value( Metric metric );
+}
\ No newline at end of file

Added: 
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/Selector.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/Selector.java?rev=732346&view=auto
==============================================================================
--- 
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/Selector.java
 (added)
+++ 
commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/Selector.java
 Wed Jan  7 06:09:33 2009
@@ -0,0 +1,85 @@
+/*
+ * 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.reporting;
+
+import org.apache.commons.monitoring.Metric;
+import org.apache.commons.monitoring.Repository;
+
+/**
+ * Retrieve a monitored data based on a path expression.
+ *
+ * @author <a href="mailto:nico...@apache.org";>Nicolas De Loof</a>
+ */
+public class Selector
+{
+    public final static String DEFAULT_SEPARATOR = "/";
+
+    private MetricData value;
+
+    private String role;
+
+    private String name;
+
+    private String category;
+
+    private String subsystem;
+
+    /**
+     * Build a selector for the specified Path
+     *
+     * @param path
+     */
+    public Selector( String path )
+    {
+        this( path, DEFAULT_SEPARATOR );
+    }
+
+    /**
+     * Build a selector for the specified Path
+     *
+     * @param path
+     * @param separator path separator
+     */
+    public Selector( String path, String separator )
+    {
+        super();
+        String[] tokens = path.split( separator );
+        int i = tokens.length;
+        if ( i < 3 )
+        {
+            throw new IllegalArgumentException( "Monitored data path must have 
at least 3 tokens" );
+        }
+        if ( i > 5 )
+        {
+            throw new IllegalArgumentException( "Monitored data path must have 
at most 5 tokens" );
+        }
+        this.value = MetricData.valueOf( tokens[--i] );
+        this.role = tokens[--i];
+        this.name = tokens[--i];
+        this.category = ( i > 0 ? tokens[--i] : null );
+        this.subsystem = ( i > 0 ? tokens[--i] : null );
+    }
+
+    public double getValue( Repository repository )
+    {
+        Metric metric = repository.getMonitor( name, category, subsystem 
).getMetric( role );
+        double d = value.value( metric );
+        return d;
+    }
+
+}


Reply via email to