|
| 1 | +/* |
| 2 | + * |
| 3 | + * * Copyright 2026 New Relic Corporation. All rights reserved. |
| 4 | + * * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + */ |
| 7 | +package com.agent.instrumentation.solr; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.StringTokenizer; |
| 14 | +import java.util.concurrent.ConcurrentHashMap; |
| 15 | + |
| 16 | +public class MetricUtil { |
| 17 | + |
| 18 | + private static List<String> desiredMetrics = new ArrayList<>(); |
| 19 | + |
| 20 | + private static List<String> desiredPaths = new ArrayList<>(); |
| 21 | + |
| 22 | + private static ConcurrentHashMap<String, NRMetric> metrics = new ConcurrentHashMap<>(); |
| 23 | + |
| 24 | + private static final String REGISTRY_PREFIX = "solr.core"; |
| 25 | + |
| 26 | + private static final HashMap<String, String> remaps = new HashMap<>(); |
| 27 | + |
| 28 | + static { |
| 29 | + desiredMetrics.add("filterCache"); |
| 30 | + desiredMetrics.add("queryResultCache"); |
| 31 | + desiredMetrics.add("documentCache"); |
| 32 | + desiredPaths.add("updateHandler"); |
| 33 | + remaps.put("cumulativeAdds", "cumulative_adds"); |
| 34 | + remaps.put("cumulativeDeletesById", "cumulative_deletesById"); |
| 35 | + remaps.put("cumulativeDeletesByQuery", "cumulative_deletesByQuery"); |
| 36 | + remaps.put("cumulativeErrors", "cumulative_errors"); |
| 37 | + } |
| 38 | + |
| 39 | + public static String getRemap(String key) { |
| 40 | + if (remaps.containsKey(key)) { |
| 41 | + return remaps.get(key); |
| 42 | + } |
| 43 | + return key; |
| 44 | + } |
| 45 | + |
| 46 | + public static void addMetric(NRMetric metric) { |
| 47 | + String metricBase = metric.getMetricBase(); |
| 48 | + metrics.put(metricBase, metric); |
| 49 | + } |
| 50 | + |
| 51 | + public static void removeMetric(String registry, String... metricPath) { |
| 52 | + metrics.entrySet() |
| 53 | + .stream() |
| 54 | + .filter(entry -> entry.getValue().registry.equals(registry) && Arrays.stream(metricPath).anyMatch(path -> path.startsWith(entry.getValue().name))) |
| 55 | + .forEach(x -> metrics.remove(x.getKey())); |
| 56 | + } |
| 57 | + |
| 58 | + public static void swapRegistries(String sourceRegistry, String targetRegistry) { |
| 59 | + metrics.entrySet() |
| 60 | + .stream() |
| 61 | + .filter(entry -> entry.getValue().registry.equals(getRegistry(sourceRegistry))) |
| 62 | + .forEach(x -> { |
| 63 | + String currentKey = x.getKey(); |
| 64 | + NRMetric metric = x.getValue(); |
| 65 | + metric.setRegistry(getRegistry(targetRegistry)); |
| 66 | + addMetric(metric); |
| 67 | + metrics.remove(currentKey); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + public static void clearRegistry(String registry) { |
| 72 | + metrics.entrySet() |
| 73 | + .stream() |
| 74 | + .filter(entry -> entry.getValue().registry.equals(registry)) |
| 75 | + .forEach(x -> metrics.remove(x.getKey())); |
| 76 | + } |
| 77 | + |
| 78 | + public static void clearAll() { |
| 79 | + metrics.clear(); |
| 80 | + } |
| 81 | + |
| 82 | + public static String getRegistry(String r) { |
| 83 | + if (r.startsWith(REGISTRY_PREFIX)) { |
| 84 | + return r.substring(REGISTRY_PREFIX.length() + 1); |
| 85 | + } else { |
| 86 | + return r; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public static ConcurrentHashMap<String, NRMetric> getMetrics() { |
| 91 | + return metrics; |
| 92 | + } |
| 93 | + |
| 94 | + public static String getDesired(String metricName, String[] metricPath) { |
| 95 | + if (!isDesired(metricName, metricPath)) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + if (metricName != null && !metricName.isEmpty()) { |
| 99 | + StringTokenizer st = new StringTokenizer(metricName, "."); |
| 100 | + while (st.hasMoreTokens()) { |
| 101 | + String token = st.nextToken(); |
| 102 | + if (desiredMetrics.contains(token)) { |
| 103 | + return token; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + if (metricPath != null) { |
| 108 | + for (int i = 0; i < metricPath.length; i++) { |
| 109 | + if (desiredPaths.contains(metricPath[i])) { |
| 110 | + return metricPath[i]; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + return null; |
| 115 | + } |
| 116 | + |
| 117 | + public static boolean isDesired(String metricName, String[] metricPath) { |
| 118 | + if (metricName != null && !metricName.isEmpty()) { |
| 119 | + StringTokenizer st = new StringTokenizer(metricName, "."); |
| 120 | + while (st.hasMoreTokens()) { |
| 121 | + String token = st.nextToken(); |
| 122 | + if (desiredMetrics.contains(token)) { |
| 123 | + return true; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + if (metricPath != null) { |
| 128 | + for (int i = 0; i < metricPath.length; i++) { |
| 129 | + if (desiredPaths.contains(metricPath[i])) { |
| 130 | + return true; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments