dsmiley commented on code in PR #2405: URL: https://github.com/apache/solr/pull/2405#discussion_r1607222435
########## 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 = Review Comment: minor: Good use for "var" here ########## 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: I could see adding some resiliency to this such that underscores in the collection name still work ########## 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: minor: define later closer to where used. Maybe right after solrPrometheusCoreExporter is declared. ########## solr/core/src/java/org/apache/solr/metrics/prometheus/core/SolrCoreHandlerMetric.java: ########## @@ -0,0 +1,89 @@ +/* + * 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.Counter; +import com.codahale.metrics.Gauge; +import com.codahale.metrics.Meter; +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 ADMIN/QUERY/UPDATE/REPLICATION.* */ +public class SolrCoreHandlerMetric extends SolrCoreMetric { + public static final String CORE_REQUESTS_TOTAL = "solr_metrics_core_requests"; + public static final String CORE_REQUESTS_UPDATE_HANDLER = "solr_metrics_core_update_handler"; + public static final String CORE_REQUESTS_TOTAL_TIME = "solr_metrics_core_requests_time"; + public static final String CORE_REQUEST_TIMES = "solr_metrics_core_average_request_time"; + + public SolrCoreHandlerMetric( + Metric dropwizardMetric, String coreName, String metricName, boolean cloudMode) { + super(dropwizardMetric, coreName, metricName, cloudMode); + } + + @Override + public SolrCoreMetric parseLabels() { + String[] parsedMetric = metricName.split("\\."); + String category = parsedMetric[0]; + String handler = parsedMetric[1]; + String type = parsedMetric[2]; + labels.put("category", category); + labels.put("type", type); + labels.put("handler", handler); + return this; Review Comment: minor: recommend concisely doing each on one line ########## 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: See org.apache.solr.core.SolrInfoBean.Category ... which maybe we should use directly as a type ########## solr/core/build.gradle: ########## @@ -153,6 +153,13 @@ dependencies { implementation 'org.apache.logging.log4j:log4j-core' runtimeOnly 'org.apache.logging.log4j:log4j-slf4j2-impl' + // Prometheus client Review Comment: Why "client"? Pretend someone reading this doesn't know about Prometheus :-). Like maybe just say "For the PrometheusResponseWriter" ########## 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<>(); Review Comment: Would LinkedHashMap bring some logical consistency to the output even though it doesn't matter? ########## 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: I'm seeing this line *a lot*. Maybe a method on the base class will do? Named getLabels() ########## solr/core/src/java/org/apache/solr/response/PrometheusResponseWriter.java: ########## @@ -0,0 +1,46 @@ +/* + * 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.response; + +import io.prometheus.metrics.expositionformats.PrometheusTextFormatWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Map; +import org.apache.solr.common.util.NamedList; +import org.apache.solr.metrics.prometheus.SolrPrometheusCoreExporter; +import org.apache.solr.request.SolrQueryRequest; + +@SuppressWarnings(value = "unchecked") +public class PrometheusResponseWriter extends RawResponseWriter { + @Override + public void write(OutputStream out, SolrQueryRequest request, SolrQueryResponse response) + throws IOException { + NamedList<Object> prometheusRegistries = + (NamedList<Object>) response.getValues().get("metrics"); + Map<String, Object> registryMap = prometheusRegistries.asShallowMap(); Review Comment: Don't need to call asShallowMap; can iterate directly as NamedList implements Iterable. Then wont' need your needless try-catch below either. ########## 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) { Review Comment: these var names should be changed everywhere so that it's not a "registry" ########## solr/core/src/java/org/apache/solr/response/PrometheusResponseWriter.java: ########## @@ -0,0 +1,46 @@ +/* + * 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.response; + +import io.prometheus.metrics.expositionformats.PrometheusTextFormatWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Map; +import org.apache.solr.common.util.NamedList; +import org.apache.solr.metrics.prometheus.SolrPrometheusCoreExporter; +import org.apache.solr.request.SolrQueryRequest; + +@SuppressWarnings(value = "unchecked") +public class PrometheusResponseWriter extends RawResponseWriter { + @Override + public void write(OutputStream out, SolrQueryRequest request, SolrQueryResponse response) + throws IOException { + NamedList<Object> prometheusRegistries = + (NamedList<Object>) response.getValues().get("metrics"); + Map<String, Object> registryMap = prometheusRegistries.asShallowMap(); + PrometheusTextFormatWriter prometheusTextFormatWriter = new PrometheusTextFormatWriter(false); Review Comment: minor: "var" would cut down verbosity ########## 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: It's debatable that this belongs here; it doesn't seem utility-like. It's called by one place, MetricsHandler, so that would be a decent spot. Alternatively, given its specific-ness to Prometheus, perhaps the PrometheusResponseWriter is a better home. -- 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