blob: 64bf9ee7d2817aa6e3a8e20ea5081fa7331aea0d [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
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
18import java.util.Map;
pankaj5de84112014-10-02 15:33:28 -070019import java.util.concurrent.ConcurrentHashMap;
pankaj390abbc2014-10-01 17:01:05 -070020import java.util.concurrent.ConcurrentMap;
21
22import com.codahale.metrics.Counter;
23import com.codahale.metrics.Gauge;
24import com.codahale.metrics.Histogram;
25import com.codahale.metrics.Meter;
26import com.codahale.metrics.Metric;
27import com.codahale.metrics.MetricFilter;
28import com.codahale.metrics.MetricRegistry;
29import 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 Vachuska7b652ad2014-10-30 14:10:51 -070052 * private final Gauge&lt;Long&gt; gauge =
53 * new {@literal Gauge&lt;Long&gt}() {
pankaj390abbc2014-10-01 17:01:05 -070054 * {@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 Radoslavov35592492014-10-21 21:49:58 -070063public class MetricsManager implements MetricsService {
pankaj390abbc2014-10-01 17:01:05 -070064
65 /**
66 * Registry to hold the Components defined in the system.
67 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -070068 private ConcurrentMap<String, MetricsComponent> componentsRegistry =
69 new ConcurrentHashMap<>();
pankaj390abbc2014-10-01 17:01:05 -070070
71 /**
72 * Registry for the Metrics objects created in the system.
73 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -070074 private MetricRegistry metricsRegistry = new MetricRegistry();
pankaj390abbc2014-10-01 17:01:05 -070075
pankaj5de84112014-10-02 15:33:28 -070076 /**
Pavlin Radoslavov35592492014-10-21 21:49:58 -070077 * Clears the internal state.
pankaj5de84112014-10-02 15:33:28 -070078 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -070079 protected void clear() {
pankaj9e34ff22014-10-06 13:21:03 -070080 this.componentsRegistry = new ConcurrentHashMap<>();
Pavlin Radoslavov35592492014-10-21 21:49:58 -070081 this.metricsRegistry = new MetricRegistry();
pankajf6577b62014-10-02 16:38:38 -070082 }
83
pankaj390abbc2014-10-01 17:01:05 -070084 /**
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 Radoslavov35592492014-10-21 21:49:58 -070090 @Override
91 public MetricsComponent registerComponent(final String name) {
pankaj390abbc2014-10-01 17:01:05 -070092 MetricsComponent component = componentsRegistry.get(name);
93 if (component == null) {
Pavlin Radoslavov35592492014-10-21 21:49:58 -070094 final MetricsComponent createdComponent =
95 new MetricsComponent(name);
pankaj390abbc2014-10-01 17:01:05 -070096 component = componentsRegistry.putIfAbsent(name, createdComponent);
97 if (component == null) {
98 component = createdComponent;
99 }
100 }
101 return component;
102 }
103
104 /**
Jian Li7261c7b2016-03-05 00:04:55 -0800105 * Fetches existing metric registry.
106 *
107 * @return metric registry
108 */
109 @Override
110 public MetricRegistry getMetricRegistry() {
111 return metricsRegistry;
112 }
113
114 /**
pankaj390abbc2014-10-01 17:01:05 -0700115 * 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 Radoslavov35592492014-10-21 21:49:58 -0700123 private String generateName(final MetricsComponent component,
124 final MetricsFeature feature,
125 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700126 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 Radoslavov35592492014-10-21 21:49:58 -0700139 @Override
140 public Counter createCounter(final MetricsComponent component,
141 final MetricsFeature feature,
142 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700143 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 Radoslavov35592492014-10-21 21:49:58 -0700155 @Override
156 public Histogram createHistogram(final MetricsComponent component,
157 final MetricsFeature feature,
158 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700159 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 HIGUCHI6a462832014-11-23 23:56:03 -0800167 * @param feature feature the Timer is defined in
pankaj390abbc2014-10-01 17:01:05 -0700168 * @param metricName local name of the metric
169 * @return the created Timer Metric
170 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700171 @Override
172 public Timer createTimer(final MetricsComponent component,
173 final MetricsFeature feature,
174 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700175 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 Radoslavov35592492014-10-21 21:49:58 -0700187 @Override
188 public Meter createMeter(final MetricsComponent component,
189 final MetricsFeature feature,
190 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700191 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 Radoslavov35592492014-10-21 21:49:58 -0700207 @Override
208 public <T extends Metric> T registerMetric(
pankaj390abbc2014-10-01 17:01:05 -0700209 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 Radoslavov35592492014-10-21 21:49:58 -0700219 * 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 /**
pankaj390abbc2014-10-01 17:01:05 -0700235 * 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 Radoslavov35592492014-10-21 21:49:58 -0700241 @Override
242 public Map<String, Timer> getTimers(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700243 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 Radoslavov35592492014-10-21 21:49:58 -0700253 @Override
254 public Map<String, Gauge> getGauges(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700255 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 Radoslavov35592492014-10-21 21:49:58 -0700265 @Override
266 public Map<String, Counter> getCounters(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700267 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 Radoslavov35592492014-10-21 21:49:58 -0700277 @Override
278 public Map<String, Meter> getMeters(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700279 return metricsRegistry.getMeters(filter);
280 }
281
282 /**
283 * Fetches the existing Histograms.
284 *
285 * @param filter filter to use to select Histograms
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700286 * @return a map of the Histograms that match the filter, with the key as
287 * the name String to the Histogram.
pankaj390abbc2014-10-01 17:01:05 -0700288 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700289 @Override
290 public Map<String, Histogram> getHistograms(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700291 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 Radoslavov35592492014-10-21 21:49:58 -0700299 @Override
300 public void removeMatching(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700301 metricsRegistry.removeMatching(filter);
302 }
Flavio Castro4b519412015-07-24 12:57:59 -0700303
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 }
pankaj390abbc2014-10-01 17:01:05 -0700314}