Skip to content

Commit

Permalink
add test; compatibility mapping of field names
Browse files Browse the repository at this point in the history
  • Loading branch information
magibney committed Sep 8, 2023
1 parent 254cba4 commit 823fb23
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -76,14 +77,27 @@ public final class PrometheusMetricsServlet extends BaseSolrServlet {
new CoresMetricsApiCaller(),
new NodeCacheMetricsApiCaller()));

private static final Map<String, PrometheusMetricType> cacheMetricTypes =
private final Map<String, PrometheusMetricType> cacheMetricTypes =
Map.of(
"bytesUsed", PrometheusMetricType.GAUGE,
"lookups", PrometheusMetricType.COUNTER,
"hits", PrometheusMetricType.COUNTER,
"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 @@ -147,11 +161,9 @@ static void getSharedCacheMetrics(

static class NodeCacheMetricsApiCaller extends MetricsApiCaller {
private static final String PREFIX = "CACHE.nodeLevelCache/";

NodeCacheMetricsApiCaller() {
super(
"node",
PREFIX,
"");
super("node", PREFIX, "");
}

@Override
Expand All @@ -164,15 +176,21 @@ protected void handle(List<PrometheusMetric> results, JsonNode metrics) throws I
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) {
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),
Locale.ROOT,
"%s %s for cache %s",
fieldName,
type.getDisplayName(),
cacheName),
next.getValue().numberValue()));
}
}
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);
}
}

0 comments on commit 823fb23

Please sign in to comment.