blob: 6c9314adf760a522ec0905b694a81dce66c5e64f [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 /**
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 Radoslavov35592492014-10-21 21:49:58 -0700113 private String generateName(final MetricsComponent component,
114 final MetricsFeature feature,
115 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700116 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 Radoslavov35592492014-10-21 21:49:58 -0700129 @Override
130 public Counter createCounter(final MetricsComponent component,
131 final MetricsFeature feature,
132 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700133 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 Radoslavov35592492014-10-21 21:49:58 -0700145 @Override
146 public Histogram createHistogram(final MetricsComponent component,
147 final MetricsFeature feature,
148 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700149 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 HIGUCHI6a462832014-11-23 23:56:03 -0800157 * @param feature feature the Timer is defined in
pankaj390abbc2014-10-01 17:01:05 -0700158 * @param metricName local name of the metric
159 * @return the created Timer Metric
160 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700161 @Override
162 public Timer createTimer(final MetricsComponent component,
163 final MetricsFeature feature,
164 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700165 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 Radoslavov35592492014-10-21 21:49:58 -0700177 @Override
178 public Meter createMeter(final MetricsComponent component,
179 final MetricsFeature feature,
180 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700181 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 Radoslavov35592492014-10-21 21:49:58 -0700197 @Override
198 public <T extends Metric> T registerMetric(
pankaj390abbc2014-10-01 17:01:05 -0700199 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 Radoslavov35592492014-10-21 21:49:58 -0700209 * 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 /**
pankaj390abbc2014-10-01 17:01:05 -0700225 * 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 Radoslavov35592492014-10-21 21:49:58 -0700231 @Override
232 public Map<String, Timer> getTimers(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700233 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 Radoslavov35592492014-10-21 21:49:58 -0700243 @Override
244 public Map<String, Gauge> getGauges(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700245 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 Radoslavov35592492014-10-21 21:49:58 -0700255 @Override
256 public Map<String, Counter> getCounters(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700257 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 Radoslavov35592492014-10-21 21:49:58 -0700267 @Override
268 public Map<String, Meter> getMeters(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700269 return metricsRegistry.getMeters(filter);
270 }
271
272 /**
273 * Fetches the existing Histograms.
274 *
275 * @param filter filter to use to select Histograms
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700276 * @return a map of the Histograms that match the filter, with the key as
277 * the name String to the Histogram.
pankaj390abbc2014-10-01 17:01:05 -0700278 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700279 @Override
280 public Map<String, Histogram> getHistograms(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700281 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 Radoslavov35592492014-10-21 21:49:58 -0700289 @Override
290 public void removeMatching(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700291 metricsRegistry.removeMatching(filter);
292 }
Flavio Castro4b519412015-07-24 12:57:59 -0700293
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 }
pankaj390abbc2014-10-01 17:01:05 -0700304}