Search in sources :

Example 1 with AggregateMetric

use of org.apache.solr.metrics.AggregateMetric in project lucene-solr by apache.

the class MetricUtilsTest method testMetrics.

@Test
public void testMetrics() throws Exception {
    MetricRegistry registry = new MetricRegistry();
    Counter counter = registry.counter("counter");
    counter.inc();
    Timer timer = registry.timer("timer");
    Timer.Context ctx = timer.time();
    Thread.sleep(150);
    ctx.stop();
    Meter meter = registry.meter("meter");
    meter.mark();
    Histogram histogram = registry.histogram("histogram");
    histogram.update(10);
    AggregateMetric am = new AggregateMetric();
    registry.register("aggregate", am);
    am.set("foo", 10);
    am.set("bar", 1);
    am.set("bar", 2);
    Gauge<String> gauge = () -> "foobar";
    registry.register("gauge", gauge);
    Gauge<Long> error = () -> {
        throw new InternalError("Memory Pool not found error");
    };
    registry.register("memory.expected.error", error);
    MetricUtils.toMaps(registry, Collections.singletonList(MetricFilter.ALL), MetricFilter.ALL, MetricUtils.PropertyFilter.ALL, false, false, false, false, (k, o) -> {
        Map v = (Map) o;
        if (k.startsWith("counter")) {
            assertEquals(1L, v.get("count"));
        } else if (k.startsWith("gauge")) {
            assertEquals("foobar", v.get("value"));
        } else if (k.startsWith("timer")) {
            assertEquals(1L, v.get("count"));
            assertTrue(((Number) v.get("min_ms")).intValue() > 100);
        } else if (k.startsWith("meter")) {
            assertEquals(1L, v.get("count"));
        } else if (k.startsWith("histogram")) {
            assertEquals(1L, v.get("count"));
        } else if (k.startsWith("aggregate")) {
            assertEquals(2, v.get("count"));
            Map<String, Object> values = (Map<String, Object>) v.get("values");
            assertNotNull(values);
            assertEquals(2, values.size());
            Map<String, Object> update = (Map<String, Object>) values.get("foo");
            assertEquals(10, update.get("value"));
            assertEquals(1, update.get("updateCount"));
            update = (Map<String, Object>) values.get("bar");
            assertEquals(2, update.get("value"));
            assertEquals(2, update.get("updateCount"));
        } else if (k.startsWith("memory.expected.error")) {
            assertNull(v);
        }
    });
    // test compact format
    MetricUtils.toMaps(registry, Collections.singletonList(MetricFilter.ALL), MetricFilter.ALL, MetricUtils.PropertyFilter.ALL, false, false, true, false, (k, o) -> {
        if (k.startsWith("counter")) {
            assertTrue(o instanceof Long);
            assertEquals(1L, o);
        } else if (k.startsWith("gauge")) {
            assertTrue(o instanceof String);
            assertEquals("foobar", o);
        } else if (k.startsWith("timer")) {
            assertTrue(o instanceof Map);
            Map v = (Map) o;
            assertEquals(1L, v.get("count"));
            assertTrue(((Number) v.get("min_ms")).intValue() > 100);
        } else if (k.startsWith("meter")) {
            assertTrue(o instanceof Map);
            Map v = (Map) o;
            assertEquals(1L, v.get("count"));
        } else if (k.startsWith("histogram")) {
            assertTrue(o instanceof Map);
            Map v = (Map) o;
            assertEquals(1L, v.get("count"));
        } else if (k.startsWith("aggregate")) {
            assertTrue(o instanceof Map);
            Map v = (Map) o;
            assertEquals(2, v.get("count"));
            Map<String, Object> values = (Map<String, Object>) v.get("values");
            assertNotNull(values);
            assertEquals(2, values.size());
            Map<String, Object> update = (Map<String, Object>) values.get("foo");
            assertEquals(10, update.get("value"));
            assertEquals(1, update.get("updateCount"));
            update = (Map<String, Object>) values.get("bar");
            assertEquals(2, update.get("value"));
            assertEquals(2, update.get("updateCount"));
        } else if (k.startsWith("memory.expected.error")) {
            assertNull(o);
        } else {
            Map v = (Map) o;
            assertEquals(1L, v.get("count"));
        }
    });
}
Also used : Histogram(com.codahale.metrics.Histogram) Meter(com.codahale.metrics.Meter) MetricRegistry(com.codahale.metrics.MetricRegistry) AggregateMetric(org.apache.solr.metrics.AggregateMetric) Counter(com.codahale.metrics.Counter) Timer(com.codahale.metrics.Timer) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with AggregateMetric

use of org.apache.solr.metrics.AggregateMetric in project lucene-solr by apache.

the class SolrCloudReportersTest method testExplicitConfiguration.

@Test
public void testExplicitConfiguration() throws Exception {
    String solrXml = IOUtils.toString(SolrCloudReportersTest.class.getResourceAsStream("/solr/solr-solrreporter.xml"), "UTF-8");
    configureCluster(2).withSolrXml(solrXml).configure();
    cluster.uploadConfigSet(Paths.get(TEST_PATH().toString(), "configsets", "minimal", "conf"), "test");
    System.out.println("ZK: " + cluster.getZkServer().getZkAddress());
    CollectionAdminRequest.createCollection("test_collection", "test", 2, 2).setMaxShardsPerNode(4).process(cluster.getSolrClient());
    waitForState("Expected test_collection with 2 shards and 2 replicas", "test_collection", clusterShape(2, 2));
    Thread.sleep(15000);
    cluster.getJettySolrRunners().forEach(jetty -> {
        CoreContainer cc = jetty.getCoreContainer();
        for (String name : cc.getLoadedCoreNames()) {
            SolrCore core = cc.getCore(name);
            try {
                String registryName = core.getCoreMetricManager().getRegistryName();
                String leaderRegistryName = core.getCoreMetricManager().getLeaderRegistryName();
                String coreName = core.getName();
                String collectionName = core.getCoreDescriptor().getCollectionName();
                String coreNodeName = core.getCoreDescriptor().getCloudDescriptor().getCoreNodeName();
                String replicaName = coreName.substring(coreName.indexOf("_replica_") + 1);
                String shardId = core.getCoreDescriptor().getCloudDescriptor().getShardId();
                assertEquals("solr.core." + collectionName + "." + shardId + "." + replicaName, registryName);
                assertEquals("solr.collection." + collectionName + "." + shardId + ".leader", leaderRegistryName);
            } finally {
                if (core != null) {
                    core.close();
                }
            }
        }
        SolrMetricManager metricManager = cc.getMetricManager();
        Map<String, SolrMetricReporter> reporters = metricManager.getReporters("solr.cluster");
        assertEquals(reporters.toString(), 1, reporters.size());
        SolrMetricReporter reporter = reporters.get("test");
        assertNotNull(reporter);
        assertTrue(reporter.toString(), reporter instanceof SolrClusterReporter);
        SolrClusterReporter sor = (SolrClusterReporter) reporter;
        assertEquals(5, sor.getPeriod());
        for (String registryName : metricManager.registryNames(".*\\.shard[0-9]\\.replica.*")) {
            reporters = metricManager.getReporters(registryName);
            assertEquals(reporters.toString(), 1 + jmxReporter, reporters.size());
            reporter = null;
            for (String name : reporters.keySet()) {
                if (name.startsWith("test")) {
                    reporter = reporters.get(name);
                }
            }
            assertNotNull(reporter);
            assertTrue(reporter.toString(), reporter instanceof SolrShardReporter);
            SolrShardReporter srr = (SolrShardReporter) reporter;
            assertEquals(5, srr.getPeriod());
        }
        for (String registryName : metricManager.registryNames(".*\\.leader")) {
            leaderRegistries++;
            reporters = metricManager.getReporters(registryName);
            assertEquals(reporters.toString(), 0, reporters.size());
            Map<String, Metric> metrics = metricManager.registry(registryName).getMetrics();
            String key = "QUERY./select.requests";
            assertTrue(key, metrics.containsKey(key));
            assertTrue(key, metrics.get(key) instanceof AggregateMetric);
            key = "UPDATE./update/json.requests";
            assertTrue(key, metrics.containsKey(key));
            assertTrue(key, metrics.get(key) instanceof AggregateMetric);
        }
        if (metricManager.registryNames().contains("solr.cluster")) {
            clusterRegistries++;
            Map<String, Metric> metrics = metricManager.registry("solr.cluster").getMetrics();
            String key = "jvm.memory.heap.init";
            assertTrue(key, metrics.containsKey(key));
            assertTrue(key, metrics.get(key) instanceof AggregateMetric);
            key = "leader.test_collection.shard1.UPDATE./update/json.requests.max";
            assertTrue(key, metrics.containsKey(key));
            assertTrue(key, metrics.get(key) instanceof AggregateMetric);
        }
    });
    assertEquals("leaderRegistries", 2, leaderRegistries);
    assertEquals("clusterRegistries", 1, clusterRegistries);
}
Also used : CoreContainer(org.apache.solr.core.CoreContainer) SolrCore(org.apache.solr.core.SolrCore) SolrMetricManager(org.apache.solr.metrics.SolrMetricManager) AggregateMetric(org.apache.solr.metrics.AggregateMetric) Metric(com.codahale.metrics.Metric) AggregateMetric(org.apache.solr.metrics.AggregateMetric) SolrMetricReporter(org.apache.solr.metrics.SolrMetricReporter) Test(org.junit.Test)

Example 3 with AggregateMetric

use of org.apache.solr.metrics.AggregateMetric in project lucene-solr by apache.

the class SolrShardReporterTest method test.

@Test
public void test() throws Exception {
    waitForRecoveriesToFinish("control_collection", jettys.get(0).getCoreContainer().getZkController().getZkStateReader(), false);
    waitForRecoveriesToFinish("collection1", jettys.get(0).getCoreContainer().getZkController().getZkStateReader(), false);
    printLayout();
    // wait for at least two reports
    Thread.sleep(10000);
    ClusterState state = jettys.get(0).getCoreContainer().getZkController().getClusterState();
    for (JettySolrRunner jetty : jettys) {
        CoreContainer cc = jetty.getCoreContainer();
        SolrMetricManager metricManager = cc.getMetricManager();
        for (final String coreName : cc.getLoadedCoreNames()) {
            CoreDescriptor cd = cc.getCoreDescriptor(coreName);
            if (cd.getCloudDescriptor() == null) {
                // not a cloud collection
                continue;
            }
            CloudDescriptor cloudDesc = cd.getCloudDescriptor();
            DocCollection docCollection = state.getCollection(cloudDesc.getCollectionName());
            String replicaName = SolrCoreMetricManager.parseReplicaName(cloudDesc.getCollectionName(), coreName);
            if (replicaName == null) {
                replicaName = cloudDesc.getCoreNodeName();
            }
            String registryName = SolrCoreMetricManager.createRegistryName(true, cloudDesc.getCollectionName(), cloudDesc.getShardId(), replicaName, null);
            String leaderRegistryName = SolrCoreMetricManager.createLeaderRegistryName(true, cloudDesc.getCollectionName(), cloudDesc.getShardId());
            boolean leader = cloudDesc.isLeader();
            Slice slice = docCollection.getSlice(cloudDesc.getShardId());
            int numReplicas = slice.getReplicas().size();
            if (leader) {
                assertTrue(metricManager.registryNames() + " doesn't contain " + leaderRegistryName, metricManager.registryNames().contains(leaderRegistryName));
                Map<String, Metric> metrics = metricManager.registry(leaderRegistryName).getMetrics();
                metrics.forEach((k, v) -> {
                    assertTrue("Unexpected type of " + k + ": " + v.getClass().getName() + ", " + v, v instanceof AggregateMetric);
                    AggregateMetric am = (AggregateMetric) v;
                    if (!k.startsWith("REPLICATION.peerSync")) {
                        assertEquals(coreName + "::" + registryName + "::" + k + ": " + am.toString(), numReplicas, am.size());
                    }
                });
            } else {
                assertFalse(metricManager.registryNames() + " contains " + leaderRegistryName + " but it's not a leader!", metricManager.registryNames().contains(leaderRegistryName));
                Map<String, Metric> metrics = metricManager.registry(leaderRegistryName).getMetrics();
                metrics.forEach((k, v) -> {
                    assertTrue("Unexpected type of " + k + ": " + v.getClass().getName() + ", " + v, v instanceof AggregateMetric);
                    AggregateMetric am = (AggregateMetric) v;
                    if (!k.startsWith("REPLICATION.peerSync")) {
                        assertEquals(coreName + "::" + registryName + "::" + k + ": " + am.toString(), 1, am.size());
                    }
                });
            }
            assertTrue(metricManager.registryNames() + " doesn't contain " + registryName, metricManager.registryNames().contains(registryName));
        }
    }
    SolrMetricManager metricManager = controlJetty.getCoreContainer().getMetricManager();
    assertTrue(metricManager.registryNames().contains("solr.cluster"));
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) CoreDescriptor(org.apache.solr.core.CoreDescriptor) AggregateMetric(org.apache.solr.metrics.AggregateMetric) CloudDescriptor(org.apache.solr.cloud.CloudDescriptor) CoreContainer(org.apache.solr.core.CoreContainer) Slice(org.apache.solr.common.cloud.Slice) SolrMetricManager(org.apache.solr.metrics.SolrMetricManager) AggregateMetric(org.apache.solr.metrics.AggregateMetric) Metric(com.codahale.metrics.Metric) DocCollection(org.apache.solr.common.cloud.DocCollection) Test(org.junit.Test)

Example 4 with AggregateMetric

use of org.apache.solr.metrics.AggregateMetric in project lucene-solr by apache.

the class MetricUtils method toMaps.

/**
   * Convert selected metrics to maps or to flattened objects.
   * @param registry source of metrics
   * @param shouldMatchFilters metrics must match any of these filters
   * @param mustMatchFilter metrics must match this filter
   * @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
   */
public static void toMaps(MetricRegistry registry, List<MetricFilter> shouldMatchFilters, MetricFilter mustMatchFilter, PropertyFilter propertyFilter, boolean skipHistograms, boolean skipAggregateValues, boolean compact, boolean simple, BiConsumer<String, Object> consumer) {
    final Map<String, Metric> metrics = registry.getMetrics();
    final SortedSet<String> names = registry.getNames();
    names.stream().filter(s -> shouldMatchFilters.stream().anyMatch(metricFilter -> metricFilter.matches(s, metrics.get(s)))).filter(s -> mustMatchFilter.matches(s, metrics.get(s))).forEach(n -> {
        Metric metric = metrics.get(n);
        convertMetric(n, metric, propertyFilter, skipHistograms, skipAggregateValues, compact, simple, consumer);
    });
}
Also used : Histogram(com.codahale.metrics.Histogram) SortedSet(java.util.SortedSet) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Introspector(java.beans.Introspector) Meter(com.codahale.metrics.Meter) InstrumentedExecutorService(com.codahale.metrics.InstrumentedExecutorService) BeanInfo(java.beans.BeanInfo) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) Counter(com.codahale.metrics.Counter) PlatformManagedObject(java.lang.management.PlatformManagedObject) MetricFilter(com.codahale.metrics.MetricFilter) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) ExecutorService(java.util.concurrent.ExecutorService) AggregateMetric(org.apache.solr.metrics.AggregateMetric) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) Metric(com.codahale.metrics.Metric) Snapshot(com.codahale.metrics.Snapshot) NamedList(org.apache.solr.common.util.NamedList) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) List(java.util.List) PropertyDescriptor(java.beans.PropertyDescriptor) SolrInfoBean(org.apache.solr.core.SolrInfoBean) Timer(com.codahale.metrics.Timer) Gauge(com.codahale.metrics.Gauge) Collections(java.util.Collections) SolrInputDocument(org.apache.solr.common.SolrInputDocument) AggregateMetric(org.apache.solr.metrics.AggregateMetric) Metric(com.codahale.metrics.Metric)

Example 5 with AggregateMetric

use of org.apache.solr.metrics.AggregateMetric 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)

Aggregations

AggregateMetric (org.apache.solr.metrics.AggregateMetric)6 Metric (com.codahale.metrics.Metric)4 Counter (com.codahale.metrics.Counter)3 Histogram (com.codahale.metrics.Histogram)3 Meter (com.codahale.metrics.Meter)3 Timer (com.codahale.metrics.Timer)3 Test (org.junit.Test)3 Gauge (com.codahale.metrics.Gauge)2 MetricRegistry (com.codahale.metrics.MetricRegistry)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CoreContainer (org.apache.solr.core.CoreContainer)2 SolrMetricManager (org.apache.solr.metrics.SolrMetricManager)2 InstrumentedExecutorService (com.codahale.metrics.InstrumentedExecutorService)1 MetricFilter (com.codahale.metrics.MetricFilter)1 Snapshot (com.codahale.metrics.Snapshot)1 BeanInfo (java.beans.BeanInfo)1 IntrospectionException (java.beans.IntrospectionException)1 Introspector (java.beans.Introspector)1 PropertyDescriptor (java.beans.PropertyDescriptor)1