pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 1 | package org.onlab.metrics; |
| 2 | |
| 3 | import java.util.Map; |
pankaj | 5de8411 | 2014-10-02 15:33:28 -0700 | [diff] [blame] | 4 | import java.util.concurrent.ConcurrentHashMap; |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 5 | import java.util.concurrent.ConcurrentMap; |
| 6 | |
| 7 | import com.codahale.metrics.Counter; |
| 8 | import com.codahale.metrics.Gauge; |
| 9 | import com.codahale.metrics.Histogram; |
| 10 | import com.codahale.metrics.Meter; |
| 11 | import com.codahale.metrics.Metric; |
| 12 | import com.codahale.metrics.MetricFilter; |
| 13 | import com.codahale.metrics.MetricRegistry; |
| 14 | import com.codahale.metrics.Timer; |
| 15 | |
| 16 | /** |
| 17 | * This class holds the Metrics registry for ONOS. |
| 18 | * All metrics (Counter, Histogram, Timer, Meter, Gauge) use a hierarchical |
| 19 | * string-based naming scheme: COMPONENT.FEATURE.NAME. |
| 20 | * Example: "Topology.Counters.TopologyUpdates". |
| 21 | * The COMPONENT and FEATURE names have to be registered in advance before |
| 22 | * a metric can be created. Example: |
| 23 | * <pre> |
| 24 | * <code> |
| 25 | * private final MetricsManager.MetricsComponent COMPONENT = |
| 26 | * MetricsManager.registerComponent("Topology"); |
| 27 | * private final MetricsManager.MetricsFeature FEATURE = |
| 28 | * COMPONENT.registerFeature("Counters"); |
| 29 | * private final Counter counterTopologyUpdates = |
| 30 | * MetricsManager.createCounter(COMPONENT, FEATURE, "TopologyUpdates"); |
| 31 | * </code> |
| 32 | * </pre> |
| 33 | * Gauges are slightly different because they are not created directly in |
| 34 | * this class, but are allocated by the caller and passed in for registration: |
| 35 | * <pre> |
| 36 | * <code> |
| 37 | * private final Gauge<Long> gauge = |
| 38 | * new {@literal Gauge<Long>}() { |
| 39 | * {@literal @}Override |
| 40 | * public Long getValue() { |
| 41 | * return gaugeValue; |
| 42 | * } |
| 43 | * }; |
| 44 | * MetricsManager.registerMetric(COMPONENT, FEATURE, GAUGE_NAME, gauge); |
| 45 | * </code> |
| 46 | * </pre> |
| 47 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 48 | public class MetricsManager implements MetricsService { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Registry to hold the Components defined in the system. |
| 52 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 53 | private ConcurrentMap<String, MetricsComponent> componentsRegistry = |
| 54 | new ConcurrentHashMap<>(); |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Registry for the Metrics objects created in the system. |
| 58 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 59 | private MetricRegistry metricsRegistry = new MetricRegistry(); |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 60 | |
pankaj | 5de8411 | 2014-10-02 15:33:28 -0700 | [diff] [blame] | 61 | /** |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 62 | * Clears the internal state. |
pankaj | 5de8411 | 2014-10-02 15:33:28 -0700 | [diff] [blame] | 63 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 64 | protected void clear() { |
pankaj | 9e34ff2 | 2014-10-06 13:21:03 -0700 | [diff] [blame] | 65 | this.componentsRegistry = new ConcurrentHashMap<>(); |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 66 | this.metricsRegistry = new MetricRegistry(); |
pankaj | f6577b6 | 2014-10-02 16:38:38 -0700 | [diff] [blame] | 67 | } |
| 68 | |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 69 | /** |
| 70 | * Registers a component. |
| 71 | * |
| 72 | * @param name name of the Component to register |
| 73 | * @return MetricsComponent object that can be used to create Metrics. |
| 74 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 75 | @Override |
| 76 | public MetricsComponent registerComponent(final String name) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 77 | MetricsComponent component = componentsRegistry.get(name); |
| 78 | if (component == null) { |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 79 | final MetricsComponent createdComponent = |
| 80 | new MetricsComponent(name); |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 81 | component = componentsRegistry.putIfAbsent(name, createdComponent); |
| 82 | if (component == null) { |
| 83 | component = createdComponent; |
| 84 | } |
| 85 | } |
| 86 | return component; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Generates a name for a Metric from its component and feature. |
| 91 | * |
| 92 | * @param component component the metric is defined in |
| 93 | * @param feature feature the metric is defined in |
| 94 | * @param metricName local name of the metric |
| 95 | * |
| 96 | * @return full name of the metric |
| 97 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 98 | private String generateName(final MetricsComponent component, |
| 99 | final MetricsFeature feature, |
| 100 | final String metricName) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 101 | return MetricRegistry.name(component.getName(), |
| 102 | feature.getName(), |
| 103 | metricName); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Creates a Counter metric. |
| 108 | * |
| 109 | * @param component component the Counter is defined in |
| 110 | * @param feature feature the Counter is defined in |
| 111 | * @param metricName local name of the metric |
| 112 | * @return the created Counter Meteric |
| 113 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 114 | @Override |
| 115 | public Counter createCounter(final MetricsComponent component, |
| 116 | final MetricsFeature feature, |
| 117 | final String metricName) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 118 | final String name = generateName(component, feature, metricName); |
| 119 | return metricsRegistry.counter(name); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Creates a Histogram metric. |
| 124 | * |
| 125 | * @param component component the Histogram is defined in |
| 126 | * @param feature feature the Histogram is defined in |
| 127 | * @param metricName local name of the metric |
| 128 | * @return the created Histogram Metric |
| 129 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 130 | @Override |
| 131 | public Histogram createHistogram(final MetricsComponent component, |
| 132 | final MetricsFeature feature, |
| 133 | final String metricName) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 134 | final String name = generateName(component, feature, metricName); |
| 135 | return metricsRegistry.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 the created Timer Metric |
| 145 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 146 | @Override |
| 147 | public Timer createTimer(final MetricsComponent component, |
| 148 | final MetricsFeature feature, |
| 149 | final String metricName) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 150 | final String name = generateName(component, feature, metricName); |
| 151 | return metricsRegistry.timer(name); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Creates a Meter metric. |
| 156 | * |
| 157 | * @param component component the Meter is defined in |
| 158 | * @param feature feature the Meter is defined in |
| 159 | * @param metricName local name of the metric |
| 160 | * @return the created Meter Metric |
| 161 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 162 | @Override |
| 163 | public Meter createMeter(final MetricsComponent component, |
| 164 | final MetricsFeature feature, |
| 165 | final String metricName) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 166 | final String name = generateName(component, feature, metricName); |
| 167 | return metricsRegistry.meter(name); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Registers an already created Metric. This is used for situation where a |
| 172 | * caller needs to allocate its own Metric, but still register it with the |
| 173 | * system. |
| 174 | * |
| 175 | * @param <T> Metric type |
| 176 | * @param component component the Metric is defined in |
| 177 | * @param feature feature the Metric is defined in |
| 178 | * @param metricName local name of the metric |
| 179 | * @param metric Metric to register |
| 180 | * @return the registered Metric |
| 181 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 182 | @Override |
| 183 | public <T extends Metric> T registerMetric( |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 184 | final MetricsComponent component, |
| 185 | final MetricsFeature feature, |
| 186 | final String metricName, |
| 187 | final T metric) { |
| 188 | final String name = generateName(component, feature, metricName); |
| 189 | metricsRegistry.register(name, metric); |
| 190 | return metric; |
| 191 | } |
| 192 | |
| 193 | /** |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 194 | * Removes the metric with the given name. |
| 195 | * |
| 196 | * @param component component the Metric is defined in |
| 197 | * @param feature feature the Metric is defined in |
| 198 | * @param metricName local name of the metric |
| 199 | * @return true if the metric existed and was removed, otherwise false |
| 200 | */ |
| 201 | @Override |
| 202 | public boolean removeMetric(final MetricsComponent component, |
| 203 | final MetricsFeature feature, |
| 204 | final String metricName) { |
| 205 | final String name = generateName(component, feature, metricName); |
| 206 | return metricsRegistry.remove(name); |
| 207 | } |
| 208 | |
| 209 | /** |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 210 | * Fetches the existing Timers. |
| 211 | * |
| 212 | * @param filter filter to use to select Timers |
| 213 | * @return a map of the Timers that match the filter, with the key as the |
| 214 | * name String to the Timer. |
| 215 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 216 | @Override |
| 217 | public Map<String, Timer> getTimers(final MetricFilter filter) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 218 | return metricsRegistry.getTimers(filter); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Fetches the existing Gauges. |
| 223 | * |
| 224 | * @param filter filter to use to select Gauges |
| 225 | * @return a map of the Gauges that match the filter, with the key as the |
| 226 | * name String to the Gauge. |
| 227 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 228 | @Override |
| 229 | public Map<String, Gauge> getGauges(final MetricFilter filter) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 230 | return metricsRegistry.getGauges(filter); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Fetches the existing Counters. |
| 235 | * |
| 236 | * @param filter filter to use to select Counters |
| 237 | * @return a map of the Counters that match the filter, with the key as the |
| 238 | * name String to the Counter. |
| 239 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 240 | @Override |
| 241 | public Map<String, Counter> getCounters(final MetricFilter filter) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 242 | return metricsRegistry.getCounters(filter); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Fetches the existing Meters. |
| 247 | * |
| 248 | * @param filter filter to use to select Meters |
| 249 | * @return a map of the Meters that match the filter, with the key as the |
| 250 | * name String to the Meter. |
| 251 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 252 | @Override |
| 253 | public Map<String, Meter> getMeters(final MetricFilter filter) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 254 | return metricsRegistry.getMeters(filter); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Fetches the existing Histograms. |
| 259 | * |
| 260 | * @param filter filter to use to select Histograms |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 261 | * @return a map of the Histograms that match the filter, with the key as |
| 262 | * the name String to the Histogram. |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 263 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 264 | @Override |
| 265 | public Map<String, Histogram> getHistograms(final MetricFilter filter) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 266 | return metricsRegistry.getHistograms(filter); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Removes all Metrics that match a given filter. |
| 271 | * |
| 272 | * @param filter filter to use to select the Metrics to remove. |
| 273 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 274 | @Override |
| 275 | public void removeMatching(final MetricFilter filter) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 276 | metricsRegistry.removeMatching(filter); |
| 277 | } |
| 278 | } |