blob: 8d5f919711eb3639e1f3e0a8258e40f6d6de10a1 [file] [log] [blame]
pankaj390abbc2014-10-01 17:01:05 -07001package org.onlab.metrics;
2
3import java.util.Map;
pankaj5de84112014-10-02 15:33:28 -07004import java.util.concurrent.ConcurrentHashMap;
pankaj390abbc2014-10-01 17:01:05 -07005import java.util.concurrent.ConcurrentMap;
6
7import com.codahale.metrics.Counter;
8import com.codahale.metrics.Gauge;
9import com.codahale.metrics.Histogram;
10import com.codahale.metrics.Meter;
11import com.codahale.metrics.Metric;
12import com.codahale.metrics.MetricFilter;
13import com.codahale.metrics.MetricRegistry;
14import com.codahale.metrics.Timer;
15
16/**
17 * This class holds the Metrics registry for ONOS.
18 * All metrics (Counter, Histogram, Timer, Meter, Gauge) use a hierarchical
19 * string-based naming scheme: COMPONENT.FEATURE.NAME.
20 * Example: "Topology.Counters.TopologyUpdates".
21 * The COMPONENT and FEATURE names have to be registered in advance before
22 * a metric can be created. Example:
23 * <pre>
24 * <code>
25 * private final MetricsManager.MetricsComponent COMPONENT =
26 * MetricsManager.registerComponent("Topology");
27 * private final MetricsManager.MetricsFeature FEATURE =
28 * COMPONENT.registerFeature("Counters");
29 * private final Counter counterTopologyUpdates =
30 * MetricsManager.createCounter(COMPONENT, FEATURE, "TopologyUpdates");
31 * </code>
32 * </pre>
33 * Gauges are slightly different because they are not created directly in
34 * this class, but are allocated by the caller and passed in for registration:
35 * <pre>
36 * <code>
37 * private final Gauge<Long> gauge =
38 * new {@literal Gauge<Long>}() {
39 * {@literal @}Override
40 * public Long getValue() {
41 * return gaugeValue;
42 * }
43 * };
44 * MetricsManager.registerMetric(COMPONENT, FEATURE, GAUGE_NAME, gauge);
45 * </code>
46 * </pre>
47 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -070048public class MetricsManager implements MetricsService {
pankaj390abbc2014-10-01 17:01:05 -070049
50 /**
51 * Registry to hold the Components defined in the system.
52 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -070053 private ConcurrentMap<String, MetricsComponent> componentsRegistry =
54 new ConcurrentHashMap<>();
pankaj390abbc2014-10-01 17:01:05 -070055
56 /**
57 * Registry for the Metrics objects created in the system.
58 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -070059 private MetricRegistry metricsRegistry = new MetricRegistry();
pankaj390abbc2014-10-01 17:01:05 -070060
pankaj5de84112014-10-02 15:33:28 -070061 /**
Pavlin Radoslavov35592492014-10-21 21:49:58 -070062 * Clears the internal state.
pankaj5de84112014-10-02 15:33:28 -070063 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -070064 protected void clear() {
pankaj9e34ff22014-10-06 13:21:03 -070065 this.componentsRegistry = new ConcurrentHashMap<>();
Pavlin Radoslavov35592492014-10-21 21:49:58 -070066 this.metricsRegistry = new MetricRegistry();
pankajf6577b62014-10-02 16:38:38 -070067 }
68
pankaj390abbc2014-10-01 17:01:05 -070069 /**
70 * Registers a component.
71 *
72 * @param name name of the Component to register
73 * @return MetricsComponent object that can be used to create Metrics.
74 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -070075 @Override
76 public MetricsComponent registerComponent(final String name) {
pankaj390abbc2014-10-01 17:01:05 -070077 MetricsComponent component = componentsRegistry.get(name);
78 if (component == null) {
Pavlin Radoslavov35592492014-10-21 21:49:58 -070079 final MetricsComponent createdComponent =
80 new MetricsComponent(name);
pankaj390abbc2014-10-01 17:01:05 -070081 component = componentsRegistry.putIfAbsent(name, createdComponent);
82 if (component == null) {
83 component = createdComponent;
84 }
85 }
86 return component;
87 }
88
89 /**
90 * Generates a name for a Metric from its component and feature.
91 *
92 * @param component component the metric is defined in
93 * @param feature feature the metric is defined in
94 * @param metricName local name of the metric
95 *
96 * @return full name of the metric
97 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -070098 private String generateName(final MetricsComponent component,
99 final MetricsFeature feature,
100 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700101 return MetricRegistry.name(component.getName(),
102 feature.getName(),
103 metricName);
104 }
105
106 /**
107 * Creates a Counter metric.
108 *
109 * @param component component the Counter is defined in
110 * @param feature feature the Counter is defined in
111 * @param metricName local name of the metric
112 * @return the created Counter Meteric
113 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700114 @Override
115 public Counter createCounter(final MetricsComponent component,
116 final MetricsFeature feature,
117 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700118 final String name = generateName(component, feature, metricName);
119 return metricsRegistry.counter(name);
120 }
121
122 /**
123 * Creates a Histogram metric.
124 *
125 * @param component component the Histogram is defined in
126 * @param feature feature the Histogram is defined in
127 * @param metricName local name of the metric
128 * @return the created Histogram Metric
129 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700130 @Override
131 public Histogram createHistogram(final MetricsComponent component,
132 final MetricsFeature feature,
133 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700134 final String name = generateName(component, feature, metricName);
135 return metricsRegistry.histogram(name);
136 }
137
138 /**
139 * Creates a Timer metric.
140 *
141 * @param component component the Timer is defined in
142 * @param feature feature the Timeer is defined in
143 * @param metricName local name of the metric
144 * @return the created Timer Metric
145 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700146 @Override
147 public Timer createTimer(final MetricsComponent component,
148 final MetricsFeature feature,
149 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700150 final String name = generateName(component, feature, metricName);
151 return metricsRegistry.timer(name);
152 }
153
154 /**
155 * Creates a Meter metric.
156 *
157 * @param component component the Meter is defined in
158 * @param feature feature the Meter is defined in
159 * @param metricName local name of the metric
160 * @return the created Meter Metric
161 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700162 @Override
163 public Meter createMeter(final MetricsComponent component,
164 final MetricsFeature feature,
165 final String metricName) {
pankaj390abbc2014-10-01 17:01:05 -0700166 final String name = generateName(component, feature, metricName);
167 return metricsRegistry.meter(name);
168 }
169
170 /**
171 * Registers an already created Metric. This is used for situation where a
172 * caller needs to allocate its own Metric, but still register it with the
173 * system.
174 *
175 * @param <T> Metric type
176 * @param component component the Metric is defined in
177 * @param feature feature the Metric is defined in
178 * @param metricName local name of the metric
179 * @param metric Metric to register
180 * @return the registered Metric
181 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700182 @Override
183 public <T extends Metric> T registerMetric(
pankaj390abbc2014-10-01 17:01:05 -0700184 final MetricsComponent component,
185 final MetricsFeature feature,
186 final String metricName,
187 final T metric) {
188 final String name = generateName(component, feature, metricName);
189 metricsRegistry.register(name, metric);
190 return metric;
191 }
192
193 /**
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700194 * Removes the metric with the given name.
195 *
196 * @param component component the Metric is defined in
197 * @param feature feature the Metric is defined in
198 * @param metricName local name of the metric
199 * @return true if the metric existed and was removed, otherwise false
200 */
201 @Override
202 public boolean removeMetric(final MetricsComponent component,
203 final MetricsFeature feature,
204 final String metricName) {
205 final String name = generateName(component, feature, metricName);
206 return metricsRegistry.remove(name);
207 }
208
209 /**
pankaj390abbc2014-10-01 17:01:05 -0700210 * Fetches the existing Timers.
211 *
212 * @param filter filter to use to select Timers
213 * @return a map of the Timers that match the filter, with the key as the
214 * name String to the Timer.
215 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700216 @Override
217 public Map<String, Timer> getTimers(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700218 return metricsRegistry.getTimers(filter);
219 }
220
221 /**
222 * Fetches the existing Gauges.
223 *
224 * @param filter filter to use to select Gauges
225 * @return a map of the Gauges that match the filter, with the key as the
226 * name String to the Gauge.
227 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700228 @Override
229 public Map<String, Gauge> getGauges(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700230 return metricsRegistry.getGauges(filter);
231 }
232
233 /**
234 * Fetches the existing Counters.
235 *
236 * @param filter filter to use to select Counters
237 * @return a map of the Counters that match the filter, with the key as the
238 * name String to the Counter.
239 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700240 @Override
241 public Map<String, Counter> getCounters(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700242 return metricsRegistry.getCounters(filter);
243 }
244
245 /**
246 * Fetches the existing Meters.
247 *
248 * @param filter filter to use to select Meters
249 * @return a map of the Meters that match the filter, with the key as the
250 * name String to the Meter.
251 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700252 @Override
253 public Map<String, Meter> getMeters(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700254 return metricsRegistry.getMeters(filter);
255 }
256
257 /**
258 * Fetches the existing Histograms.
259 *
260 * @param filter filter to use to select Histograms
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700261 * @return a map of the Histograms that match the filter, with the key as
262 * the name String to the Histogram.
pankaj390abbc2014-10-01 17:01:05 -0700263 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700264 @Override
265 public Map<String, Histogram> getHistograms(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700266 return metricsRegistry.getHistograms(filter);
267 }
268
269 /**
270 * Removes all Metrics that match a given filter.
271 *
272 * @param filter filter to use to select the Metrics to remove.
273 */
Pavlin Radoslavov35592492014-10-21 21:49:58 -0700274 @Override
275 public void removeMatching(final MetricFilter filter) {
pankaj390abbc2014-10-01 17:01:05 -0700276 metricsRegistry.removeMatching(filter);
277 }
278}