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