usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sfeld...@apache.org
Subject git commit: add metrics factory, cod hale.metrics to pom, add property
Date Fri, 18 Jul 2014 20:42:45 GMT
Repository: incubator-usergrid
Updated Branches:
  refs/heads/add-metrics df6b84a76 -> bb600018a


add metrics factory, cod hale.metrics to pom, add property


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/bb600018
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/bb600018
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/bb600018

Branch: refs/heads/add-metrics
Commit: bb600018a4f35fa6672ac21200dc951280dc9790
Parents: df6b84a
Author: Shawn Feldman <sfeldman@apache.org>
Authored: Fri Jul 18 14:41:58 2014 -0600
Committer: Shawn Feldman <sfeldman@apache.org>
Committed: Fri Jul 18 14:41:58 2014 -0600

----------------------------------------------------------------------
 .../main/resources/usergrid-default.properties  |   3 +
 stack/core/pom.xml                              |  11 ++
 .../apache/usergrid/metrics/MetricsFactory.java | 121 +++++++++++++++++++
 .../main/resources/usergrid-core-context.xml    |   2 +
 stack/pom.xml                                   |   2 +-
 5 files changed, 138 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/bb600018/stack/config/src/main/resources/usergrid-default.properties
----------------------------------------------------------------------
diff --git a/stack/config/src/main/resources/usergrid-default.properties b/stack/config/src/main/resources/usergrid-default.properties
index 8f7cb44..ab3ffc8 100644
--- a/stack/config/src/main/resources/usergrid-default.properties
+++ b/stack/config/src/main/resources/usergrid-default.properties
@@ -318,6 +318,9 @@ usergrid.management.email.user-pin=\
     <p>${pin}</p>
 
 
+# graphite server
+usergrid.metrics.graphite.host=badhost
+
 ###############################################################################
 #
 # Redirect urls to use instead of internal JSPs.  Not all of these should be

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/bb600018/stack/core/pom.xml
----------------------------------------------------------------------
diff --git a/stack/core/pom.xml b/stack/core/pom.xml
index dad4a6b..c6748aa 100644
--- a/stack/core/pom.xml
+++ b/stack/core/pom.xml
@@ -559,5 +559,16 @@
       <artifactId>spring-test</artifactId>
       <scope>test</scope>
     </dependency>
+
+    <dependency>
+      <groupId>com.codahale.metrics</groupId>
+      <artifactId>metrics-core</artifactId>
+      <version>${metrics.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>com.codahale.metrics</groupId>
+      <artifactId>metrics-graphite</artifactId>
+      <version>${metrics.version}</version>
+    </dependency>
   </dependencies>
 </project>

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/bb600018/stack/core/src/main/java/org/apache/usergrid/metrics/MetricsFactory.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/metrics/MetricsFactory.java b/stack/core/src/main/java/org/apache/usergrid/metrics/MetricsFactory.java
new file mode 100644
index 0000000..c5dbf1e
--- /dev/null
+++ b/stack/core/src/main/java/org/apache/usergrid/metrics/MetricsFactory.java
@@ -0,0 +1,121 @@
+/*
+ * 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.usergrid.metrics;
+import com.codahale.metrics.*;
+import com.codahale.metrics.graphite.Graphite;
+import com.codahale.metrics.graphite.GraphiteReporter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.net.InetSocketAddress;
+import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Singleton class to manage metrics.
+ */
+@Component("metricsFactory")
+public class MetricsFactory {
+    @Autowired
+    private Properties properties;
+    public MetricRegistry registry;
+    private GraphiteReporter graphiteReporter;
+    private JmxReporter jmxReporter;
+    private ConcurrentHashMap<String,Metric> hashMap;
+    private static final Logger LOG = LoggerFactory.getLogger(MetricsFactory.class);
+
+    public MetricsFactory() {
+
+    }
+
+    @PostConstruct
+    void init() {
+        properties = new Properties();
+        try {
+            properties.load(Thread.currentThread()
+                    .getContextClassLoader()
+                    .getResourceAsStream("usergrid.properties"));
+        } catch (Exception e) {
+            LOG.error("Could not load props","");
+        }
+        registry = new MetricRegistry();
+        String badHost = "badhost";
+        String metricsHost = properties.getProperty("usergrid.metrics.graphite.host", badHost);
+        Graphite graphite = new Graphite(new InetSocketAddress(metricsHost, 2003));
+        graphiteReporter = GraphiteReporter.forRegistry(registry)
+                .prefixedWith("notifications")
+                .convertRatesTo(TimeUnit.SECONDS)
+                .convertDurationsTo(TimeUnit.MILLISECONDS)
+                .filter(MetricFilter.ALL)
+                .build(graphite);
+        if(metricsHost!=badHost) {
+            graphiteReporter.start(30, TimeUnit.SECONDS);
+        }else {
+            LOG.warn("MetricsService:Logger not started.");
+            graphiteReporter.stop();
+        }
+        hashMap = new ConcurrentHashMap<String, Metric>();
+
+        jmxReporter = JmxReporter.forRegistry(registry).build();
+        jmxReporter.start();
+    }
+
+    public MetricRegistry getRegistry() {
+        return registry;
+    }
+
+    public Timer getTimer(Class<?> klass, String name) {
+        return getMetric(Timer.class, klass, name);
+    }
+
+    public Histogram getHistogram(Class<?> klass, String name) {
+        return getMetric(Histogram.class, klass, name);
+    }
+
+    public Counter getCounter(Class<?> klass, String name) {
+        return getMetric(Counter.class, klass, name);
+    }
+
+    public Meter getMeter(Class<?> klass, String name) {
+        return getMetric(Meter.class, klass, name);
+    }
+
+    private <T> T getMetric(Class<T> metricClass, Class<?> klass, String
name) {
+        String key = metricClass.getName() + klass.getName() + name;
+        Metric metric = hashMap.get(key);
+        if (metric == null) {
+            if (metricClass == Histogram.class) {
+                metric = this.getRegistry().histogram(MetricRegistry.name(klass, name));
+            }
+            if (metricClass == Timer.class) {
+                metric = this.getRegistry().timer(MetricRegistry.name(klass, name));
+            }
+            if (metricClass == Meter.class) {
+                metric = this.getRegistry().meter(MetricRegistry.name(klass, name));
+            }
+            if (metricClass == Counter.class) {
+                metric = this.getRegistry().counter(MetricRegistry.name(klass, name));
+            }
+            hashMap.put(key, metric);
+        }
+        return (T) metric;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/bb600018/stack/core/src/main/resources/usergrid-core-context.xml
----------------------------------------------------------------------
diff --git a/stack/core/src/main/resources/usergrid-core-context.xml b/stack/core/src/main/resources/usergrid-core-context.xml
index 7e4e202..0c66cf2 100644
--- a/stack/core/src/main/resources/usergrid-core-context.xml
+++ b/stack/core/src/main/resources/usergrid-core-context.xml
@@ -193,6 +193,8 @@
 
     <bean id="jobFactory" class="org.apache.usergrid.batch.UsergridJobFactory" />
 
+    <bean id="metricsFactory" class="org.apache.usergrid.metrics.MetricsFactory" scope="singleton"/>
+
     <!-- scan all job classes -->
     <context:component-scan base-package="org.apache.usergrid.batch.job" />
     <context:annotation-config />

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/bb600018/stack/pom.xml
----------------------------------------------------------------------
diff --git a/stack/pom.xml b/stack/pom.xml
index 3a37577..b58eafb 100644
--- a/stack/pom.xml
+++ b/stack/pom.xml
@@ -113,7 +113,7 @@
     <tomcat-version>7.0.42</tomcat-version>
     <antlr.version>3.4</antlr.version>
     <tika.version>1.4</tika.version>
-
+    <metrics.version>3.0.0</metrics.version>
   </properties>
 
   <licenses>


Mime
View raw message