igiguere commented on code in PR #4057: URL: https://github.com/apache/solr/pull/4057#discussion_r2714525190
########## solr/core/src/java/org/apache/solr/handler/admin/api/GetMetrics.java: ########## @@ -0,0 +1,159 @@ +/* + * 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.handler.admin.api; + +import io.prometheus.metrics.model.snapshots.MetricSnapshot; +import io.prometheus.metrics.model.snapshots.MetricSnapshots; +import jakarta.inject.Inject; +import jakarta.ws.rs.WebApplicationException; +import jakarta.ws.rs.core.StreamingOutput; +import java.io.IOException; +import java.io.OutputStream; +import java.lang.invoke.MethodHandles; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.SortedMap; +import org.apache.solr.client.api.endpoint.MetricsApi; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.params.CommonParams; +import org.apache.solr.common.params.SolrParams; +import org.apache.solr.core.CoreContainer; +import org.apache.solr.handler.admin.AdminHandlersProxy; +import org.apache.solr.jersey.PermissionName; +import org.apache.solr.metrics.MetricsUtil; +import org.apache.solr.metrics.SolrMetricManager; +import org.apache.solr.metrics.otel.FilterablePrometheusMetricReader; +import org.apache.solr.request.SolrQueryRequest; +import org.apache.solr.response.PrometheusResponseWriter; +import org.apache.solr.response.SolrQueryResponse; +import org.apache.solr.security.PermissionNameProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * V2 API implementation to fetch metrics gathered by Solr. + * + * <p>This API is analogous to the v1 /admin/metrics endpoint. + */ +public class GetMetrics extends AdminAPIBase implements MetricsApi { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + private final SolrMetricManager metricManager; + private final boolean enabled; + + @Inject + public GetMetrics( + CoreContainer coreContainer, + SolrQueryRequest solrQueryRequest, + SolrQueryResponse solrQueryResponse) { + super(coreContainer, solrQueryRequest, solrQueryResponse); + this.metricManager = coreContainer.getMetricManager(); + this.enabled = coreContainer.getConfig().getMetricsConfig().isEnabled(); + } + + @Override + @PermissionName(PermissionNameProvider.Name.METRICS_READ_PERM) + public StreamingOutput getMetrics() { + + validateRequest(); + + if (proxyToNodes()) { + return null; + } + + SolrParams params = solrQueryRequest.getParams(); + + Set<String> metricNames = MetricsUtil.readParamsAsSet(params, MetricsUtil.METRIC_NAME_PARAM); + SortedMap<String, Set<String>> labelFilters = MetricsUtil.labelFilters(params); + + return doGetMetrics(metricNames, labelFilters); + } + + private void validateRequest() { + + if (!enabled) { + throw new SolrException( + SolrException.ErrorCode.INVALID_STATE, "Metrics collection is disabled"); + } + + if (metricManager == null) { + throw new SolrException( + SolrException.ErrorCode.INVALID_STATE, "SolrMetricManager instance not initialized"); + } + + SolrParams params = solrQueryRequest.getParams(); Review Comment: @mlbiscoc : I found the Prometheus and OpenMetrics MediaTypes in class PrometheusResponseWriter. That was probably you ;) Thanks. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
