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 | /** |
| 105 | * Generates a name for a Metric from its component and feature. |
| 106 | * |
| 107 | * @param component component the metric is defined in |
| 108 | * @param feature feature the metric is defined in |
| 109 | * @param metricName local name of the metric |
| 110 | * |
| 111 | * @return full name of the metric |
| 112 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 113 | private String generateName(final MetricsComponent component, |
| 114 | final MetricsFeature feature, |
| 115 | final String metricName) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 116 | return MetricRegistry.name(component.getName(), |
| 117 | feature.getName(), |
| 118 | metricName); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Creates a Counter metric. |
| 123 | * |
| 124 | * @param component component the Counter is defined in |
| 125 | * @param feature feature the Counter is defined in |
| 126 | * @param metricName local name of the metric |
| 127 | * @return the created Counter Meteric |
| 128 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 129 | @Override |
| 130 | public Counter createCounter(final MetricsComponent component, |
| 131 | final MetricsFeature feature, |
| 132 | final String metricName) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 133 | final String name = generateName(component, feature, metricName); |
| 134 | return metricsRegistry.counter(name); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Creates a Histogram metric. |
| 139 | * |
| 140 | * @param component component the Histogram is defined in |
| 141 | * @param feature feature the Histogram is defined in |
| 142 | * @param metricName local name of the metric |
| 143 | * @return the created Histogram Metric |
| 144 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 145 | @Override |
| 146 | public Histogram createHistogram(final MetricsComponent component, |
| 147 | final MetricsFeature feature, |
| 148 | final String metricName) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 149 | final String name = generateName(component, feature, metricName); |
| 150 | return metricsRegistry.histogram(name); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Creates a Timer metric. |
| 155 | * |
| 156 | * @param component component the Timer is defined in |
Yuta HIGUCHI | 6a46283 | 2014-11-23 23:56:03 -0800 | [diff] [blame] | 157 | * @param feature feature the Timer is defined in |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 158 | * @param metricName local name of the metric |
| 159 | * @return the created Timer Metric |
| 160 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 161 | @Override |
| 162 | public Timer createTimer(final MetricsComponent component, |
| 163 | final MetricsFeature feature, |
| 164 | final String metricName) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 165 | final String name = generateName(component, feature, metricName); |
| 166 | return metricsRegistry.timer(name); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Creates a Meter metric. |
| 171 | * |
| 172 | * @param component component the Meter is defined in |
| 173 | * @param feature feature the Meter is defined in |
| 174 | * @param metricName local name of the metric |
| 175 | * @return the created Meter Metric |
| 176 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 177 | @Override |
| 178 | public Meter createMeter(final MetricsComponent component, |
| 179 | final MetricsFeature feature, |
| 180 | final String metricName) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 181 | final String name = generateName(component, feature, metricName); |
| 182 | return metricsRegistry.meter(name); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Registers an already created Metric. This is used for situation where a |
| 187 | * caller needs to allocate its own Metric, but still register it with the |
| 188 | * system. |
| 189 | * |
| 190 | * @param <T> Metric type |
| 191 | * @param component component the Metric is defined in |
| 192 | * @param feature feature the Metric is defined in |
| 193 | * @param metricName local name of the metric |
| 194 | * @param metric Metric to register |
| 195 | * @return the registered Metric |
| 196 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 197 | @Override |
| 198 | public <T extends Metric> T registerMetric( |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 199 | final MetricsComponent component, |
| 200 | final MetricsFeature feature, |
| 201 | final String metricName, |
| 202 | final T metric) { |
| 203 | final String name = generateName(component, feature, metricName); |
| 204 | metricsRegistry.register(name, metric); |
| 205 | return metric; |
| 206 | } |
| 207 | |
| 208 | /** |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 209 | * Removes the metric with the given name. |
| 210 | * |
| 211 | * @param component component the Metric is defined in |
| 212 | * @param feature feature the Metric is defined in |
| 213 | * @param metricName local name of the metric |
| 214 | * @return true if the metric existed and was removed, otherwise false |
| 215 | */ |
| 216 | @Override |
| 217 | public boolean removeMetric(final MetricsComponent component, |
| 218 | final MetricsFeature feature, |
| 219 | final String metricName) { |
| 220 | final String name = generateName(component, feature, metricName); |
| 221 | return metricsRegistry.remove(name); |
| 222 | } |
| 223 | |
| 224 | /** |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 225 | * Fetches the existing Timers. |
| 226 | * |
| 227 | * @param filter filter to use to select Timers |
| 228 | * @return a map of the Timers that match the filter, with the key as the |
| 229 | * name String to the Timer. |
| 230 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 231 | @Override |
| 232 | public Map<String, Timer> getTimers(final MetricFilter filter) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 233 | return metricsRegistry.getTimers(filter); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Fetches the existing Gauges. |
| 238 | * |
| 239 | * @param filter filter to use to select Gauges |
| 240 | * @return a map of the Gauges that match the filter, with the key as the |
| 241 | * name String to the Gauge. |
| 242 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 243 | @Override |
| 244 | public Map<String, Gauge> getGauges(final MetricFilter filter) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 245 | return metricsRegistry.getGauges(filter); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Fetches the existing Counters. |
| 250 | * |
| 251 | * @param filter filter to use to select Counters |
| 252 | * @return a map of the Counters that match the filter, with the key as the |
| 253 | * name String to the Counter. |
| 254 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 255 | @Override |
| 256 | public Map<String, Counter> getCounters(final MetricFilter filter) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 257 | return metricsRegistry.getCounters(filter); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Fetches the existing Meters. |
| 262 | * |
| 263 | * @param filter filter to use to select Meters |
| 264 | * @return a map of the Meters that match the filter, with the key as the |
| 265 | * name String to the Meter. |
| 266 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 267 | @Override |
| 268 | public Map<String, Meter> getMeters(final MetricFilter filter) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 269 | return metricsRegistry.getMeters(filter); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Fetches the existing Histograms. |
| 274 | * |
| 275 | * @param filter filter to use to select Histograms |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 276 | * @return a map of the Histograms that match the filter, with the key as |
| 277 | * the name String to the Histogram. |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 278 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 279 | @Override |
| 280 | public Map<String, Histogram> getHistograms(final MetricFilter filter) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 281 | return metricsRegistry.getHistograms(filter); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Removes all Metrics that match a given filter. |
| 286 | * |
| 287 | * @param filter filter to use to select the Metrics to remove. |
| 288 | */ |
Pavlin Radoslavov | 3559249 | 2014-10-21 21:49:58 -0700 | [diff] [blame] | 289 | @Override |
| 290 | public void removeMatching(final MetricFilter filter) { |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 291 | metricsRegistry.removeMatching(filter); |
| 292 | } |
Flavio Castro | 4b51941 | 2015-07-24 12:57:59 -0700 | [diff] [blame] | 293 | |
| 294 | /** |
| 295 | * Fetches the existing Meters. |
| 296 | * |
| 297 | * |
| 298 | * @return a map of all metrics with the key as the |
| 299 | * name String to the Meter. |
| 300 | */ |
| 301 | public Map<String, Metric> getMetrics() { |
| 302 | return metricsRegistry.getMetrics(); |
| 303 | } |
pankaj | 390abbc | 2014-10-01 17:01:05 -0700 | [diff] [blame] | 304 | } |