Search in sources :

Example 51 with Counter

use of com.codahale.metrics.Counter in project lucene-solr by apache.

the class SolrJmxReporterTest method testReloadCore.

@Test
public void testReloadCore() throws Exception {
    Random random = random();
    String scope = SolrMetricTestUtils.getRandomScope(random, true);
    SolrInfoBean.Category category = SolrMetricTestUtils.getRandomCategory(random, true);
    Map<String, Counter> metrics = SolrMetricTestUtils.getRandomMetrics(random, true);
    SolrMetricProducer producer = SolrMetricTestUtils.getProducerOf(metricManager, category, scope, metrics);
    coreMetricManager.registerMetricProducer(scope, producer);
    Set<ObjectInstance> objects = mBeanServer.queryMBeans(null, null);
    assertEquals(metrics.size(), objects.stream().filter(o -> scope.equals(o.getObjectName().getKeyProperty("scope")) && o.getObjectName().getDomain().equals(rootName)).count());
    h.getCoreContainer().reload(h.getCore().getName());
    PluginInfo pluginInfo = createReporterPluginInfo(rootName, true);
    metricManager.loadReporter(coreMetricManager.getRegistryName(), coreMetricManager.getCore().getResourceLoader(), pluginInfo, String.valueOf(coreMetricManager.getCore().hashCode()));
    coreMetricManager.registerMetricProducer(scope, producer);
    objects = mBeanServer.queryMBeans(null, null);
    assertEquals(metrics.size(), objects.stream().filter(o -> scope.equals(o.getObjectName().getKeyProperty("scope")) && rootName.equals(o.getObjectName().getDomain())).count());
}
Also used : Counter(com.codahale.metrics.Counter) Random(java.util.Random) SolrInfoBean(org.apache.solr.core.SolrInfoBean) SolrMetricProducer(org.apache.solr.metrics.SolrMetricProducer) ObjectInstance(javax.management.ObjectInstance) PluginInfo(org.apache.solr.core.PluginInfo) Test(org.junit.Test)

Example 52 with Counter

use of com.codahale.metrics.Counter in project lucene-solr by apache.

the class SolrMetricManagerTest method testSwapRegistries.

@Test
public void testSwapRegistries() throws Exception {
    Random r = random();
    SolrMetricManager metricManager = new SolrMetricManager();
    Map<String, Counter> metrics1 = SolrMetricTestUtils.getRandomMetrics(r, true);
    Map<String, Counter> metrics2 = SolrMetricTestUtils.getRandomMetrics(r, true);
    String fromName = "from-" + TestUtil.randomSimpleString(r, 1, 10);
    String toName = "to-" + TestUtil.randomSimpleString(r, 1, 10);
    // register test metrics
    for (Map.Entry<String, Counter> entry : metrics1.entrySet()) {
        metricManager.register(null, fromName, entry.getValue(), false, entry.getKey(), "metrics1");
    }
    for (Map.Entry<String, Counter> entry : metrics2.entrySet()) {
        metricManager.register(null, toName, entry.getValue(), false, entry.getKey(), "metrics2");
    }
    assertEquals(metrics1.size(), metricManager.registry(fromName).getMetrics().size());
    assertEquals(metrics2.size(), metricManager.registry(toName).getMetrics().size());
    // swap
    metricManager.swapRegistries(fromName, toName);
    // check metrics
    Map<String, Metric> fromMetrics = metricManager.registry(fromName).getMetrics();
    assertEquals(metrics2.size(), fromMetrics.size());
    for (Map.Entry<String, Counter> entry : metrics2.entrySet()) {
        Object value = fromMetrics.get(SolrMetricManager.mkName(entry.getKey(), "metrics2"));
        assertNotNull(value);
        assertEquals(entry.getValue(), value);
    }
    Map<String, Metric> toMetrics = metricManager.registry(toName).getMetrics();
    assertEquals(metrics1.size(), toMetrics.size());
    for (Map.Entry<String, Counter> entry : metrics1.entrySet()) {
        Object value = toMetrics.get(SolrMetricManager.mkName(entry.getKey(), "metrics1"));
        assertNotNull(value);
        assertEquals(entry.getValue(), value);
    }
}
Also used : Counter(com.codahale.metrics.Counter) Random(java.util.Random) Metric(com.codahale.metrics.Metric) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 53 with Counter

use of com.codahale.metrics.Counter in project lucene-solr by apache.

the class SolrMetricManagerTest method testClearMetrics.

@Test
public void testClearMetrics() throws Exception {
    Random r = random();
    SolrMetricManager metricManager = new SolrMetricManager();
    Map<String, Counter> metrics = SolrMetricTestUtils.getRandomMetrics(r, true);
    String registryName = TestUtil.randomSimpleString(r, 1, 10);
    for (Map.Entry<String, Counter> entry : metrics.entrySet()) {
        metricManager.register(null, registryName, entry.getValue(), false, entry.getKey(), "foo", "bar");
    }
    for (Map.Entry<String, Counter> entry : metrics.entrySet()) {
        metricManager.register(null, registryName, entry.getValue(), false, entry.getKey(), "foo", "baz");
    }
    for (Map.Entry<String, Counter> entry : metrics.entrySet()) {
        metricManager.register(null, registryName, entry.getValue(), false, entry.getKey(), "foo");
    }
    assertEquals(metrics.size() * 3, metricManager.registry(registryName).getMetrics().size());
    // clear all metrics with prefix "foo.bar."
    Set<String> removed = metricManager.clearMetrics(registryName, "foo", "bar.");
    assertEquals(metrics.size(), removed.size());
    for (String s : removed) {
        assertTrue(s.startsWith("foo.bar."));
    }
    removed = metricManager.clearMetrics(registryName, "foo", "baz.");
    assertEquals(metrics.size(), removed.size());
    for (String s : removed) {
        assertTrue(s.startsWith("foo.baz."));
    }
    // perhaps surprisingly, this works too - see PrefixFilter docs
    removed = metricManager.clearMetrics(registryName, "fo");
    assertEquals(metrics.size(), removed.size());
    for (String s : removed) {
        assertTrue(s.startsWith("foo."));
    }
}
Also used : Counter(com.codahale.metrics.Counter) Random(java.util.Random) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 54 with Counter

use of com.codahale.metrics.Counter in project lucene-solr by apache.

the class MetricUtils method convertMetric.

/**
   * Convert a single instance of metric into a map or flattened object.
   * @param n metric name
   * @param metric metric instance
   * @param propertyFilter limit what properties of a metric are returned
   * @param skipHistograms discard any {@link Histogram}-s and histogram parts of {@link Timer}-s.
   * @param skipAggregateValues discard internal values of {@link AggregateMetric}-s.
   * @param compact use compact representation for counters and gauges.
   * @param simple use simplified representation for complex metrics - instead of a (name, map)
   *             only the selected (name "." key, value) pairs will be produced.
   * @param consumer consumer that accepts produced objects
   */
static void convertMetric(String n, Metric metric, PropertyFilter propertyFilter, boolean skipHistograms, boolean skipAggregateValues, boolean compact, boolean simple, BiConsumer<String, Object> consumer) {
    if (metric instanceof Counter) {
        Counter counter = (Counter) metric;
        convertCounter(n, counter, propertyFilter, compact, consumer);
    } else if (metric instanceof Gauge) {
        Gauge gauge = (Gauge) metric;
        try {
            convertGauge(n, gauge, propertyFilter, simple, compact, consumer);
        } catch (InternalError ie) {
            if (n.startsWith("memory.") && ie.getMessage().contains("Memory Pool not found")) {
                LOG.warn("Error converting gauge '" + n + "', possible JDK bug: SOLR-10362", ie);
                consumer.accept(n, null);
            } else {
                throw ie;
            }
        }
    } else if (metric instanceof Meter) {
        Meter meter = (Meter) metric;
        convertMeter(n, meter, propertyFilter, simple, consumer);
    } else if (metric instanceof Timer) {
        Timer timer = (Timer) metric;
        convertTimer(n, timer, propertyFilter, skipHistograms, simple, consumer);
    } else if (metric instanceof Histogram) {
        if (!skipHistograms) {
            Histogram histogram = (Histogram) metric;
            convertHistogram(n, histogram, propertyFilter, simple, consumer);
        }
    } else if (metric instanceof AggregateMetric) {
        convertAggregateMetric(n, (AggregateMetric) metric, propertyFilter, skipAggregateValues, simple, consumer);
    }
}
Also used : Histogram(com.codahale.metrics.Histogram) Counter(com.codahale.metrics.Counter) Timer(com.codahale.metrics.Timer) Meter(com.codahale.metrics.Meter) AggregateMetric(org.apache.solr.metrics.AggregateMetric) Gauge(com.codahale.metrics.Gauge)

Example 55 with Counter

use of com.codahale.metrics.Counter in project sling by apache.

the class MetricWebConsolePlugin method addCounterDetails.

private void addCounterDetails(PrintWriter pw, SortedMap<String, Counter> counters) {
    if (counters.isEmpty()) {
        return;
    }
    pw.println("<br>");
    pw.println("<div class='table'>");
    pw.println("<div class='ui-widget-header ui-corner-top buttonGroup'>Counters</div>");
    pw.println("<table class='nicetable' id='data-counters'>");
    pw.println("<thead>");
    pw.println("<tr>");
    pw.println("<th class='header'>Name</th>");
    pw.println("<th class='header'>Count</th>");
    pw.println("</tr>");
    pw.println("</thead>");
    pw.println("<tbody>");
    String rowClass = "odd";
    for (Map.Entry<String, Counter> e : counters.entrySet()) {
        Counter c = e.getValue();
        String name = e.getKey();
        pw.printf("<tr class='%s ui-state-default'>%n", rowClass);
        pw.printf("<td>%s</td>", name);
        pw.printf("<td>%d</td>", c.getCount());
        pw.println("</tr>");
        rowClass = "odd".equals(rowClass) ? "even" : "odd";
    }
    pw.println("</tbody>");
    pw.println("</table>");
    pw.println("</div>");
}
Also used : Counter(com.codahale.metrics.Counter) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SortedMap(java.util.SortedMap)

Aggregations

Counter (com.codahale.metrics.Counter)55 Test (org.junit.Test)24 Map (java.util.Map)15 Timer (com.codahale.metrics.Timer)14 MetricRegistry (com.codahale.metrics.MetricRegistry)13 HashMap (java.util.HashMap)11 Gauge (com.codahale.metrics.Gauge)10 Histogram (com.codahale.metrics.Histogram)10 Metric (com.codahale.metrics.Metric)9 Meter (com.codahale.metrics.Meter)8 Random (java.util.Random)8 SolrInfoBean (org.apache.solr.core.SolrInfoBean)6 SortedMap (java.util.SortedMap)5 IOException (java.io.IOException)4 TreeMap (java.util.TreeMap)4 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)4 SolrMetricManager (org.apache.solr.metrics.SolrMetricManager)4 Description (com.google.gerrit.metrics.Description)3 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3