Apache Lucene open-source search software
SOLR-9911: Add a way to filter metrics by prefix in the MetricsHandler API
Shalin Shekhar Mangar0452cb8c969f
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 1ad63594a83..82fd7ae603e 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -186,9 +186,13 @@ New Features* SOLR-9805: Use metrics-jvm library to instrument jvm internals such as GC, memory usage and others. (shalin)-* SOLR-9812: Added a new /admin/metrics API to return all metrics collected by Solr via API. API supports two- optional parameters 'group' (all,jvm,jetty,http,node,core) and 'type' (all,counter,timer,gauge,histogram) both- of which are multi-valued. Example: http://localhost:8983/solr/admin/metrics?group=jvm,jetty&type=counter+* SOLR-9812: SOLR-9911: Added a new /admin/metrics API to return all metrics collected by Solr via API.+ API supports three optional parameters:+ * 'group' (all,jvm,jetty,http,node,core),+ * 'type' (all,counter,timer,gauge,histogram) both of which are multi-valued+ * 'prefix' that filters the return metrics+ Example: http://localhost:8983/solr/admin/metrics?group=jvm,jetty&type=counter+ Example: http://localhost:8983/solr/admin/metrics?group=jvm&prefix=buffers(shalin)* SOLR-9884: Add version to segments handler output (Steven Bower via Erick Erickson)diff --git a/solr/core/src/java/org/apache/solr/handler/admin/MetricsHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/MetricsHandler.javaindex 78b20452268..428a72b5178 100644--- a/solr/core/src/java/org/apache/solr/handler/admin/MetricsHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/admin/MetricsHandler.java@@ -71,6 +71,7 @@ public class MetricsHandler extends RequestHandlerBase implements PermissionNamethrow new SolrException(SolrException.ErrorCode.INVALID_STATE, "Core container instance not initialized");}+ MetricFilter mustMatchFilter = parseMustMatchFilter(req);List<MetricType> metricTypes = parseMetricTypes(req);List<MetricFilter> metricFilters = metricTypes.stream().map(MetricType::asMetricFilter).collect(Collectors.toList());List<Group> requestedGroups = parseGroups(req);@@ -86,16 +87,27 @@ public class MetricsHandler extends RequestHandlerBase implements PermissionNamecoreRegistryName = core.getCoreMetricManager().getRegistryName();}MetricRegistry registry = metricManager.registry(coreRegistryName);- response.add(coreRegistryName, MetricUtils.toNamedList(registry, metricFilters));+ response.add(coreRegistryName, MetricUtils.toNamedList(registry, metricFilters, mustMatchFilter));});} else {MetricRegistry registry = metricManager.registry(registryName);- response.add(registryName, MetricUtils.toNamedList(registry, metricFilters));+ response.add(registryName, MetricUtils.toNamedList(registry, metricFilters, mustMatchFilter));}}rsp.getValues().add("metrics", response);}+ private MetricFilter parseMustMatchFilter(SolrQueryRequest req) {+ String prefix = req.getParams().get("prefix");+ MetricFilter mustMatchFilter;+ if (prefix != null) {+ mustMatchFilter = new SolrMetricManager.PrefixFilter(prefix.trim());+ } else {+ mustMatchFilter = MetricFilter.ALL;+ }+ return mustMatchFilter;+ }+private List<Group> parseGroups(SolrQueryRequest req) {String[] groupStr = req.getParams().getParams("group");List<String> groups = Collections.emptyList();diff --git a/solr/core/src/java/org/apache/solr/util/stats/MetricUtils.java b/solr/core/src/java/org/apache/solr/util/stats/MetricUtils.javaindex 4a83c869448..0d386ae74ef 100644--- a/solr/core/src/java/org/apache/solr/util/stats/MetricUtils.java+++ b/solr/core/src/java/org/apache/solr/util/stats/MetricUtils.java@@ -72,14 +72,21 @@ public class MetricUtils {* are converted to NamedList which match at least one of the given MetricFilter instances.** @param registry the {@link MetricRegistry} to be converted to NamedList- * @param metricFilters a list of {@link MetricFilter} instances+ * @param shouldMatchFilters a list of {@link MetricFilter} instances.+ * A metric must match <em>any one</em> of the filters from this list to be+ * included in the output+ * @param mustMatchFilter a {@link MetricFilter}.+ * A metric <em>must</em> match this filter to be included in the output.* @return a {@link NamedList}*/- public static NamedList toNamedList(MetricRegistry registry, List<MetricFilter> metricFilters) {+ public static NamedList toNamedList(MetricRegistry registry, List<MetricFilter> shouldMatchFilters, MetricFilter mustMatchFilter) {NamedList response = new NamedList();Map<String, Metric> metrics = registry.getMetrics();SortedSet<String> names = registry.getNames();- names.stream().filter(s -> metricFilters.stream().anyMatch(metricFilter -> metricFilter.matches(s, metrics.get(s)))).forEach(n -> {+ 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);if (metric instanceof Counter) {Counter counter = (Counter) metric;diff --git a/solr/core/src/test/org/apache/solr/handler/admin/MetricsHandlerTest.java b/solr/core/src/test/org/apache/solr/handler/admin/MetricsHandlerTest.javaindex e15778d5c0c..67bf0e3bc96 100644--- a/solr/core/src/test/org/apache/solr/handler/admin/MetricsHandlerTest.java+++ b/solr/core/src/test/org/apache/solr/handler/admin/MetricsHandlerTest.java@@ -91,7 +91,40 @@ public class MetricsHandlerTest extends SolrTestCaseJ4 {assertNotNull(values.get("metrics"));values = (NamedList) values.get("metrics");assertEquals(1, values.size());- assertNotNull(values.get("solr.node"));+ values = (NamedList) values.get("solr.node");+ assertNotNull(values);assertNull(values.get("QUERYHANDLER./admin/authorization.errors")); // this is a timer node++ resp = new SolrQueryResponse();+ handler.handleRequestBody(req(CommonParams.QT, "/admin/metrics", CommonParams.WT, "json", "prefix", "cores"), resp);+ values = resp.getValues();+ assertNotNull(values.get("metrics"));+ values = (NamedList) values.get("metrics");+ assertEquals(5, values.size());+ assertEquals(0, ((NamedList)values.get("solr.jvm")).size());+ assertEquals(0, ((NamedList)values.get("solr.http")).size());+ assertEquals(0, ((NamedList)values.get("solr.jetty")).size());+ assertEquals(0, ((NamedList)values.get("solr.core.collection1")).size());+ assertEquals(3, ((NamedList)values.get("solr.node")).size());+ assertNotNull(values.get("solr.node"));+ values = (NamedList) values.get("solr.node");+ assertNotNull(values.get("cores.lazy")); // this is a gauge node++ resp = new SolrQueryResponse();+ handler.handleRequestBody(req(CommonParams.QT, "/admin/metrics", CommonParams.WT, "json", "group", "jvm", "prefix", "cores"), resp);+ values = resp.getValues();+ assertNotNull(values.get("metrics"));+ values = (NamedList) values.get("metrics");+ assertEquals(1, values.size());+ assertEquals(0, ((NamedList)values.get("solr.jvm")).size());+ assertNull(values.get("solr.node"));++ resp = new SolrQueryResponse();+ handler.handleRequestBody(req(CommonParams.QT, "/admin/metrics", CommonParams.WT, "json", "group", "node", "type", "timer", "prefix", "cores"), resp);+ values = resp.getValues();+ assertNotNull(values.get("metrics"));+ values = (NamedList) values.get("metrics");+ assertEquals(1, values.size());+ assertEquals(0, ((NamedList)values.get("solr.node")).size());}}