
org.apache.solr.prometheus.scraper.SolrScraper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solr-prometheus-exporter Show documentation
Show all versions of solr-prometheus-exporter Show documentation
Apache Solr (module: prometheus-exporter)
The newest version!
/*
* 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.prometheus.scraper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.prometheus.client.Collector;
import io.prometheus.client.Counter;
import java.io.Closeable;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.function.Function;
import java.util.stream.Collectors;
import net.thisptr.jackson.jq.JsonQuery;
import net.thisptr.jackson.jq.exception.JsonQueryException;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.impl.Http2SolrClient;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.prometheus.collector.MetricSamples;
import org.apache.solr.prometheus.exporter.MetricsQuery;
import org.apache.solr.prometheus.exporter.SolrExporter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class SolrScraper implements Closeable {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
protected static final String ZK_HOST_LABEL = "zk_host";
protected static final String BASE_URL_LABEL = "base_url";
protected static final String CLUSTER_ID_LABEL = "cluster_id";
private static final Counter scrapeErrorTotal =
Counter.build()
.name("solr_exporter_scrape_error_total")
.help("Number of scrape error.")
.labelNames(ZK_HOST_LABEL, BASE_URL_LABEL, CLUSTER_ID_LABEL)
.register(SolrExporter.defaultRegistry);
protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
protected final String clusterId;
protected final ExecutorService executor;
public abstract Map metricsForAllHosts(MetricsQuery query)
throws IOException;
public abstract Map pingAllCores(MetricsQuery query) throws IOException;
public abstract Map pingAllCollections(MetricsQuery query)
throws IOException;
public abstract MetricSamples search(MetricsQuery query) throws IOException;
public abstract MetricSamples collections(MetricsQuery metricsQuery) throws IOException;
public SolrScraper(ExecutorService executor, String clusterId) {
this.executor = executor;
this.clusterId = clusterId;
}
protected Map sendRequestsInParallel(
Collection items, Function samplesCallable)
throws IOException {
Map result = new HashMap<>(); // sync on this when adding to it below
try {
// invoke each samplesCallable with each item and putting the results in the above "result"
// map.
executor.invokeAll(
items.stream()
.map(
item ->
(Callable)
() -> {
try {
final MetricSamples samples = samplesCallable.apply(item);
synchronized (result) {
result.put(item, samples);
}
} catch (Exception e) {
// do NOT totally fail; just log and move on
log.warn("Error occurred during metrics collection", e);
}
return null; // not used
})
.collect(Collectors.toList()));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
return result;
}
protected MetricSamples request(SolrClient client, MetricsQuery query) throws IOException {
MetricSamples samples = new MetricSamples();
String baseUrlLabelValue = "";
String zkHostLabelValue = "";
if (client instanceof Http2SolrClient) {
baseUrlLabelValue = ((Http2SolrClient) client).getBaseURL();
} else if (client instanceof CloudSolrClient) {
zkHostLabelValue = ((CloudSolrClient) client).getClusterStateProvider().getQuorumHosts();
}
QueryRequest queryRequest = new QueryRequest(query.getParameters());
queryRequest.setPath(query.getPath());
NamedList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy