You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ab...@apache.org on 2016/11/30 11:44:29 UTC

lucene-solr:feature/metrics: SOLR-4735 Simplify method names. Allow removing multiple metrics with a prefix.

Repository: lucene-solr
Updated Branches:
  refs/heads/feature/metrics 7cedc4acb -> 14ff79175


SOLR-4735 Simplify method names. Allow removing multiple metrics with a prefix.


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/14ff7917
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/14ff7917
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/14ff7917

Branch: refs/heads/feature/metrics
Commit: 14ff79175f524b38ec85f0542753e28dc8b1b2c6
Parents: 7cedc4a
Author: Andrzej Bialecki <ab...@apache.org>
Authored: Wed Nov 30 12:43:46 2016 +0100
Committer: Andrzej Bialecki <ab...@apache.org>
Committed: Wed Nov 30 12:43:46 2016 +0100

----------------------------------------------------------------------
 .../apache/solr/handler/RequestHandlerBase.java | 12 +--
 .../apache/solr/metrics/SolrMetricManager.java  | 96 ++++++++++++++++----
 .../solr/metrics/reporters/SolrJmxReporter.java |  2 +-
 .../solr/metrics/SolrCoreMetricManagerTest.java |  2 +-
 .../solr/metrics/SolrMetricTestUtils.java       |  2 +-
 5 files changed, 88 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/14ff7917/solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java b/solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java
index 2f00767..7310c32 100644
--- a/solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java
+++ b/solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java
@@ -134,12 +134,12 @@ public abstract class RequestHandlerBase implements SolrRequestHandler, SolrInfo
 
   @Override
   public Collection<String> initializeMetrics(String registryName, String scope) {
-    numErrors = SolrMetricManager.getOrCreateMeter(registryName, "errors", getCategory().toString(), scope);
-    numServerErrors = SolrMetricManager.getOrCreateMeter(registryName, "serverErrors", getCategory().toString(), scope);
-    numClientErrors = SolrMetricManager.getOrCreateMeter(registryName, "clientErrors", getCategory().toString(), scope);
-    numTimeouts = SolrMetricManager.getOrCreateMeter(registryName, "timeouts", getCategory().toString(), scope);
-    requests = SolrMetricManager.getOrCreateCounter(registryName, "requests", getCategory().toString(), scope);
-    requestTimes = SolrMetricManager.getOrCreateTimer(registryName, "requestTimes", getCategory().toString(), scope);
+    numErrors = SolrMetricManager.meter(registryName, "errors", getCategory().toString(), scope);
+    numServerErrors = SolrMetricManager.meter(registryName, "serverErrors", getCategory().toString(), scope);
+    numClientErrors = SolrMetricManager.meter(registryName, "clientErrors", getCategory().toString(), scope);
+    numTimeouts = SolrMetricManager.meter(registryName, "timeouts", getCategory().toString(), scope);
+    requests = SolrMetricManager.counter(registryName, "requests", getCategory().toString(), scope);
+    requestTimes = SolrMetricManager.timer(registryName, "requestTimes", getCategory().toString(), scope);
     return Arrays.asList("errors", "serverErrors", "clientErrors", "timeouts", "requestTimes", "requests");
   }
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/14ff7917/solr/core/src/java/org/apache/solr/metrics/SolrMetricManager.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/metrics/SolrMetricManager.java b/solr/core/src/java/org/apache/solr/metrics/SolrMetricManager.java
index cfd135e..3a7fddf 100644
--- a/solr/core/src/java/org/apache/solr/metrics/SolrMetricManager.java
+++ b/solr/core/src/java/org/apache/solr/metrics/SolrMetricManager.java
@@ -16,15 +16,19 @@
  */
 package org.apache.solr.metrics;
 
+import java.util.Collections;
+import java.util.HashSet;
 import java.util.Set;
 
 import com.codahale.metrics.Counter;
 import com.codahale.metrics.Histogram;
 import com.codahale.metrics.Meter;
+import com.codahale.metrics.Metric;
 import com.codahale.metrics.MetricFilter;
 import com.codahale.metrics.MetricRegistry;
 import com.codahale.metrics.SharedMetricRegistries;
 import com.codahale.metrics.Timer;
+import com.google.common.base.Preconditions;
 
 /**
  *
@@ -36,6 +40,53 @@ public class SolrMetricManager {
   // don't create instances of this class
   private SolrMetricManager() { }
 
+  /**
+   * An implementation of {@link MetricFilter} that selects metrics
+   * with names that start with a prefix.
+   */
+  public static class PrefixFilter implements MetricFilter {
+    private final String prefix;
+    private final Set<String> matched = new HashSet<>();
+
+    /**
+     * Create a filter that uses the provided prefix.
+     * @param prefix prefix to use, must not be null. If empty then any
+     *               name will match.
+     */
+    public PrefixFilter(String prefix) {
+      Preconditions.checkNotNull(prefix);
+      this.prefix = prefix;
+    }
+
+    @Override
+    public boolean matches(String name, Metric metric) {
+      if (prefix.isEmpty()) {
+        matched.add(name);
+        return true;
+      }
+      if (name.startsWith(prefix)) {
+        matched.add(name);
+        return true;
+      } else {
+        return false;
+      }
+    }
+
+    /**
+     * Return the set of names that matched this filter.
+     * @return
+     */
+    public Set<String> getMatched() {
+      return Collections.unmodifiableSet(matched);
+    }
+
+    /**
+     * Clear the set of names that matched.
+     */
+    public void reset() {
+      matched.clear();
+    }
+  }
 
   /**
    * Return a set of existing registry names.
@@ -49,7 +100,7 @@ public class SolrMetricManager {
    * @param registry name of the registry
    * @return existing or newly created registry
    */
-  public static MetricRegistry registryFor(String registry) {
+  public static MetricRegistry registry(String registry) {
     return SharedMetricRegistries.getOrCreate(overridableRegistryName(registry));
   }
 
@@ -57,19 +108,30 @@ public class SolrMetricManager {
    * Remove all metrics from a specified registry.
    * @param registry registry name
    */
-  public static void clearRegistryFor(String registry) {
-    registryFor(registry).removeMatching(MetricFilter.ALL);
+  public static void clearRegistry(String registry) {
+    registry(registry).removeMatching(MetricFilter.ALL);
   }
 
   /**
-   * Remove a single specific metric from a named registry
+   * Remove some metrics from a named registry
    * @param registry registry name
-   * @param metricName metric name, either final name or a fully-qualified name
-   *                   using dotted notation
-   * @param metricPath (optional) additional top-most metric name path elements
+   * @param metricPath (optional) top-most metric name path elements. If empty then
+   *        this is equivalent to calling {@link #clearRegistry(String)},
+   *        otherwise non-empty elements will be joined using dotted notation
+   *        to form a fully-qualified prefix. Metrics with names that start
+   *        with the prefix will be removed.
+   * @return set of metrics names that have been removed.
    */
-  public static void clearMetric(String registry, String metricName, String... metricPath) {
-    registryFor(registry).remove(mkName(metricName, metricPath));
+  public static Set<String> clearMetrics(String registry, String... metricPath) {
+    PrefixFilter filter;
+    if (metricPath == null || metricPath.length == 0) {
+      filter = new PrefixFilter("");
+    } else {
+      String prefix = MetricRegistry.name("", metricPath);
+      filter = new PrefixFilter(prefix);
+    }
+    registry(registry).removeMatching(filter);
+    return filter.getMatched();
   }
 
   /**
@@ -80,8 +142,8 @@ public class SolrMetricManager {
    * @param metricPath (optional) additional top-most metric name path elements
    * @return existing or a newly created {@link Meter}
    */
-  public static Meter getOrCreateMeter(String registry, String metricName, String... metricPath) {
-    return registryFor(registry).meter(mkName(metricName, metricPath));
+  public static Meter meter(String registry, String metricName, String... metricPath) {
+    return registry(registry).meter(mkName(metricName, metricPath));
   }
 
   /**
@@ -92,8 +154,8 @@ public class SolrMetricManager {
    * @param metricPath (optional) additional top-most metric name path elements
    * @return existing or a newly created {@link Timer}
    */
-  public static Timer getOrCreateTimer(String registry, String metricName, String... metricPath) {
-    return registryFor(registry).timer(mkName(metricName, metricPath));
+  public static Timer timer(String registry, String metricName, String... metricPath) {
+    return registry(registry).timer(mkName(metricName, metricPath));
   }
 
   /**
@@ -104,8 +166,8 @@ public class SolrMetricManager {
    * @param metricPath (optional) additional top-most metric name path elements
    * @return existing or a newly created {@link Counter}
    */
-  public static Counter getOrCreateCounter(String registry, String metricName, String... metricPath) {
-    return registryFor(registry).counter(mkName(metricName, metricPath));
+  public static Counter counter(String registry, String metricName, String... metricPath) {
+    return registry(registry).counter(mkName(metricName, metricPath));
   }
 
   /**
@@ -116,8 +178,8 @@ public class SolrMetricManager {
    * @param metricPath (optional) additional top-most metric name path elements
    * @return existing or a newly created {@link Histogram}
    */
-  public static Histogram getOrCreateHistogram(String registry, String metricName, String... metricPath) {
-    return registryFor(registry).histogram(mkName(metricName, metricPath));
+  public static Histogram histogram(String registry, String metricName, String... metricPath) {
+    return registry(registry).histogram(mkName(metricName, metricPath));
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/14ff7917/solr/core/src/java/org/apache/solr/metrics/reporters/SolrJmxReporter.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/metrics/reporters/SolrJmxReporter.java b/solr/core/src/java/org/apache/solr/metrics/reporters/SolrJmxReporter.java
index 97db12a..cb8bc8a 100644
--- a/solr/core/src/java/org/apache/solr/metrics/reporters/SolrJmxReporter.java
+++ b/solr/core/src/java/org/apache/solr/metrics/reporters/SolrJmxReporter.java
@@ -96,7 +96,7 @@ public class SolrJmxReporter extends SolrMetricReporter {
 
     JmxObjectNameFactory jmxObjectNameFactory = new JmxObjectNameFactory(registryName);
 
-    reporter = JmxReporter.forRegistry(SolrMetricManager.registryFor(registryName))
+    reporter = JmxReporter.forRegistry(SolrMetricManager.registry(registryName))
                           .registerWith(mBeanServer)
                           .inDomain(domain)
                           .createsObjectNamesWith(jmxObjectNameFactory)

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/14ff7917/solr/core/src/test/org/apache/solr/metrics/SolrCoreMetricManagerTest.java
----------------------------------------------------------------------
diff --git a/solr/core/src/test/org/apache/solr/metrics/SolrCoreMetricManagerTest.java b/solr/core/src/test/org/apache/solr/metrics/SolrCoreMetricManagerTest.java
index d046916..80b41b8 100644
--- a/solr/core/src/test/org/apache/solr/metrics/SolrCoreMetricManagerTest.java
+++ b/solr/core/src/test/org/apache/solr/metrics/SolrCoreMetricManagerTest.java
@@ -131,7 +131,7 @@ public class SolrCoreMetricManagerTest extends SolrTestCaseJ4 {
       return;
     }
     String filter = "." + scope + ".";
-    MetricRegistry registry = SolrMetricManager.registryFor(metricManager.getRegistryName());
+    MetricRegistry registry = SolrMetricManager.registry(metricManager.getRegistryName());
     assertEquals(newMetrics.size(), registry.getMetrics().
         keySet().stream().filter(s -> s.contains(filter)).count());
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/14ff7917/solr/core/src/test/org/apache/solr/metrics/SolrMetricTestUtils.java
----------------------------------------------------------------------
diff --git a/solr/core/src/test/org/apache/solr/metrics/SolrMetricTestUtils.java b/solr/core/src/test/org/apache/solr/metrics/SolrMetricTestUtils.java
index 2c1c452..f8ca6d7 100644
--- a/solr/core/src/test/org/apache/solr/metrics/SolrMetricTestUtils.java
+++ b/solr/core/src/test/org/apache/solr/metrics/SolrMetricTestUtils.java
@@ -87,7 +87,7 @@ public final class SolrMetricTestUtils {
           return Collections.emptyList();
         }
         for (Map.Entry<String, Counter> entry : metrics.entrySet()) {
-          SolrMetricManager.getOrCreateCounter(registry, entry.getKey(), category.toString(), scope);
+          SolrMetricManager.counter(registry, entry.getKey(), category.toString(), scope);
         }
         return metrics.keySet();
       }