mlbiscoc commented on code in PR #2405: URL: https://github.com/apache/solr/pull/2405#discussion_r1610557889
########## solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrCoreMetric.java: ########## @@ -0,0 +1,54 @@ +/* + * 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 com.codahale.metrics.Metric; +import java.util.HashMap; +import java.util.Map; +import org.apache.solr.metrics.prometheus.SolrPrometheusCoreExporter; + +/** + * Base class is a wrapper to categorize and export {@link com.codahale.metrics.Metric} to {@link + * io.prometheus.metrics.model.snapshots.DataPointSnapshot} and register to a {@link + * SolrPrometheusCoreExporter}. {@link com.codahale.metrics.MetricRegistry} does not support tags + * unlike prometheus. Metrics registered to the registry need to be parsed out from the metric name + * to be exported to {@link io.prometheus.metrics.model.snapshots.DataPointSnapshot} + */ +public abstract class SolrCoreMetric { + public Metric dropwizardMetric; + public String coreName; + public String metricName; + public Map<String, String> labels = new HashMap<>(); + + public SolrCoreMetric( + Metric dropwizardMetric, String coreName, String metricName, boolean cloudMode) { + this.dropwizardMetric = dropwizardMetric; + this.coreName = coreName; + this.metricName = metricName; + labels.put("core", coreName); + if (cloudMode) { + String[] coreNameParsed = coreName.split("_"); + labels.put("collection", coreNameParsed[1]); Review Comment: Good catch. I created a new function with this regex pattern `"^core_(.*)_(shard[0-9]+)_(replica_n[0-9]+)"` for core names looking like `core_test_collection_shard1_replica_n1`. Also created a test under `SolrCoreMetricTest` to avoid regressions if for some reason the pattern for core name changes. ########## 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( Review Comment: That's a fair point. Moved it to the PrometheusResponseWriter so it's known it's only used for this. ########## 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"; + + public SolrPrometheusCoreExporter(String coreName, boolean cloudMode) { + super(); + this.coreName = coreName; + this.cloudMode = cloudMode; + } + + /** + * Export {@link Metric} to {@link io.prometheus.metrics.model.snapshots.MetricSnapshot} and + * registers the Snapshot + * + * @param dropwizardMetric the {@link Meter} to be exported + * @param metricName Dropwizard metric name + */ + @Override + public void exportDropwizardMetric(Metric dropwizardMetric, String metricName) { + SolrCoreMetric solrCoreMetric = categorizeCoreMetric(dropwizardMetric, metricName); + solrCoreMetric.parseLabels().toPrometheus(this); + } + + private SolrCoreMetric categorizeCoreMetric(Metric dropwizardMetric, String metricName) { + String metricCategory = metricName.split("\\.")[0]; Review Comment: Personally more a fan of split instead of many `startsWith`. I'll keep as if unless someone feels strongly otherwise. -- 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