Skip to content

Commit

Permalink
get node-level cache metrics into Prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
magibney committed Sep 8, 2023
1 parent c238c08 commit 254cba4
Showing 1 changed file with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -72,9 +73,10 @@ public final class PrometheusMetricsServlet extends BaseSolrServlet {
new OsMetricsApiCaller(),
new ThreadMetricsApiCaller(),
new StatusCodeMetricsApiCaller(),
new CoresMetricsApiCaller()));
new CoresMetricsApiCaller(),
new NodeCacheMetricsApiCaller()));

private final Map<String, PrometheusMetricType> cacheMetricTypes =
private static final Map<String, PrometheusMetricType> cacheMetricTypes =
Map.of(
"bytesUsed", PrometheusMetricType.GAUGE,
"lookups", PrometheusMetricType.COUNTER,
Expand Down Expand Up @@ -143,6 +145,41 @@ static void getSharedCacheMetrics(
}
}

static class NodeCacheMetricsApiCaller extends MetricsApiCaller {
private static final String PREFIX = "CACHE.nodeLevelCache/";
NodeCacheMetricsApiCaller() {
super(
"node",
PREFIX,
"");
}

@Override
protected void handle(List<PrometheusMetric> results, JsonNode metrics) throws IOException {
JsonNode parent = metrics.path("solr.node");
Iterator<Map.Entry<String, JsonNode>> caches = parent.fields();
while (caches.hasNext()) {
Map.Entry<String, JsonNode> cacheEntry = caches.next();
String cacheName = cacheEntry.getKey().substring(PREFIX.length()).toLowerCase(Locale.ROOT);
Iterator<Map.Entry<String, JsonNode>> fields = cacheEntry.getValue().fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> next = fields.next();
String fieldName = next.getKey();
PrometheusMetricType type = cacheMetricTypes.get(fieldName);
if (type != null) {
results.add(
new PrometheusMetric(
String.format(Locale.ROOT, "cache_%s_%s", cacheName, fieldName),
type,
String.format(
Locale.ROOT, "%s %s for cache %s", fieldName, type.getDisplayName(), cacheName),
next.getValue().numberValue()));
}
}
}
}
}

static class GarbageCollectorMetricsApiCaller extends MetricsApiCaller {

GarbageCollectorMetricsApiCaller() {
Expand Down

0 comments on commit 254cba4

Please sign in to comment.