mlbiscoc commented on code in PR #2405: URL: https://github.com/apache/solr/pull/2405#discussion_r1610556518
########## solr/core/src/java/org/apache/solr/util/stats/MetricUtils.java: ########## @@ -165,6 +169,71 @@ public static void toSolrInputDocuments( }); } + /** + * Provides a representation of the given Dropwizard metric registry as {@link + * SolrPrometheusCoreExporter}-s. Only those metrics are converted which match at least one of the + * given MetricFilter instances. + * + * @param registry the {@link MetricRegistry} to be converted + * @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. + * @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 consumer consumer that accepts produced {@link SolrPrometheusCoreExporter}-s + */ + public static void toPrometheusRegistry( + MetricRegistry registry, + String registryName, + List<MetricFilter> shouldMatchFilters, + MetricFilter mustMatchFilter, + Predicate<CharSequence> propertyFilter, + boolean skipHistograms, + boolean skipAggregateValues, + boolean compact, + Consumer<SolrPrometheusCoreExporter> consumer) { + String coreName; + boolean cloudMode = false; + Map<String, Metric> dropwizardMetrics = registry.getMetrics(); + String[] rawParsedRegistry = registryName.split("\\."); + List<String> parsedRegistry = new ArrayList<>(Arrays.asList(rawParsedRegistry)); + + if (parsedRegistry.size() == 3) { + coreName = parsedRegistry.get(2); + } else if (parsedRegistry.size() == 5) { + coreName = parsedRegistry.stream().skip(1).collect(Collectors.joining("_")); + cloudMode = true; + } else { + coreName = registryName; + } + + SolrPrometheusCoreExporter solrPrometheusCoreExporter = + new SolrPrometheusCoreExporter(coreName, cloudMode); + + toMaps( + registry, + shouldMatchFilters, + mustMatchFilter, + propertyFilter, + skipHistograms, + skipAggregateValues, + compact, + false, + (metricName, metric) -> { + try { + Metric dropwizardMetric = dropwizardMetrics.get(metricName); Review Comment: A bit confused where you would prefer this. The metric is retrieved within the scope of the `BiConsumer` from `toMap`. I can't move it where `SolrPrometheusCoreExporter` is declared because it is out of scope of this block and the metrics are looped through the registry from `toMaps`. ########## solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrCoreSearcherMetric.java: ########## @@ -0,0 +1,70 @@ +/* + * 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.solr.metrics.prometheus.core; + +import static org.apache.solr.metrics.prometheus.core.SolrCoreCacheMetric.CORE_CACHE_SEARCHER_METRICS; + +import com.codahale.metrics.Counter; +import com.codahale.metrics.Gauge; +import com.codahale.metrics.Metric; +import com.codahale.metrics.Timer; +import io.prometheus.metrics.model.snapshots.Labels; +import java.util.ArrayList; +import org.apache.solr.metrics.prometheus.SolrPrometheusCoreExporter; + +/** Dropwizard metrics of name SEARCHER.* */ +public class SolrCoreSearcherMetric extends SolrCoreMetric { + public static final String CORE_SEARCHER_METRICS = "solr_metrics_core_searcher_documents"; + public static final String CORE_SEARCHER_TIMES = "solr_metrics_core_average_searcher_warmup_time"; + + public SolrCoreSearcherMetric( + Metric dropwizardMetric, String coreName, String metricName, boolean cloudMode) { + super(dropwizardMetric, coreName, metricName, cloudMode); + } + + @Override + public SolrCoreMetric parseLabels() { + String[] parsedMetric = metricName.split("\\."); + if (!(dropwizardMetric instanceof Counter)) { + String type = parsedMetric[2]; + labels.put("type", type); + } + return this; + } + + @Override + public void toPrometheus(SolrPrometheusCoreExporter solrPrometheusCoreRegistry) { + if (dropwizardMetric instanceof Gauge) { + if (metricName.endsWith("liveDocsCache")) { + solrPrometheusCoreRegistry.exportGauge( + CORE_CACHE_SEARCHER_METRICS, + (Gauge<?>) dropwizardMetric, + Labels.of(new ArrayList<>(labels.keySet()), new ArrayList<>(labels.values()))); Review Comment: Addressed with a `getLabels()` and I agree, it is much cleaner. ########## solr/core/src/java/org/apache/solr/metrics/prometheus/SolrPrometheusCoreExporter.java: ########## @@ -0,0 +1,104 @@ +/* + * 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.solr.metrics.prometheus; + +import com.codahale.metrics.Meter; +import com.codahale.metrics.Metric; +import org.apache.solr.metrics.prometheus.core.SolrCoreCacheMetric; +import org.apache.solr.metrics.prometheus.core.SolrCoreHandlerMetric; +import org.apache.solr.metrics.prometheus.core.SolrCoreHighlighterMetric; +import org.apache.solr.metrics.prometheus.core.SolrCoreIndexMetric; +import org.apache.solr.metrics.prometheus.core.SolrCoreMetric; +import org.apache.solr.metrics.prometheus.core.SolrCoreNoOpMetric; +import org.apache.solr.metrics.prometheus.core.SolrCoreSearcherMetric; +import org.apache.solr.metrics.prometheus.core.SolrCoreTlogMetric; + +/** + * This class maintains a {@link io.prometheus.metrics.model.snapshots.MetricSnapshot}s exported + * from solr.core {@link com.codahale.metrics.MetricRegistry} + */ +public class SolrPrometheusCoreExporter extends SolrPrometheusExporter { + public final String coreName; + public final boolean cloudMode; + public static final String ADMIN = "ADMIN"; + public static final String QUERY = "QUERY"; + public static final String UPDATE = "UPDATE"; + public static final String REPLICATION = "REPLICATION"; + public static final String TLOG = "TLOG"; + public static final String CACHE = "CACHE"; + public static final String SEARCHER = "SEARCHER"; + public static final String HIGHLIGHTER = "HIGHLIGHTER"; + public static final String INDEX = "INDEX"; + public static final String CORE = "CORE"; + Review Comment: I created `PrometheusExporterInfo` that holds all these constant strings. Let me know what you think. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For additional commands, e-mail: issues-h...@solr.apache.org