Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get node-level cache metrics into Prometheus #140

Open
wants to merge 2 commits into
base: fs/branch_9x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
import java.lang.invoke.MethodHandles;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
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,7 +74,8 @@ public final class PrometheusMetricsServlet extends BaseSolrServlet {
new OsMetricsApiCaller(),
new ThreadMetricsApiCaller(),
new StatusCodeMetricsApiCaller(),
new CoresMetricsApiCaller()));
new CoresMetricsApiCaller(),
new NodeCacheMetricsApiCaller()));

private final Map<String, PrometheusMetricType> cacheMetricTypes =
Map.of(
Expand All @@ -82,6 +85,19 @@ public final class PrometheusMetricsServlet extends BaseSolrServlet {
"puts", PrometheusMetricType.COUNTER,
"evictions", PrometheusMetricType.COUNTER);

/**
* node-level caches use slightly different terminology for some fields, so we map to old
* terminology for consistency.
*/
private static final Map<String, Map.Entry<String, PrometheusMetricType>>
nodeLevelCacheMetricTypes =
Map.of(
"ramBytesUsed", new SimpleImmutableEntry<>("bytesUsed", PrometheusMetricType.GAUGE),
"lookups", new SimpleImmutableEntry<>("lookups", PrometheusMetricType.COUNTER),
"hits", new SimpleImmutableEntry<>("hits", PrometheusMetricType.COUNTER),
"inserts", new SimpleImmutableEntry<>("puts", PrometheusMetricType.COUNTER),
"evictions", new SimpleImmutableEntry<>("evictions", PrometheusMetricType.COUNTER));

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, UnavailableException {
Expand Down Expand Up @@ -143,6 +159,45 @@ 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();
Map.Entry<String, PrometheusMetricType> typeEntry =
nodeLevelCacheMetricTypes.get(next.getKey());
if (typeEntry != null) {
String fieldName = typeEntry.getKey();
PrometheusMetricType type = typeEntry.getValue();
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
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,48 @@ public void testCoresMetricsApiCallerMissingIndex() throws Exception {
+ "deletes_by_query 66\n";
assertMetricsApiCaller(new PrometheusMetricsServlet.CoresMetricsApiCaller(), json, 25, output);
}

@Test
public void testNodeCacheMetricsApiCaller() throws Exception {
String json =
"{\n"
+ " \"responseHeader\":{\n"
+ " \"status\":0,\n"
+ " \"QTime\":25},\n"
+ " \"metrics\":{\n"
+ " \"solr.node\":{\n"
+ " \"CACHE.nodeLevelCache/myCache\":{\n"
+ " \"lookups\":2,\n"
+ " \"hits\":1,\n"
+ " \"hitratio\":0.5,\n"
+ " \"inserts\":1,\n"
+ " \"evictions\":0,\n"
+ " \"size\":1,\n"
+ " \"warmupTime\":0,\n"
+ " \"ramBytesUsed\":623,\n"
+ " \"maxRamMB\":-1,\n"
+ " \"cumulative_lookups\":2,\n"
+ " \"cumulative_hits\":1,\n"
+ " \"cumulative_hitratio\":0.5,\n"
+ " \"cumulative_inserts\":1,\n"
+ " \"cumulative_evictions\":0}}}}";
String output =
"# HELP cache_mycache_lookups lookups counter for cache mycache\n"
+ "# TYPE cache_mycache_lookups counter\n"
+ "cache_mycache_lookups 2\n"
+ "# HELP cache_mycache_hits hits counter for cache mycache\n"
+ "# TYPE cache_mycache_hits counter\n"
+ "cache_mycache_hits 1\n"
+ "# HELP cache_mycache_puts puts counter for cache mycache\n"
+ "# TYPE cache_mycache_puts counter\n"
+ "cache_mycache_puts 1\n"
+ "# HELP cache_mycache_evictions evictions counter for cache mycache\n"
+ "# TYPE cache_mycache_evictions counter\n"
+ "cache_mycache_evictions 0\n"
+ "# HELP cache_mycache_bytes_used bytesUsed gauge for cache mycache\n"
+ "# TYPE cache_mycache_bytes_used gauge\n"
+ "cache_mycache_bytes_used 623\n";
assertMetricsApiCaller(
new PrometheusMetricsServlet.NodeCacheMetricsApiCaller(), json, 25, output);
}
}
Loading