tom

Merge remote-tracking branch 'origin/master'

...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
11 <bundle>mvn:io.netty/netty/3.9.2.Final</bundle> 11 <bundle>mvn:io.netty/netty/3.9.2.Final</bundle>
12 12
13 <bundle>mvn:com.hazelcast/hazelcast/3.3</bundle> 13 <bundle>mvn:com.hazelcast/hazelcast/3.3</bundle>
14 + <bundle>mvn:com.codahale.metrics/metrics-core/3.0.2</bundle>
14 <bundle>mvn:com.eclipsesource.minimal-json/minimal-json/0.9.1</bundle> 15 <bundle>mvn:com.eclipsesource.minimal-json/minimal-json/0.9.1</bundle>
15 16
16 <bundle>mvn:com.esotericsoftware.kryo/kryo/2.24.0</bundle> 17 <bundle>mvn:com.esotericsoftware.kryo/kryo/2.24.0</bundle>
......
...@@ -55,6 +55,11 @@ ...@@ -55,6 +55,11 @@
55 <groupId>org.objenesis</groupId> 55 <groupId>org.objenesis</groupId>
56 <artifactId>objenesis</artifactId> 56 <artifactId>objenesis</artifactId>
57 </dependency> 57 </dependency>
58 + <dependency>
59 + <groupId>com.codahale.metrics</groupId>
60 + <artifactId>metrics-core</artifactId>
61 + <version>3.0.2</version>
62 + </dependency>
58 </dependencies> 63 </dependencies>
59 64
60 </project> 65 </project>
......
1 +package org.onlab.metrics;
2 +
3 +import java.util.concurrent.ConcurrentHashMap;
4 +import java.util.concurrent.ConcurrentMap;
5 +
6 +/**
7 + * Components to register for metrics.
8 + */
9 +public class MetricsComponent implements MetricsComponentRegistry {
10 + private final String name;
11 +
12 + /**
13 + * Registry to hold the Features defined in this Component.
14 + */
15 + private final ConcurrentMap<String, MetricsFeature> featuresRegistry =
16 + new ConcurrentHashMap<>();
17 +
18 + /**
19 + * Constructs a component from a name.
20 + *
21 + * @param newName name of the component
22 + */
23 + MetricsComponent(final String newName) {
24 + name = newName;
25 + }
26 +
27 + @Override public String getName() {
28 + return name;
29 + }
30 +
31 + @Override public MetricsFeature registerFeature(final String featureName) {
32 + MetricsFeature feature = featuresRegistry.get(featureName);
33 + if (feature == null) {
34 + final MetricsFeature createdFeature = new MetricsFeature(featureName);
35 + feature = featuresRegistry.putIfAbsent(featureName, createdFeature);
36 + if (feature == null) {
37 + feature = createdFeature;
38 + }
39 + }
40 + return feature;
41 + }
42 +}
1 +package org.onlab.metrics;
2 +
3 +/**
4 + * Registry Entry for Metrics Components.
5 + */
6 +public interface MetricsComponentRegistry {
7 + String getName();
8 +
9 + MetricsFeature registerFeature(String featureName);
10 +}
1 +package org.onlab.metrics;
2 +
3 +/**
4 + * Features to tag metrics.
5 + */
6 +public class MetricsFeature {
7 + private final String name;
8 +
9 + /**
10 + * Constructs a Feature from a name.
11 + *
12 + * @param newName name of the Feature
13 + */
14 + MetricsFeature(final String newName) {
15 + name = newName;
16 + }
17 +
18 + public String getName() {
19 + return name;
20 + }
21 +}
1 +package org.onlab.metrics;
2 +
3 +import java.util.Map;
4 +import java.util.concurrent.ConcurrentHashMap;
5 +import java.util.concurrent.ConcurrentMap;
6 +
7 +import com.codahale.metrics.Counter;
8 +import com.codahale.metrics.Gauge;
9 +import com.codahale.metrics.Histogram;
10 +import com.codahale.metrics.Meter;
11 +import com.codahale.metrics.Metric;
12 +import com.codahale.metrics.MetricFilter;
13 +import com.codahale.metrics.MetricRegistry;
14 +import 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 + */
48 +public final class MetricsManager implements MetricsService {
49 +
50 + /**
51 + * Registry to hold the Components defined in the system.
52 + */
53 + private ConcurrentMap<String, MetricsComponent> componentsRegistry =
54 + new ConcurrentHashMap<>();
55 +
56 + /**
57 + * Registry for the Metrics objects created in the system.
58 + */
59 + private final MetricRegistry metricsRegistry = new MetricRegistry();
60 +
61 + /**
62 + * Hide constructor. The only way to get the registry is through the
63 + * singleton getter.
64 + */
65 + private MetricsManager() {}
66 +
67 + /**
68 + * Registers a component.
69 + *
70 + * @param name name of the Component to register
71 + * @return MetricsComponent object that can be used to create Metrics.
72 + */
73 + public MetricsComponent registerComponent(final String name) {
74 + MetricsComponent component = componentsRegistry.get(name);
75 + if (component == null) {
76 + final MetricsComponent createdComponent = new MetricsComponent(name);
77 + component = componentsRegistry.putIfAbsent(name, createdComponent);
78 + if (component == null) {
79 + component = createdComponent;
80 + }
81 + }
82 + return component;
83 + }
84 +
85 + /**
86 + * Generates a name for a Metric from its component and feature.
87 + *
88 + * @param component component the metric is defined in
89 + * @param feature feature the metric is defined in
90 + * @param metricName local name of the metric
91 + *
92 + * @return full name of the metric
93 + */
94 + public String generateName(final MetricsComponent component,
95 + final MetricsFeature feature,
96 + final String metricName) {
97 + return MetricRegistry.name(component.getName(),
98 + feature.getName(),
99 + metricName);
100 + }
101 +
102 + /**
103 + * Creates a Counter metric.
104 + *
105 + * @param component component the Counter is defined in
106 + * @param feature feature the Counter is defined in
107 + * @param metricName local name of the metric
108 + * @return the created Counter Meteric
109 + */
110 + public Counter createCounter(final MetricsComponent component,
111 + final MetricsFeature feature,
112 + final String metricName) {
113 + final String name = generateName(component, feature, metricName);
114 + return metricsRegistry.counter(name);
115 + }
116 +
117 + /**
118 + * Creates a Histogram metric.
119 + *
120 + * @param component component the Histogram is defined in
121 + * @param feature feature the Histogram is defined in
122 + * @param metricName local name of the metric
123 + * @return the created Histogram Metric
124 + */
125 + public Histogram createHistogram(final MetricsComponent component,
126 + final MetricsFeature feature,
127 + final String metricName) {
128 + final String name = generateName(component, feature, metricName);
129 + return metricsRegistry.histogram(name);
130 + }
131 +
132 + /**
133 + * Creates a Timer metric.
134 + *
135 + * @param component component the Timer is defined in
136 + * @param feature feature the Timeer is defined in
137 + * @param metricName local name of the metric
138 + * @return the created Timer Metric
139 + */
140 + public Timer createTimer(final MetricsComponent component,
141 + final MetricsFeature feature,
142 + final String metricName) {
143 + final String name = generateName(component, feature, metricName);
144 + return metricsRegistry.timer(name);
145 + }
146 +
147 + /**
148 + * Creates a Meter metric.
149 + *
150 + * @param component component the Meter is defined in
151 + * @param feature feature the Meter is defined in
152 + * @param metricName local name of the metric
153 + * @return the created Meter Metric
154 + */
155 + public Meter createMeter(final MetricsComponent component,
156 + final MetricsFeature feature,
157 + final String metricName) {
158 + final String name = generateName(component, feature, metricName);
159 + return metricsRegistry.meter(name);
160 + }
161 +
162 + /**
163 + * Registers an already created Metric. This is used for situation where a
164 + * caller needs to allocate its own Metric, but still register it with the
165 + * system.
166 + *
167 + * @param <T> Metric type
168 + * @param component component the Metric is defined in
169 + * @param feature feature the Metric is defined in
170 + * @param metricName local name of the metric
171 + * @param metric Metric to register
172 + * @return the registered Metric
173 + */
174 + public <T extends Metric> T registerMetric(
175 + final MetricsComponent component,
176 + final MetricsFeature feature,
177 + final String metricName,
178 + final T metric) {
179 + final String name = generateName(component, feature, metricName);
180 + metricsRegistry.register(name, metric);
181 + return metric;
182 + }
183 +
184 + /**
185 + * Fetches the existing Timers.
186 + *
187 + * @param filter filter to use to select Timers
188 + * @return a map of the Timers that match the filter, with the key as the
189 + * name String to the Timer.
190 + */
191 + public Map<String, Timer> getTimers(final MetricFilter filter) {
192 + return metricsRegistry.getTimers(filter);
193 + }
194 +
195 + /**
196 + * Fetches the existing Gauges.
197 + *
198 + * @param filter filter to use to select Gauges
199 + * @return a map of the Gauges that match the filter, with the key as the
200 + * name String to the Gauge.
201 + */
202 + @SuppressWarnings("rawtypes")
203 + public Map<String, Gauge> getGauges(final MetricFilter filter) {
204 + return metricsRegistry.getGauges(filter);
205 + }
206 +
207 + /**
208 + * Fetches the existing Counters.
209 + *
210 + * @param filter filter to use to select Counters
211 + * @return a map of the Counters that match the filter, with the key as the
212 + * name String to the Counter.
213 + */
214 + public Map<String, Counter> getCounters(final MetricFilter filter) {
215 + return metricsRegistry.getCounters(filter);
216 + }
217 +
218 + /**
219 + * Fetches the existing Meters.
220 + *
221 + * @param filter filter to use to select Meters
222 + * @return a map of the Meters that match the filter, with the key as the
223 + * name String to the Meter.
224 + */
225 + public Map<String, Meter> getMeters(final MetricFilter filter) {
226 + return metricsRegistry.getMeters(filter);
227 + }
228 +
229 + /**
230 + * Fetches the existing Histograms.
231 + *
232 + * @param filter filter to use to select Histograms
233 + * @return a map of the Histograms that match the filter, with the key as the
234 + * name String to the Histogram.
235 + */
236 + public Map<String, Histogram> getHistograms(final MetricFilter filter) {
237 + return metricsRegistry.getHistograms(filter);
238 + }
239 +
240 + /**
241 + * Removes all Metrics that match a given filter.
242 + *
243 + * @param filter filter to use to select the Metrics to remove.
244 + */
245 + public void removeMatching(final MetricFilter filter) {
246 + metricsRegistry.removeMatching(filter);
247 + }
248 +}
249 +
1 +package org.onlab.metrics;
2 +
3 +import java.util.Map;
4 +
5 +import com.codahale.metrics.Counter;
6 +import com.codahale.metrics.Gauge;
7 +import com.codahale.metrics.Histogram;
8 +import com.codahale.metrics.Meter;
9 +import com.codahale.metrics.Metric;
10 +import com.codahale.metrics.MetricFilter;
11 +import com.codahale.metrics.Timer;
12 +
13 +/**
14 + * Metrics Service to collect metrics.
15 + */
16 +interface MetricsService {
17 +
18 + /**
19 + * Registers a component.
20 + *
21 + * @param name name of the Component to register
22 + * @return MetricsComponent object that can be used to create Metrics.
23 + */
24 + MetricsComponent registerComponent(String name);
25 +
26 + /**
27 + * Creates a Counter metric.
28 + *
29 + * @param component component the Counter is defined in
30 + * @param feature feature the Counter is defined in
31 + * @param metricName local name of the metric
32 + * @return the created Counter Meteric
33 + */
34 + Counter createCounter(MetricsComponent component,
35 + MetricsFeature feature,
36 + String metricName);
37 +
38 + /**
39 + * Creates a Histogram metric.
40 + *
41 + * @param component component the Histogram is defined in
42 + * @param feature feature the Histogram is defined in
43 + * @param metricName local name of the metric
44 + * @return the created Histogram Metric
45 + */
46 + Histogram createHistogram(MetricsComponent component,
47 + MetricsFeature feature,
48 + String metricName);
49 +
50 + /**
51 + * Creates a Timer metric.
52 + *
53 + * @param component component the Timer is defined in
54 + * @param feature feature the Timer is defined in
55 + * @param metricName local name of the metric
56 + * @return the created Timer Metric
57 + */
58 + Timer createTimer(MetricsComponent component,
59 + MetricsFeature feature,
60 + String metricName);
61 +
62 + /**
63 + * Registers an already created Metric. This is used for situation where a
64 + * caller needs to allocate its own Metric, but still register it with the
65 + * system.
66 + *
67 + * @param <T> Metric type
68 + * @param component component the Metric is defined in
69 + * @param feature feature the Metric is defined in
70 + * @param metricName local name of the metric
71 + * @param metric Metric to register
72 + * @return the registered Metric
73 + */
74 + <T extends Metric> T registerMetric(
75 + MetricsComponent component,
76 + MetricsFeature feature,
77 + String metricName,
78 + T metric);
79 +
80 + /**
81 + * Fetches the existing Timers.
82 + *
83 + * @param filter filter to use to select Timers
84 + * @return a map of the Timers that match the filter, with the key as the
85 + * name String to the Timer.
86 + */
87 + Map<String, Timer> getTimers(MetricFilter filter);
88 +
89 + /**
90 + * Fetches the existing Gauges.
91 + *
92 + * @param filter filter to use to select Gauges
93 + * @return a map of the Gauges that match the filter, with the key as the
94 + * name String to the Gauge.
95 + */
96 + Map<String, Gauge> getGauges(MetricFilter filter);
97 +
98 + /**
99 + * Fetches the existing Counters.
100 + *
101 + * @param filter filter to use to select Counters
102 + * @return a map of the Counters that match the filter, with the key as the
103 + * name String to the Counter.
104 + */
105 + Map<String, Counter> getCounters(MetricFilter filter);
106 +
107 + /**
108 + * Fetches the existing Meters.
109 + *
110 + * @param filter filter to use to select Meters
111 + * @return a map of the Meters that match the filter, with the key as the
112 + * name String to the Meter.
113 + */
114 + Map<String, Meter> getMeters(MetricFilter filter);
115 +
116 + /**
117 + * Fetches the existing Histograms.
118 + *
119 + * @param filter filter to use to select Histograms
120 + * @return a map of the Histograms that match the filter, with the key as the
121 + * name String to the Histogram.
122 + */
123 + Map<String, Histogram> getHistograms(MetricFilter filter);
124 + /**
125 + * Removes all Metrics that match a given filter.
126 + *
127 + * @param filter filter to use to select the Metrics to remove.
128 + */
129 + void removeMatching(MetricFilter filter);
130 +
131 +}
1 +/**
2 + * Misc utils for various performance metrics.
3 + */
4 +package org.onlab.metrics;