MetricsManager.java
10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright 2014-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onlab.metrics;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
/**
* This class holds the Metrics registry for ONOS.
* All metrics (Counter, Histogram, Timer, Meter, Gauge) use a hierarchical
* string-based naming scheme: COMPONENT.FEATURE.NAME.
* Example: "Topology.Counters.TopologyUpdates".
* The COMPONENT and FEATURE names have to be registered in advance before
* a metric can be created. Example:
* <pre>
* <code>
* private final MetricsManager.MetricsComponent COMPONENT =
* MetricsManager.registerComponent("Topology");
* private final MetricsManager.MetricsFeature FEATURE =
* COMPONENT.registerFeature("Counters");
* private final Counter counterTopologyUpdates =
* MetricsManager.createCounter(COMPONENT, FEATURE, "TopologyUpdates");
* </code>
* </pre>
* Gauges are slightly different because they are not created directly in
* this class, but are allocated by the caller and passed in for registration:
* <pre>
* <code>
* private final Gauge<Long> gauge =
* new {@literal Gauge<Long>}() {
* {@literal @}Override
* public Long getValue() {
* return gaugeValue;
* }
* };
* MetricsManager.registerMetric(COMPONENT, FEATURE, GAUGE_NAME, gauge);
* </code>
* </pre>
*/
public class MetricsManager implements MetricsService {
/**
* Registry to hold the Components defined in the system.
*/
private ConcurrentMap<String, MetricsComponent> componentsRegistry =
new ConcurrentHashMap<>();
/**
* Registry for the Metrics objects created in the system.
*/
private MetricRegistry metricsRegistry = new MetricRegistry();
/**
* Clears the internal state.
*/
protected void clear() {
this.componentsRegistry = new ConcurrentHashMap<>();
this.metricsRegistry = new MetricRegistry();
}
/**
* Registers a component.
*
* @param name name of the Component to register
* @return MetricsComponent object that can be used to create Metrics.
*/
@Override
public MetricsComponent registerComponent(final String name) {
MetricsComponent component = componentsRegistry.get(name);
if (component == null) {
final MetricsComponent createdComponent =
new MetricsComponent(name);
component = componentsRegistry.putIfAbsent(name, createdComponent);
if (component == null) {
component = createdComponent;
}
}
return component;
}
/**
* Fetches existing metric registry.
*
* @return metric registry
*/
@Override
public MetricRegistry getMetricRegistry() {
return metricsRegistry;
}
/**
* Generates a name for a Metric from its component and feature.
*
* @param component component the metric is defined in
* @param feature feature the metric is defined in
* @param metricName local name of the metric
*
* @return full name of the metric
*/
private String generateName(final MetricsComponent component,
final MetricsFeature feature,
final String metricName) {
return MetricRegistry.name(component.getName(),
feature.getName(),
metricName);
}
/**
* Creates a Counter metric.
*
* @param component component the Counter is defined in
* @param feature feature the Counter is defined in
* @param metricName local name of the metric
* @return the created Counter Meteric
*/
@Override
public Counter createCounter(final MetricsComponent component,
final MetricsFeature feature,
final String metricName) {
final String name = generateName(component, feature, metricName);
return metricsRegistry.counter(name);
}
/**
* Creates a Histogram metric.
*
* @param component component the Histogram is defined in
* @param feature feature the Histogram is defined in
* @param metricName local name of the metric
* @return the created Histogram Metric
*/
@Override
public Histogram createHistogram(final MetricsComponent component,
final MetricsFeature feature,
final String metricName) {
final String name = generateName(component, feature, metricName);
return metricsRegistry.histogram(name);
}
/**
* Creates a Timer metric.
*
* @param component component the Timer is defined in
* @param feature feature the Timer is defined in
* @param metricName local name of the metric
* @return the created Timer Metric
*/
@Override
public Timer createTimer(final MetricsComponent component,
final MetricsFeature feature,
final String metricName) {
final String name = generateName(component, feature, metricName);
return metricsRegistry.timer(name);
}
/**
* Creates a Meter metric.
*
* @param component component the Meter is defined in
* @param feature feature the Meter is defined in
* @param metricName local name of the metric
* @return the created Meter Metric
*/
@Override
public Meter createMeter(final MetricsComponent component,
final MetricsFeature feature,
final String metricName) {
final String name = generateName(component, feature, metricName);
return metricsRegistry.meter(name);
}
/**
* Registers an already created Metric. This is used for situation where a
* caller needs to allocate its own Metric, but still register it with the
* system.
*
* @param <T> Metric type
* @param component component the Metric is defined in
* @param feature feature the Metric is defined in
* @param metricName local name of the metric
* @param metric Metric to register
* @return the registered Metric
*/
@Override
public <T extends Metric> T registerMetric(
final MetricsComponent component,
final MetricsFeature feature,
final String metricName,
final T metric) {
final String name = generateName(component, feature, metricName);
metricsRegistry.register(name, metric);
return metric;
}
/**
* Removes the metric with the given name.
*
* @param component component the Metric is defined in
* @param feature feature the Metric is defined in
* @param metricName local name of the metric
* @return true if the metric existed and was removed, otherwise false
*/
@Override
public boolean removeMetric(final MetricsComponent component,
final MetricsFeature feature,
final String metricName) {
final String name = generateName(component, feature, metricName);
return metricsRegistry.remove(name);
}
/**
* Fetches the existing Timers.
*
* @param filter filter to use to select Timers
* @return a map of the Timers that match the filter, with the key as the
* name String to the Timer.
*/
@Override
public Map<String, Timer> getTimers(final MetricFilter filter) {
return metricsRegistry.getTimers(filter);
}
/**
* Fetches the existing Gauges.
*
* @param filter filter to use to select Gauges
* @return a map of the Gauges that match the filter, with the key as the
* name String to the Gauge.
*/
@Override
public Map<String, Gauge> getGauges(final MetricFilter filter) {
return metricsRegistry.getGauges(filter);
}
/**
* Fetches the existing Counters.
*
* @param filter filter to use to select Counters
* @return a map of the Counters that match the filter, with the key as the
* name String to the Counter.
*/
@Override
public Map<String, Counter> getCounters(final MetricFilter filter) {
return metricsRegistry.getCounters(filter);
}
/**
* Fetches the existing Meters.
*
* @param filter filter to use to select Meters
* @return a map of the Meters that match the filter, with the key as the
* name String to the Meter.
*/
@Override
public Map<String, Meter> getMeters(final MetricFilter filter) {
return metricsRegistry.getMeters(filter);
}
/**
* Fetches the existing Histograms.
*
* @param filter filter to use to select Histograms
* @return a map of the Histograms that match the filter, with the key as
* the name String to the Histogram.
*/
@Override
public Map<String, Histogram> getHistograms(final MetricFilter filter) {
return metricsRegistry.getHistograms(filter);
}
/**
* Removes all Metrics that match a given filter.
*
* @param filter filter to use to select the Metrics to remove.
*/
@Override
public void removeMatching(final MetricFilter filter) {
metricsRegistry.removeMatching(filter);
}
/**
* Fetches the existing Meters.
*
*
* @return a map of all metrics with the key as the
* name String to the Meter.
*/
public Map<String, Metric> getMetrics() {
return metricsRegistry.getMetrics();
}
}