Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 16 | package org.onlab.metrics; |
| 17 | |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 18 | import com.codahale.metrics.Counter; |
| 19 | import com.codahale.metrics.Gauge; |
| 20 | import com.codahale.metrics.Histogram; |
| 21 | import com.codahale.metrics.Meter; |
| 22 | import com.codahale.metrics.Metric; |
| 23 | import com.codahale.metrics.MetricFilter; |
| 24 | import com.codahale.metrics.MetricRegistry; |
Jordan Halterman | f3f050a | 2018-02-09 19:09:30 -0800 | [diff] [blame] | 25 | import com.codahale.metrics.SlidingWindowReservoir; |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 26 | import com.codahale.metrics.Timer; |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 27 | import com.google.common.collect.Sets; |
| 28 | |
| 29 | import java.util.Map; |
| 30 | import java.util.Set; |
| 31 | import java.util.concurrent.ConcurrentHashMap; |
| 32 | import java.util.concurrent.ConcurrentMap; |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * This class holds the Metrics registry for ONOS. |
| 36 | * All metrics (Counter, Histogram, Timer, Meter, Gauge) use a hierarchical |
| 37 | * string-based naming scheme: COMPONENT.FEATURE.NAME. |
| 38 | * Example: "Topology.Counters.TopologyUpdates". |
| 39 | * The COMPONENT and FEATURE names have to be registered in advance before |
| 40 | * a metric can be created. Example: |
| 41 | * <pre> |
| 42 | * <code> |
| 43 | * private final MetricsManager.MetricsComponent COMPONENT = |
| 44 | * MetricsManager.registerComponent("Topology"); |
| 45 | * private final MetricsManager.MetricsFeature FEATURE = |
| 46 | * COMPONENT.registerFeature("Counters"); |
| 47 | * private final Counter counterTopologyUpdates = |
| 48 | * MetricsManager.createCounter(COMPONENT, FEATURE, "TopologyUpdates"); |
| 49 | * </code> |
| 50 | * </pre> |
| 51 | * Gauges are slightly different because they are not created directly in |
| 52 | * this class, but are allocated by the caller and passed in for registration: |
| 53 | * <pre> |
| 54 | * <code> |
Thomas Vachuska | 7b652ad | 2014-10-30 14:10:51 -0700 | [diff] [blame] | 55 | * private final Gauge<Long> gauge = |
| 56 | * new {@literal Gauge<Long>}() { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 57 | * {@literal @}Override |
| 58 | * public Long getValue() { |
| 59 | * return gaugeValue; |
| 60 | * } |
| 61 | * }; |
| 62 | * MetricsManager.registerMetric(COMPONENT, FEATURE, GAUGE_NAME, gauge); |
| 63 | * </code> |
| 64 | * </pre> |
| 65 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 66 | public class MetricsManager implements MetricsService { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Registry to hold the Components defined in the system. |
| 70 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 71 | private ConcurrentMap<String, MetricsComponent> componentsRegistry = |
| 72 | new ConcurrentHashMap<>(); |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Registry for the Metrics objects created in the system. |
| 76 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 77 | private MetricRegistry metricsRegistry = new MetricRegistry(); |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 78 | |
pankaj | 5de8411 | 2014-10-02 15:33:28 -0700 | [diff] [blame] | 79 | /** |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 80 | * Reporter for exposing metrics objects to third party persistent system. |
| 81 | */ |
| 82 | private Set<MetricsReporter> reporters = Sets.newConcurrentHashSet(); |
| 83 | |
| 84 | /** |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 85 | * Clears the internal state. |
pankaj | 5de8411 | 2014-10-02 15:33:28 -0700 | [diff] [blame] | 86 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 87 | protected void clear() { |
pankaj | 9e34ff2 | 2014-10-06 13:21:03 -0700 | [diff] [blame] | 88 | this.componentsRegistry = new ConcurrentHashMap<>(); |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 89 | this.metricsRegistry = new MetricRegistry(); |
pankaj | f6577b6 | 2014-10-02 16:38:38 -0700 | [diff] [blame] | 90 | } |
| 91 | |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Registers a component. |
| 94 | * |
| 95 | * @param name name of the Component to register |
| 96 | * @return MetricsComponent object that can be used to create Metrics. |
| 97 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 98 | @Override |
| 99 | public MetricsComponent registerComponent(final String name) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 100 | MetricsComponent component = componentsRegistry.get(name); |
| 101 | if (component == null) { |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 102 | final MetricsComponent createdComponent = |
| 103 | new MetricsComponent(name); |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 104 | component = componentsRegistry.putIfAbsent(name, createdComponent); |
| 105 | if (component == null) { |
| 106 | component = createdComponent; |
| 107 | } |
| 108 | } |
| 109 | return component; |
| 110 | } |
| 111 | |
| 112 | /** |
Jian Li | 7261c7b | 2016-03-05 00:04:55 -0800 | [diff] [blame] | 113 | * Fetches existing metric registry. |
| 114 | * |
| 115 | * @return metric registry |
| 116 | */ |
| 117 | @Override |
| 118 | public MetricRegistry getMetricRegistry() { |
| 119 | return metricsRegistry; |
| 120 | } |
| 121 | |
| 122 | /** |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 123 | * Generates a name for a Metric from its component and feature. |
| 124 | * |
| 125 | * @param component component the metric is defined in |
| 126 | * @param feature feature the metric is defined in |
| 127 | * @param metricName local name of the metric |
| 128 | * |
| 129 | * @return full name of the metric |
| 130 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 131 | private String generateName(final MetricsComponent component, |
| 132 | final MetricsFeature feature, |
| 133 | final String metricName) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 134 | return MetricRegistry.name(component.getName(), |
| 135 | feature.getName(), |
| 136 | metricName); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Creates a Counter metric. |
| 141 | * |
| 142 | * @param component component the Counter is defined in |
| 143 | * @param feature feature the Counter is defined in |
| 144 | * @param metricName local name of the metric |
| 145 | * @return the created Counter Meteric |
| 146 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 147 | @Override |
| 148 | public Counter createCounter(final MetricsComponent component, |
| 149 | final MetricsFeature feature, |
| 150 | final String metricName) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 151 | final String name = generateName(component, feature, metricName); |
| 152 | return metricsRegistry.counter(name); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Creates a Histogram metric. |
| 157 | * |
| 158 | * @param component component the Histogram is defined in |
| 159 | * @param feature feature the Histogram is defined in |
| 160 | * @param metricName local name of the metric |
| 161 | * @return the created Histogram Metric |
| 162 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 163 | @Override |
| 164 | public Histogram createHistogram(final MetricsComponent component, |
| 165 | final MetricsFeature feature, |
| 166 | final String metricName) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 167 | final String name = generateName(component, feature, metricName); |
| 168 | return metricsRegistry.histogram(name); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Creates a Timer metric. |
| 173 | * |
| 174 | * @param component component the Timer is defined in |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 175 | * @param feature feature the Timer is defined in |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 176 | * @param metricName local name of the metric |
| 177 | * @return the created Timer Metric |
| 178 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 179 | @Override |
| 180 | public Timer createTimer(final MetricsComponent component, |
| 181 | final MetricsFeature feature, |
| 182 | final String metricName) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 183 | final String name = generateName(component, feature, metricName); |
Jordan Halterman | f3f050a | 2018-02-09 19:09:30 -0800 | [diff] [blame] | 184 | Timer timer = metricsRegistry.getTimers().get(name); |
| 185 | if (timer != null) { |
| 186 | return timer; |
| 187 | } |
| 188 | |
| 189 | timer = new Timer(new SlidingWindowReservoir(1028)); |
| 190 | try { |
| 191 | return metricsRegistry.register(name, timer); |
| 192 | } catch (IllegalArgumentException e) { |
| 193 | return metricsRegistry.timer(name); |
| 194 | } |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Creates a Meter metric. |
| 199 | * |
| 200 | * @param component component the Meter is defined in |
| 201 | * @param feature feature the Meter is defined in |
| 202 | * @param metricName local name of the metric |
| 203 | * @return the created Meter Metric |
| 204 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 205 | @Override |
| 206 | public Meter createMeter(final MetricsComponent component, |
| 207 | final MetricsFeature feature, |
| 208 | final String metricName) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 209 | final String name = generateName(component, feature, metricName); |
| 210 | return metricsRegistry.meter(name); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Registers an already created Metric. This is used for situation where a |
| 215 | * caller needs to allocate its own Metric, but still register it with the |
| 216 | * system. |
| 217 | * |
| 218 | * @param <T> Metric type |
| 219 | * @param component component the Metric is defined in |
| 220 | * @param feature feature the Metric is defined in |
| 221 | * @param metricName local name of the metric |
| 222 | * @param metric Metric to register |
| 223 | * @return the registered Metric |
| 224 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 225 | @Override |
| 226 | public <T extends Metric> T registerMetric( |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 227 | final MetricsComponent component, |
| 228 | final MetricsFeature feature, |
| 229 | final String metricName, |
| 230 | final T metric) { |
| 231 | final String name = generateName(component, feature, metricName); |
| 232 | metricsRegistry.register(name, metric); |
| 233 | return metric; |
| 234 | } |
| 235 | |
| 236 | /** |
Jian Li | 55cbd5c | 2016-04-06 09:50:20 -0700 | [diff] [blame] | 237 | * Registers a reporter to receive any changes on metric registry. |
| 238 | * |
| 239 | * @param reporter metric reporter |
| 240 | */ |
| 241 | @Override |
| 242 | public void registerReporter(MetricsReporter reporter) { |
| 243 | reporters.add(reporter); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Unregisters the given metric reporter. |
| 248 | * |
| 249 | * @param reporter metric reporter |
| 250 | */ |
| 251 | @Override |
| 252 | public void unregisterReporter(MetricsReporter reporter) { |
| 253 | reporters.remove(reporter); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Notifies the changes on metric registry to all registered reporters. |
| 258 | */ |
| 259 | @Override |
| 260 | public void notifyReporters() { |
| 261 | reporters.forEach(MetricsReporter::notifyMetricsChange); |
| 262 | } |
| 263 | |
| 264 | /** |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 265 | * Removes the metric with the given name. |
| 266 | * |
| 267 | * @param component component the Metric is defined in |
| 268 | * @param feature feature the Metric is defined in |
| 269 | * @param metricName local name of the metric |
| 270 | * @return true if the metric existed and was removed, otherwise false |
| 271 | */ |
| 272 | @Override |
| 273 | public boolean removeMetric(final MetricsComponent component, |
| 274 | final MetricsFeature feature, |
| 275 | final String metricName) { |
| 276 | final String name = generateName(component, feature, metricName); |
| 277 | return metricsRegistry.remove(name); |
| 278 | } |
| 279 | |
| 280 | /** |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 281 | * Fetches the existing Timers. |
| 282 | * |
| 283 | * @param filter filter to use to select Timers |
| 284 | * @return a map of the Timers that match the filter, with the key as the |
| 285 | * name String to the Timer. |
| 286 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 287 | @Override |
| 288 | public Map<String, Timer> getTimers(final MetricFilter filter) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 289 | return metricsRegistry.getTimers(filter); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Fetches the existing Gauges. |
| 294 | * |
| 295 | * @param filter filter to use to select Gauges |
| 296 | * @return a map of the Gauges that match the filter, with the key as the |
| 297 | * name String to the Gauge. |
| 298 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 299 | @Override |
| 300 | public Map<String, Gauge> getGauges(final MetricFilter filter) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 301 | return metricsRegistry.getGauges(filter); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Fetches the existing Counters. |
| 306 | * |
| 307 | * @param filter filter to use to select Counters |
| 308 | * @return a map of the Counters that match the filter, with the key as the |
| 309 | * name String to the Counter. |
| 310 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 311 | @Override |
| 312 | public Map<String, Counter> getCounters(final MetricFilter filter) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 313 | return metricsRegistry.getCounters(filter); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Fetches the existing Meters. |
| 318 | * |
| 319 | * @param filter filter to use to select Meters |
| 320 | * @return a map of the Meters that match the filter, with the key as the |
| 321 | * name String to the Meter. |
| 322 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 323 | @Override |
| 324 | public Map<String, Meter> getMeters(final MetricFilter filter) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 325 | return metricsRegistry.getMeters(filter); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Fetches the existing Histograms. |
| 330 | * |
| 331 | * @param filter filter to use to select Histograms |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 332 | * @return a map of the Histograms that match the filter, with the key as |
| 333 | * the name String to the Histogram. |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 334 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 335 | @Override |
| 336 | public Map<String, Histogram> getHistograms(final MetricFilter filter) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 337 | return metricsRegistry.getHistograms(filter); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Removes all Metrics that match a given filter. |
| 342 | * |
| 343 | * @param filter filter to use to select the Metrics to remove. |
| 344 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 345 | @Override |
| 346 | public void removeMatching(final MetricFilter filter) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 347 | metricsRegistry.removeMatching(filter); |
| 348 | } |
Flavio Castro | 4b51941 | 2015-07-24 12:57:59 -0700 | [diff] [blame] | 349 | |
| 350 | /** |
| 351 | * Fetches the existing Meters. |
| 352 | * |
| 353 | * |
| 354 | * @return a map of all metrics with the key as the |
| 355 | * name String to the Meter. |
| 356 | */ |
| 357 | public Map<String, Metric> getMetrics() { |
| 358 | return metricsRegistry.getMetrics(); |
| 359 | } |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 360 | } |