blob: dacd31044ac7c74c2ceab6e978f09f221cdd3a06 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
pankaj390abbc2014-10-01 17:01:05 -070016package org.onlab.metrics;
17
pankaj390abbc2014-10-01 17:01:05 -070018import com.codahale.metrics.Counter;
19import com.codahale.metrics.Gauge;
20import com.codahale.metrics.Histogram;
21import com.codahale.metrics.Meter;
22import com.codahale.metrics.Metric;
23import com.codahale.metrics.MetricFilter;
24import com.codahale.metrics.MetricRegistry;
Jordan Haltermanf3f050a2018-02-09 19:09:30 -080025import com.codahale.metrics.SlidingWindowReservoir;
pankaj390abbc2014-10-01 17:01:05 -070026import com.codahale.metrics.Timer;
Jian Li55cbd5c2016-04-06 09:50:20 -070027import com.google.common.collect.Sets;
28
29import java.util.Map;
30import java.util.Set;
31import java.util.concurrent.ConcurrentHashMap;
32import java.util.concurrent.ConcurrentMap;
pankaj390abbc2014-10-01 17:01:05 -070033
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 Vachuska7b652ad2014-10-30 14:10:51 -070055 * private final Gauge&lt;Long&gt; gauge =
56 * new {@literal Gauge&lt;Long&gt}() {
pankaj390abbc2014-10-01 17:01:05 -070057 * {@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 Radoslavov35592492014-10-21 21:49:58 -070066public class MetricsManager implements MetricsService {
pankaj390abbc2014-10-01 17:01:05 -070067
68 /**
69 * Registry to hold the Components defined in the system.
70 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -070071 private ConcurrentMap<String, MetricsComponent> componentsRegistry =
72 new ConcurrentHashMap<>();
pankaj390abbc2014-10-01 17:01:05 -070073
74 /**
75 * Registry for the Metrics objects created in the system.
76 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -070077 private MetricRegistry metricsRegistry = new MetricRegistry();
pankaj390abbc2014-10-01 17:01:05 -070078
pankaj5de84112014-10-02 15:33:28 -070079 /**
Jian Li55cbd5c2016-04-06 09:50:20 -070080 * Reporter for exposing metrics objects to third party persistent system.
81 */
82 private Set<MetricsReporter> reporters = Sets.newConcurrentHashSet();
83
84 /**
Pavlin Radoslavov35592492014-10-21 21:49:58 -070085 * Clears the internal state.
pankaj5de84112014-10-02 15:33:28 -070086 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -070087 protected void clear() {
pankaj9e34ff22014-10-06 13:21:03 -070088 this.componentsRegistry = new ConcurrentHashMap<>();
Pavlin Radoslavov35592492014-10-21 21:49:58 -070089 this.metricsRegistry = new MetricRegistry();
pankajf6577b62014-10-02 16:38:38 -070090 }
91
pankaj390abbc2014-10-01 17:01:05 -070092 /**
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 Radoslavov35592492014-10-21 21:49:58 -070098 @Override
99 public MetricsComponent registerComponent(final String name) {
pankaj390abbc2014-10-01 17:01:05 -0700100 MetricsComponent component = componentsRegistry.get(name);
101 if (component == null) {
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700102 final MetricsComponent createdComponent =
103 new MetricsComponent(name);
pankaj390abbc2014-10-01 17:01:05 -0700104 component = componentsRegistry.putIfAbsent(name, createdComponent);
105 if (component == null) {
106 component = createdComponent;
107 }
108 }
109 return component;
110 }
111
112 /**
Jian Li7261c7b2016-03-05 00:04:55 -0800113 * Fetches existing metric registry.
114 *
115 * @return metric registry
116 */
117 @Override
118 public MetricRegistry getMetricRegistry() {
119 return metricsRegistry;
120 }
121
122 /**
pankaj390abbc2014-10-01 17:01:05 -0700123 * 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 Radoslavov35592492014-10-21 21:49:58 -0700131 private String generateName(final MetricsComponent component,
132 final MetricsFeature feature,
133 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700134 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 Radoslavov35592492014-10-21 21:49:58 -0700147 @Override
148 public Counter createCounter(final MetricsComponent component,
149 final MetricsFeature feature,
150 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700151 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 Radoslavov35592492014-10-21 21:49:58 -0700163 @Override
164 public Histogram createHistogram(final MetricsComponent component,
165 final MetricsFeature feature,
166 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700167 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 HIGUCHI6a462832014-11-23 23:56:03 -0800175 * @param feature feature the Timer is defined in
pankaj390abbc2014-10-01 17:01:05 -0700176 * @param metricName local name of the metric
177 * @return the created Timer Metric
178 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700179 @Override
180 public Timer createTimer(final MetricsComponent component,
181 final MetricsFeature feature,
182 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700183 final String name = generateName(component, feature, metricName);
Jordan Haltermanf3f050a2018-02-09 19:09:30 -0800184 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 }
pankaj390abbc2014-10-01 17:01:05 -0700195 }
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 Radoslavov35592492014-10-21 21:49:58 -0700205 @Override
206 public Meter createMeter(final MetricsComponent component,
207 final MetricsFeature feature,
208 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700209 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 Radoslavov35592492014-10-21 21:49:58 -0700225 @Override
226 public <T extends Metric> T registerMetric(
pankaj390abbc2014-10-01 17:01:05 -0700227 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 Li55cbd5c2016-04-06 09:50:20 -0700237 * 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 Radoslavov35592492014-10-21 21:49:58 -0700265 * 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 /**
pankaj390abbc2014-10-01 17:01:05 -0700281 * 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 Radoslavov35592492014-10-21 21:49:58 -0700287 @Override
288 public Map<String, Timer> getTimers(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700289 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 Radoslavov35592492014-10-21 21:49:58 -0700299 @Override
300 public Map<String, Gauge> getGauges(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700301 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 Radoslavov35592492014-10-21 21:49:58 -0700311 @Override
312 public Map<String, Counter> getCounters(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700313 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 Radoslavov35592492014-10-21 21:49:58 -0700323 @Override
324 public Map<String, Meter> getMeters(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700325 return metricsRegistry.getMeters(filter);
326 }
327
328 /**
329 * Fetches the existing Histograms.
330 *
331 * @param filter filter to use to select Histograms
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700332 * @return a map of the Histograms that match the filter, with the key as
333 * the name String to the Histogram.
pankaj390abbc2014-10-01 17:01:05 -0700334 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700335 @Override
336 public Map<String, Histogram> getHistograms(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700337 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 Radoslavov35592492014-10-21 21:49:58 -0700345 @Override
346 public void removeMatching(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700347 metricsRegistry.removeMatching(filter);
348 }
Flavio Castro4b519412015-07-24 12:57:59 -0700349
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 }
pankaj390abbc2014-10-01 17:01:05 -0700360}