Refactoring and cleanup in the Metrics module:
* Removed logging related code inside class MetricsManager * Removed @Component related code, because it is not suppose to be used as an component. * Added a new class-wrapper so the Metrics can be used as a loadable service: MetricsManagerComponent. The name and the location of this class will be refactored in the future. * Added new method MetricsManager.removeMetric() * Line formatting * Changed "interface MetricsService" to public
Showing
4 changed files
with
104 additions
and
83 deletions
... | @@ -68,7 +68,6 @@ private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class); | ... | @@ -68,7 +68,6 @@ private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class); |
68 | metrics = new MetricsManager(); | 68 | metrics = new MetricsManager(); |
69 | Endpoint endpoint = new Endpoint(host, port); | 69 | Endpoint endpoint = new Endpoint(host, port); |
70 | messaging.activate(); | 70 | messaging.activate(); |
71 | - metrics.activate(); | ||
72 | MetricsFeature feature = new MetricsFeature("latency"); | 71 | MetricsFeature feature = new MetricsFeature("latency"); |
73 | MetricsComponent component = metrics.registerComponent("NettyMessaging"); | 72 | MetricsComponent component = metrics.registerComponent("NettyMessaging"); |
74 | log.info("connecting " + host + ":" + port + " warmup:" + warmup + " iterations:" + iterations); | 73 | log.info("connecting " + host + ":" + port + " warmup:" + warmup + " iterations:" + iterations); |
... | @@ -117,7 +116,6 @@ private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class); | ... | @@ -117,7 +116,6 @@ private static Logger log = LoggerFactory.getLogger(SimpleNettyClient.class); |
117 | public static void stop() { | 116 | public static void stop() { |
118 | try { | 117 | try { |
119 | messaging.deactivate(); | 118 | messaging.deactivate(); |
120 | - metrics.deactivate(); | ||
121 | } catch (Exception e) { | 119 | } catch (Exception e) { |
122 | log.info("Unable to stop client %s", e); | 120 | log.info("Unable to stop client %s", e); |
123 | } | 121 | } | ... | ... |
1 | +package org.onlab.onos.impl; | ||
2 | + | ||
3 | +import org.apache.felix.scr.annotations.Activate; | ||
4 | +import org.apache.felix.scr.annotations.Component; | ||
5 | +import org.apache.felix.scr.annotations.Deactivate; | ||
6 | +import org.apache.felix.scr.annotations.Service; | ||
7 | + | ||
8 | +import org.onlab.metrics.MetricsManager; | ||
9 | + | ||
10 | +/** | ||
11 | + * Metrics service implementation. | ||
12 | + */ | ||
13 | +@Component(immediate = true) | ||
14 | +@Service | ||
15 | +public class MetricsManagerComponent extends MetricsManager { | ||
16 | + | ||
17 | + @Activate | ||
18 | + protected void activate() { | ||
19 | + super.clear(); | ||
20 | + } | ||
21 | + | ||
22 | + @Deactivate | ||
23 | + protected void deactivate() { | ||
24 | + super.clear(); | ||
25 | + } | ||
26 | +} |
... | @@ -3,15 +3,7 @@ package org.onlab.metrics; | ... | @@ -3,15 +3,7 @@ package org.onlab.metrics; |
3 | import java.util.Map; | 3 | import java.util.Map; |
4 | import java.util.concurrent.ConcurrentHashMap; | 4 | import java.util.concurrent.ConcurrentHashMap; |
5 | import java.util.concurrent.ConcurrentMap; | 5 | import java.util.concurrent.ConcurrentMap; |
6 | -import java.util.concurrent.TimeUnit; | ||
7 | 6 | ||
8 | -import org.apache.felix.scr.annotations.Activate; | ||
9 | -import org.apache.felix.scr.annotations.Component; | ||
10 | -import org.apache.felix.scr.annotations.Deactivate; | ||
11 | -import org.slf4j.Logger; | ||
12 | -import org.slf4j.LoggerFactory; | ||
13 | - | ||
14 | -import com.codahale.metrics.ConsoleReporter; | ||
15 | import com.codahale.metrics.Counter; | 7 | import com.codahale.metrics.Counter; |
16 | import com.codahale.metrics.Gauge; | 8 | import com.codahale.metrics.Gauge; |
17 | import com.codahale.metrics.Histogram; | 9 | import com.codahale.metrics.Histogram; |
... | @@ -53,48 +45,25 @@ import com.codahale.metrics.Timer; | ... | @@ -53,48 +45,25 @@ import com.codahale.metrics.Timer; |
53 | * </code> | 45 | * </code> |
54 | * </pre> | 46 | * </pre> |
55 | */ | 47 | */ |
56 | -@Component(immediate = true) | 48 | +public class MetricsManager implements MetricsService { |
57 | -public final class MetricsManager implements MetricsService { | ||
58 | 49 | ||
59 | - private final Logger log = LoggerFactory.getLogger(getClass()); | ||
60 | /** | 50 | /** |
61 | * Registry to hold the Components defined in the system. | 51 | * Registry to hold the Components defined in the system. |
62 | */ | 52 | */ |
63 | - private ConcurrentMap<String, MetricsComponent> componentsRegistry; | 53 | + private ConcurrentMap<String, MetricsComponent> componentsRegistry = |
54 | + new ConcurrentHashMap<>(); | ||
64 | 55 | ||
65 | /** | 56 | /** |
66 | * Registry for the Metrics objects created in the system. | 57 | * Registry for the Metrics objects created in the system. |
67 | */ | 58 | */ |
68 | - private final MetricRegistry metricsRegistry; | 59 | + private MetricRegistry metricsRegistry = new MetricRegistry(); |
69 | 60 | ||
70 | /** | 61 | /** |
71 | - * Default Reporter for this metrics manager. | 62 | + * Clears the internal state. |
72 | */ | 63 | */ |
73 | - //private final Slf4jReporter reporter; | 64 | + protected void clear() { |
74 | - private final ConsoleReporter reporter; | ||
75 | - | ||
76 | - public MetricsManager() { | ||
77 | - this.metricsRegistry = new MetricRegistry(); | ||
78 | -// this.reporter = Slf4jReporter.forRegistry(this.metricsRegistry) | ||
79 | -// .outputTo(log) | ||
80 | -// .convertRatesTo(TimeUnit.SECONDS) | ||
81 | -// .convertDurationsTo(TimeUnit.MICROSECONDS) | ||
82 | -// .build(); | ||
83 | - this.reporter = ConsoleReporter.forRegistry(this.metricsRegistry) | ||
84 | - .convertRatesTo(TimeUnit.SECONDS) | ||
85 | - .convertDurationsTo(TimeUnit.MICROSECONDS) | ||
86 | - .build(); | ||
87 | - } | ||
88 | - | ||
89 | - @Activate | ||
90 | - public void activate() { | ||
91 | this.componentsRegistry = new ConcurrentHashMap<>(); | 65 | this.componentsRegistry = new ConcurrentHashMap<>(); |
92 | - reporter.start(10, TimeUnit.SECONDS); | 66 | + this.metricsRegistry = new MetricRegistry(); |
93 | - } | ||
94 | - | ||
95 | - @Deactivate | ||
96 | - public void deactivate() { | ||
97 | - reporter.stop(); | ||
98 | } | 67 | } |
99 | 68 | ||
100 | /** | 69 | /** |
... | @@ -103,11 +72,12 @@ public final class MetricsManager implements MetricsService { | ... | @@ -103,11 +72,12 @@ public final class MetricsManager implements MetricsService { |
103 | * @param name name of the Component to register | 72 | * @param name name of the Component to register |
104 | * @return MetricsComponent object that can be used to create Metrics. | 73 | * @return MetricsComponent object that can be used to create Metrics. |
105 | */ | 74 | */ |
106 | - @Override | 75 | + @Override |
107 | - public MetricsComponent registerComponent(final String name) { | 76 | + public MetricsComponent registerComponent(final String name) { |
108 | MetricsComponent component = componentsRegistry.get(name); | 77 | MetricsComponent component = componentsRegistry.get(name); |
109 | if (component == null) { | 78 | if (component == null) { |
110 | - final MetricsComponent createdComponent = new MetricsComponent(name); | 79 | + final MetricsComponent createdComponent = |
80 | + new MetricsComponent(name); | ||
111 | component = componentsRegistry.putIfAbsent(name, createdComponent); | 81 | component = componentsRegistry.putIfAbsent(name, createdComponent); |
112 | if (component == null) { | 82 | if (component == null) { |
113 | component = createdComponent; | 83 | component = createdComponent; |
... | @@ -125,9 +95,9 @@ public final class MetricsManager implements MetricsService { | ... | @@ -125,9 +95,9 @@ public final class MetricsManager implements MetricsService { |
125 | * | 95 | * |
126 | * @return full name of the metric | 96 | * @return full name of the metric |
127 | */ | 97 | */ |
128 | - private String generateName(final MetricsComponent component, | 98 | + private String generateName(final MetricsComponent component, |
129 | - final MetricsFeature feature, | 99 | + final MetricsFeature feature, |
130 | - final String metricName) { | 100 | + final String metricName) { |
131 | return MetricRegistry.name(component.getName(), | 101 | return MetricRegistry.name(component.getName(), |
132 | feature.getName(), | 102 | feature.getName(), |
133 | metricName); | 103 | metricName); |
... | @@ -141,10 +111,10 @@ public final class MetricsManager implements MetricsService { | ... | @@ -141,10 +111,10 @@ public final class MetricsManager implements MetricsService { |
141 | * @param metricName local name of the metric | 111 | * @param metricName local name of the metric |
142 | * @return the created Counter Meteric | 112 | * @return the created Counter Meteric |
143 | */ | 113 | */ |
144 | - @Override | 114 | + @Override |
145 | - public Counter createCounter(final MetricsComponent component, | 115 | + public Counter createCounter(final MetricsComponent component, |
146 | - final MetricsFeature feature, | 116 | + final MetricsFeature feature, |
147 | - final String metricName) { | 117 | + final String metricName) { |
148 | final String name = generateName(component, feature, metricName); | 118 | final String name = generateName(component, feature, metricName); |
149 | return metricsRegistry.counter(name); | 119 | return metricsRegistry.counter(name); |
150 | } | 120 | } |
... | @@ -157,10 +127,10 @@ public final class MetricsManager implements MetricsService { | ... | @@ -157,10 +127,10 @@ public final class MetricsManager implements MetricsService { |
157 | * @param metricName local name of the metric | 127 | * @param metricName local name of the metric |
158 | * @return the created Histogram Metric | 128 | * @return the created Histogram Metric |
159 | */ | 129 | */ |
160 | - @Override | 130 | + @Override |
161 | - public Histogram createHistogram(final MetricsComponent component, | 131 | + public Histogram createHistogram(final MetricsComponent component, |
162 | - final MetricsFeature feature, | 132 | + final MetricsFeature feature, |
163 | - final String metricName) { | 133 | + final String metricName) { |
164 | final String name = generateName(component, feature, metricName); | 134 | final String name = generateName(component, feature, metricName); |
165 | return metricsRegistry.histogram(name); | 135 | return metricsRegistry.histogram(name); |
166 | } | 136 | } |
... | @@ -173,10 +143,10 @@ public final class MetricsManager implements MetricsService { | ... | @@ -173,10 +143,10 @@ public final class MetricsManager implements MetricsService { |
173 | * @param metricName local name of the metric | 143 | * @param metricName local name of the metric |
174 | * @return the created Timer Metric | 144 | * @return the created Timer Metric |
175 | */ | 145 | */ |
176 | - @Override | 146 | + @Override |
177 | - public Timer createTimer(final MetricsComponent component, | 147 | + public Timer createTimer(final MetricsComponent component, |
178 | - final MetricsFeature feature, | 148 | + final MetricsFeature feature, |
179 | - final String metricName) { | 149 | + final String metricName) { |
180 | final String name = generateName(component, feature, metricName); | 150 | final String name = generateName(component, feature, metricName); |
181 | return metricsRegistry.timer(name); | 151 | return metricsRegistry.timer(name); |
182 | } | 152 | } |
... | @@ -189,10 +159,10 @@ public final class MetricsManager implements MetricsService { | ... | @@ -189,10 +159,10 @@ public final class MetricsManager implements MetricsService { |
189 | * @param metricName local name of the metric | 159 | * @param metricName local name of the metric |
190 | * @return the created Meter Metric | 160 | * @return the created Meter Metric |
191 | */ | 161 | */ |
192 | - @Override | 162 | + @Override |
193 | - public Meter createMeter(final MetricsComponent component, | 163 | + public Meter createMeter(final MetricsComponent component, |
194 | - final MetricsFeature feature, | 164 | + final MetricsFeature feature, |
195 | - final String metricName) { | 165 | + final String metricName) { |
196 | final String name = generateName(component, feature, metricName); | 166 | final String name = generateName(component, feature, metricName); |
197 | return metricsRegistry.meter(name); | 167 | return metricsRegistry.meter(name); |
198 | } | 168 | } |
... | @@ -209,8 +179,8 @@ public final class MetricsManager implements MetricsService { | ... | @@ -209,8 +179,8 @@ public final class MetricsManager implements MetricsService { |
209 | * @param metric Metric to register | 179 | * @param metric Metric to register |
210 | * @return the registered Metric | 180 | * @return the registered Metric |
211 | */ | 181 | */ |
212 | - @Override | 182 | + @Override |
213 | - public <T extends Metric> T registerMetric( | 183 | + public <T extends Metric> T registerMetric( |
214 | final MetricsComponent component, | 184 | final MetricsComponent component, |
215 | final MetricsFeature feature, | 185 | final MetricsFeature feature, |
216 | final String metricName, | 186 | final String metricName, |
... | @@ -221,14 +191,30 @@ public final class MetricsManager implements MetricsService { | ... | @@ -221,14 +191,30 @@ public final class MetricsManager implements MetricsService { |
221 | } | 191 | } |
222 | 192 | ||
223 | /** | 193 | /** |
194 | + * 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 | + /** | ||
224 | * Fetches the existing Timers. | 210 | * Fetches the existing Timers. |
225 | * | 211 | * |
226 | * @param filter filter to use to select Timers | 212 | * @param filter filter to use to select Timers |
227 | * @return a map of the Timers that match the filter, with the key as the | 213 | * @return a map of the Timers that match the filter, with the key as the |
228 | * name String to the Timer. | 214 | * name String to the Timer. |
229 | */ | 215 | */ |
230 | - @Override | 216 | + @Override |
231 | - public Map<String, Timer> getTimers(final MetricFilter filter) { | 217 | + public Map<String, Timer> getTimers(final MetricFilter filter) { |
232 | return metricsRegistry.getTimers(filter); | 218 | return metricsRegistry.getTimers(filter); |
233 | } | 219 | } |
234 | 220 | ||
... | @@ -239,8 +225,8 @@ public final class MetricsManager implements MetricsService { | ... | @@ -239,8 +225,8 @@ public final class MetricsManager implements MetricsService { |
239 | * @return a map of the Gauges that match the filter, with the key as the | 225 | * @return a map of the Gauges that match the filter, with the key as the |
240 | * name String to the Gauge. | 226 | * name String to the Gauge. |
241 | */ | 227 | */ |
242 | - @Override | 228 | + @Override |
243 | - public Map<String, Gauge> getGauges(final MetricFilter filter) { | 229 | + public Map<String, Gauge> getGauges(final MetricFilter filter) { |
244 | return metricsRegistry.getGauges(filter); | 230 | return metricsRegistry.getGauges(filter); |
245 | } | 231 | } |
246 | 232 | ||
... | @@ -251,8 +237,8 @@ public final class MetricsManager implements MetricsService { | ... | @@ -251,8 +237,8 @@ public final class MetricsManager implements MetricsService { |
251 | * @return a map of the Counters that match the filter, with the key as the | 237 | * @return a map of the Counters that match the filter, with the key as the |
252 | * name String to the Counter. | 238 | * name String to the Counter. |
253 | */ | 239 | */ |
254 | - @Override | 240 | + @Override |
255 | - public Map<String, Counter> getCounters(final MetricFilter filter) { | 241 | + public Map<String, Counter> getCounters(final MetricFilter filter) { |
256 | return metricsRegistry.getCounters(filter); | 242 | return metricsRegistry.getCounters(filter); |
257 | } | 243 | } |
258 | 244 | ||
... | @@ -263,8 +249,8 @@ public final class MetricsManager implements MetricsService { | ... | @@ -263,8 +249,8 @@ public final class MetricsManager implements MetricsService { |
263 | * @return a map of the Meters that match the filter, with the key as the | 249 | * @return a map of the Meters that match the filter, with the key as the |
264 | * name String to the Meter. | 250 | * name String to the Meter. |
265 | */ | 251 | */ |
266 | - @Override | 252 | + @Override |
267 | - public Map<String, Meter> getMeters(final MetricFilter filter) { | 253 | + public Map<String, Meter> getMeters(final MetricFilter filter) { |
268 | return metricsRegistry.getMeters(filter); | 254 | return metricsRegistry.getMeters(filter); |
269 | } | 255 | } |
270 | 256 | ||
... | @@ -272,11 +258,11 @@ public final class MetricsManager implements MetricsService { | ... | @@ -272,11 +258,11 @@ public final class MetricsManager implements MetricsService { |
272 | * Fetches the existing Histograms. | 258 | * Fetches the existing Histograms. |
273 | * | 259 | * |
274 | * @param filter filter to use to select Histograms | 260 | * @param filter filter to use to select Histograms |
275 | - * @return a map of the Histograms that match the filter, with the key as the | 261 | + * @return a map of the Histograms that match the filter, with the key as |
276 | - * name String to the Histogram. | 262 | + * the name String to the Histogram. |
277 | */ | 263 | */ |
278 | - @Override | 264 | + @Override |
279 | - public Map<String, Histogram> getHistograms(final MetricFilter filter) { | 265 | + public Map<String, Histogram> getHistograms(final MetricFilter filter) { |
280 | return metricsRegistry.getHistograms(filter); | 266 | return metricsRegistry.getHistograms(filter); |
281 | } | 267 | } |
282 | 268 | ||
... | @@ -285,9 +271,8 @@ public final class MetricsManager implements MetricsService { | ... | @@ -285,9 +271,8 @@ public final class MetricsManager implements MetricsService { |
285 | * | 271 | * |
286 | * @param filter filter to use to select the Metrics to remove. | 272 | * @param filter filter to use to select the Metrics to remove. |
287 | */ | 273 | */ |
288 | - @Override | 274 | + @Override |
289 | - public void removeMatching(final MetricFilter filter) { | 275 | + public void removeMatching(final MetricFilter filter) { |
290 | metricsRegistry.removeMatching(filter); | 276 | metricsRegistry.removeMatching(filter); |
291 | } | 277 | } |
292 | } | 278 | } |
293 | - | ... | ... |
... | @@ -13,7 +13,7 @@ import com.codahale.metrics.Timer; | ... | @@ -13,7 +13,7 @@ import com.codahale.metrics.Timer; |
13 | /** | 13 | /** |
14 | * Metrics Service to collect metrics. | 14 | * Metrics Service to collect metrics. |
15 | */ | 15 | */ |
16 | -interface MetricsService { | 16 | +public interface MetricsService { |
17 | 17 | ||
18 | /** | 18 | /** |
19 | * Registers a component. | 19 | * Registers a component. |
... | @@ -90,6 +90,18 @@ interface MetricsService { | ... | @@ -90,6 +90,18 @@ interface MetricsService { |
90 | T metric); | 90 | T metric); |
91 | 91 | ||
92 | /** | 92 | /** |
93 | + * Removes the metric with the given name. | ||
94 | + * | ||
95 | + * @param component component the Metric is defined in | ||
96 | + * @param feature feature the Metric is defined in | ||
97 | + * @param metricName local name of the metric | ||
98 | + * @return true if the metric existed and was removed, otherwise false | ||
99 | + */ | ||
100 | + boolean removeMetric(MetricsComponent component, | ||
101 | + MetricsFeature feature, | ||
102 | + String metricName); | ||
103 | + | ||
104 | + /** | ||
93 | * Fetches the existing Timers. | 105 | * Fetches the existing Timers. |
94 | * | 106 | * |
95 | * @param filter filter to use to select Timers | 107 | * @param filter filter to use to select Timers |
... | @@ -129,15 +141,15 @@ interface MetricsService { | ... | @@ -129,15 +141,15 @@ interface MetricsService { |
129 | * Fetches the existing Histograms. | 141 | * Fetches the existing Histograms. |
130 | * | 142 | * |
131 | * @param filter filter to use to select Histograms | 143 | * @param filter filter to use to select Histograms |
132 | - * @return a map of the Histograms that match the filter, with the key as the | 144 | + * @return a map of the Histograms that match the filter, with the key as |
133 | - * name String to the Histogram. | 145 | + * the name String to the Histogram. |
134 | */ | 146 | */ |
135 | Map<String, Histogram> getHistograms(MetricFilter filter); | 147 | Map<String, Histogram> getHistograms(MetricFilter filter); |
148 | + | ||
136 | /** | 149 | /** |
137 | * Removes all Metrics that match a given filter. | 150 | * Removes all Metrics that match a given filter. |
138 | * | 151 | * |
139 | * @param filter filter to use to select the Metrics to remove. | 152 | * @param filter filter to use to select the Metrics to remove. |
140 | */ | 153 | */ |
141 | void removeMatching(MetricFilter filter); | 154 | void removeMatching(MetricFilter filter); |
142 | - | ||
143 | } | 155 | } | ... | ... |
-
Please register or login to post a comment