Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.metrics; |
| 2 | |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 3 | import com.codahale.metrics.Counter; |
Ray Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame^] | 4 | import com.codahale.metrics.Gauge; |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 5 | import com.codahale.metrics.Histogram; |
| 6 | import com.codahale.metrics.Meter; |
| 7 | import com.codahale.metrics.Metric; |
Ray Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame^] | 8 | import com.codahale.metrics.MetricFilter; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 9 | import com.codahale.metrics.MetricRegistry; |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 10 | import com.codahale.metrics.Timer; |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 11 | |
Ray Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame^] | 12 | import java.util.Map; |
| 13 | |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 14 | /** |
| 15 | * This class acts a singleton to hold the Metrics registry for ONOS. |
| 16 | */ |
| 17 | public final class OnosMetrics { |
| 18 | |
| 19 | /** |
| 20 | * Hide constructor. The only way to get the registry is through the |
| 21 | * singleton getter. |
| 22 | */ |
| 23 | private OnosMetrics() {} |
| 24 | |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Components that can hold Metrics. This is used as the first part of |
| 27 | * a Metric's name. |
| 28 | */ |
| 29 | public enum MetricsComponents { |
| 30 | /** |
| 31 | * Global scope, not associated with a particular component. |
| 32 | */ |
| 33 | GLOBAL("Global"), |
| 34 | |
| 35 | /** |
| 36 | * Topology component. |
| 37 | */ |
| 38 | TOPOLOGY("Topology"); |
| 39 | |
| 40 | private final String name; |
| 41 | |
| 42 | /** |
| 43 | * Constructor allows specifying an alternate string name. |
| 44 | * |
| 45 | * @param name string for the name of the component |
| 46 | */ |
| 47 | private MetricsComponents(String name) { |
| 48 | this.name = name; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public String toString() { |
| 53 | return name; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Features that can hold Metrics. This is used as the second part of |
| 59 | * a Metric's name. |
| 60 | */ |
| 61 | public enum MetricsFeatures { |
| 62 | /** |
| 63 | * Global scope, not associated with a particular feature. |
| 64 | */ |
| 65 | GLOBAL("Global"), |
| 66 | |
| 67 | /** |
| 68 | * Topology Intents Framework feature. (example) |
| 69 | */ |
| 70 | TOPOLOGY_INTENTS("IntentsFramework"); |
| 71 | |
| 72 | private final String name; |
| 73 | |
| 74 | /** |
| 75 | * Constructor allows specifying an alternate string name. |
| 76 | * |
| 77 | * @param name string for the name of the component |
| 78 | */ |
| 79 | private MetricsFeatures(String name) { |
| 80 | this.name = name; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public String toString() { |
| 85 | return name; |
| 86 | } |
| 87 | } |
| 88 | |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 89 | private static final MetricRegistry METRICS_REGISTRY = new MetricRegistry(); |
| 90 | |
| 91 | /** |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 92 | * Generates a name for a Metric from its component and feature. |
| 93 | * |
| 94 | * @param component component the metric is defined in |
| 95 | * @param feature feature the metric is defined in |
| 96 | * @param metricName local name of the metric |
| 97 | * |
| 98 | * @return full name of the metric |
| 99 | */ |
| 100 | public static String generateName(final MetricsComponents component, |
| 101 | final MetricsFeatures feature, |
| 102 | final String metricName) { |
| 103 | return MetricRegistry.name(component.toString(), |
| 104 | feature.toString(), |
| 105 | metricName); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Creates a Counter metric. |
| 110 | * |
| 111 | * @param component component the Counter is defined in |
| 112 | * @param feature feature the Counter is defined in |
| 113 | * @param metricName local name of the metric |
| 114 | * @return Counter Meteric |
| 115 | */ |
| 116 | public static Counter createCounter(final MetricsComponents component, |
| 117 | final MetricsFeatures feature, |
| 118 | final String metricName) { |
| 119 | final String name = generateName(component, feature, metricName); |
| 120 | return METRICS_REGISTRY.counter(name); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Creates a Histogram metric. |
| 125 | * |
| 126 | * @param component component the Histogram is defined in |
| 127 | * @param feature feature the Histogram is defined in |
| 128 | * @param metricName local name of the metric |
| 129 | * @return Histogram Metric |
| 130 | */ |
| 131 | public static Histogram createHistogram(final MetricsComponents component, |
| 132 | final MetricsFeatures feature, |
| 133 | final String metricName) { |
| 134 | final String name = generateName(component, feature, metricName); |
| 135 | return METRICS_REGISTRY.histogram(name); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Creates a Timer metric. |
| 140 | * |
| 141 | * @param component component the Timer is defined in |
| 142 | * @param feature feature the Timeer is defined in |
| 143 | * @param metricName local name of the metric |
| 144 | * @return Timer Metric |
| 145 | */ |
| 146 | public static Timer createTimer(final MetricsComponents component, |
| 147 | final MetricsFeatures feature, |
| 148 | final String metricName) { |
| 149 | final String name = generateName(component, feature, metricName); |
| 150 | return METRICS_REGISTRY.timer(name); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Creates a Meter metric. |
| 155 | * |
| 156 | * @param component component the Meter is defined in |
| 157 | * @param feature feature the Meter is defined in |
| 158 | * @param metricName local name of the metric |
| 159 | * @return Meter Metric |
| 160 | */ |
| 161 | public static Meter createMeter(final MetricsComponents component, |
| 162 | final MetricsFeatures feature, |
| 163 | final String metricName) { |
| 164 | final String name = generateName(component, feature, metricName); |
| 165 | return METRICS_REGISTRY.meter(name); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Registers an already created Metric. This is used for situation where a |
| 170 | * caller needs to allocate its own Metric, but still register it with the |
| 171 | * system. |
| 172 | * |
| 173 | * @param component component the Metric is defined in |
| 174 | * @param feature feature the metric is defined in |
| 175 | * @param metricName local name of the metric |
| 176 | * @param metric Metric to register |
| 177 | */ |
Ray Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame^] | 178 | public static void registerMetric(final MetricsComponents component, |
| 179 | final MetricsFeatures feature, |
| 180 | final String metricName, |
| 181 | final Metric metric) { |
Ray Milkey | 49d67be | 2014-07-10 13:47:01 -0700 | [diff] [blame] | 182 | final String name = generateName(component, feature, metricName); |
| 183 | METRICS_REGISTRY.register(name, metric); |
| 184 | } |
| 185 | |
| 186 | /** |
Ray Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame^] | 187 | * Fetches the existing Timers. |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 188 | * |
Ray Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame^] | 189 | * @param filter filter to use to select Timers |
| 190 | * @return a map of the Timers that match the filter, with the key as the |
| 191 | * name String to the Timer. |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 192 | */ |
Ray Milkey | 128651a | 2014-07-14 11:24:28 -0700 | [diff] [blame^] | 193 | public static Map<String, Timer> getTimers(final MetricFilter filter) { |
| 194 | return METRICS_REGISTRY.getTimers(filter); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Fetches the existing Gauges. |
| 199 | * |
| 200 | * @param filter filter to use to select Gauges |
| 201 | * @return a map of the Gauges that match the filter, with the key as the |
| 202 | * name String to the Gauge. |
| 203 | */ |
| 204 | @SuppressWarnings("rawtypes") |
| 205 | public static Map<String, Gauge> getGauges(final MetricFilter filter) { |
| 206 | return METRICS_REGISTRY.getGauges(filter); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Fetches the existing Counters. |
| 211 | * |
| 212 | * @param filter filter to use to select Counters |
| 213 | * @return a map of the Counters that match the filter, with the key as the |
| 214 | * name String to the Counter. |
| 215 | */ |
| 216 | public static Map<String, Counter> getCounters(final MetricFilter filter) { |
| 217 | return METRICS_REGISTRY.getCounters(filter); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Fetches the existing Meters. |
| 222 | * |
| 223 | * @param filter filter to use to select Meters |
| 224 | * @return a map of the Meters that match the filter, with the key as the |
| 225 | * name String to the Meter. |
| 226 | */ |
| 227 | public static Map<String, Meter> getMeters(final MetricFilter filter) { |
| 228 | return METRICS_REGISTRY.getMeters(filter); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Fetches the existing Histograms. |
| 233 | * |
| 234 | * @param filter filter to use to select Histograms |
| 235 | * @return a map of the Histograms that match the filter, with the key as the |
| 236 | * name String to the Histogram. |
| 237 | */ |
| 238 | public static Map<String, Histogram> getHistograms(final MetricFilter filter) { |
| 239 | return METRICS_REGISTRY.getHistograms(filter); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Removes all Metrics that match a given filter. |
| 244 | * |
| 245 | * @param filter filter to use to select the Metrics to remove. |
| 246 | */ |
| 247 | public static void removeMatching(final MetricFilter filter) { |
| 248 | METRICS_REGISTRY.removeMatching(filter); |
Ray Milkey | 26921af | 2014-06-30 16:27:40 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |