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