Committed by
Gerrit Code Review
Slice out MetricsHelper/Util
Change-Id: Ic6848f47d38550b78ebd6cdcf414305e54408882
Showing
3 changed files
with
120 additions
and
28 deletions
| 1 | +/* | ||
| 2 | + * Copyright 2014 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * 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 | ||
| 7 | + * | ||
| 8 | + * 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. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +package org.onlab.onos.core; | ||
| 18 | + | ||
| 19 | +import org.onlab.metrics.MetricsComponent; | ||
| 20 | +import org.onlab.metrics.MetricsFeature; | ||
| 21 | +import org.onlab.metrics.MetricsService; | ||
| 22 | + | ||
| 23 | +import com.codahale.metrics.Timer; | ||
| 24 | + | ||
| 25 | +/** | ||
| 26 | + * Collection of utility methods used for providing Metrics. | ||
| 27 | + */ | ||
| 28 | +public interface MetricsHelper { | ||
| 29 | + | ||
| 30 | + /** | ||
| 31 | + * Returns MetricService instance. | ||
| 32 | + * | ||
| 33 | + * @return MetricService instance | ||
| 34 | + */ | ||
| 35 | + abstract MetricsService metricsService(); | ||
| 36 | + | ||
| 37 | + | ||
| 38 | + /** | ||
| 39 | + * Creates a Timer instance with given name. | ||
| 40 | + * | ||
| 41 | + * @param component component name | ||
| 42 | + * @param feature feature name | ||
| 43 | + * @param name timer name | ||
| 44 | + * @return Timer instance | ||
| 45 | + */ | ||
| 46 | + default Timer createTimer(String component, String feature, String name) { | ||
| 47 | + final MetricsService metricsService = metricsService(); | ||
| 48 | + if (metricsService != null) { | ||
| 49 | + MetricsComponent c = metricsService.registerComponent(component); | ||
| 50 | + MetricsFeature f = c.registerFeature(feature); | ||
| 51 | + return metricsService.createTimer(c, f, name); | ||
| 52 | + } | ||
| 53 | + return null; | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | +} |
| ... | @@ -17,6 +17,7 @@ package org.onlab.onos.cluster.impl; | ... | @@ -17,6 +17,7 @@ package org.onlab.onos.cluster.impl; |
| 17 | 17 | ||
| 18 | import static com.google.common.base.Preconditions.checkNotNull; | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; | 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | +import static org.onlab.metrics.MetricsUtil.*; | ||
| 20 | 21 | ||
| 21 | import java.util.Set; | 22 | import java.util.Set; |
| 22 | import java.util.concurrent.atomic.AtomicInteger; | 23 | import java.util.concurrent.atomic.AtomicInteger; |
| ... | @@ -27,8 +28,6 @@ import org.apache.felix.scr.annotations.Deactivate; | ... | @@ -27,8 +28,6 @@ import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Reference; | 28 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; | 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 29 | import org.apache.felix.scr.annotations.Service; | 30 | import org.apache.felix.scr.annotations.Service; |
| 30 | -import org.onlab.metrics.MetricsComponent; | ||
| 31 | -import org.onlab.metrics.MetricsFeature; | ||
| 32 | import org.onlab.metrics.MetricsService; | 31 | import org.onlab.metrics.MetricsService; |
| 33 | import org.onlab.onos.cluster.ClusterEvent; | 32 | import org.onlab.onos.cluster.ClusterEvent; |
| 34 | import org.onlab.onos.cluster.ClusterEventListener; | 33 | import org.onlab.onos.cluster.ClusterEventListener; |
| ... | @@ -36,6 +35,7 @@ import org.onlab.onos.cluster.ClusterService; | ... | @@ -36,6 +35,7 @@ import org.onlab.onos.cluster.ClusterService; |
| 36 | import org.onlab.onos.cluster.ControllerNode; | 35 | import org.onlab.onos.cluster.ControllerNode; |
| 37 | import org.onlab.onos.cluster.NodeId; | 36 | import org.onlab.onos.cluster.NodeId; |
| 38 | import org.onlab.onos.cluster.RoleInfo; | 37 | import org.onlab.onos.cluster.RoleInfo; |
| 38 | +import org.onlab.onos.core.MetricsHelper; | ||
| 39 | import org.onlab.onos.event.AbstractListenerRegistry; | 39 | import org.onlab.onos.event.AbstractListenerRegistry; |
| 40 | import org.onlab.onos.event.EventDeliveryService; | 40 | import org.onlab.onos.event.EventDeliveryService; |
| 41 | import org.onlab.onos.mastership.MastershipAdminService; | 41 | import org.onlab.onos.mastership.MastershipAdminService; |
| ... | @@ -56,7 +56,8 @@ import com.codahale.metrics.Timer.Context; | ... | @@ -56,7 +56,8 @@ import com.codahale.metrics.Timer.Context; |
| 56 | @Component(immediate = true) | 56 | @Component(immediate = true) |
| 57 | @Service | 57 | @Service |
| 58 | public class MastershipManager | 58 | public class MastershipManager |
| 59 | - implements MastershipService, MastershipAdminService, MastershipTermService { | 59 | + implements MastershipService, MastershipAdminService, MastershipTermService, |
| 60 | + MetricsHelper { | ||
| 60 | 61 | ||
| 61 | private static final String NODE_ID_NULL = "Node ID cannot be null"; | 62 | private static final String NODE_ID_NULL = "Node ID cannot be null"; |
| 62 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; | 63 | private static final String DEVICE_ID_NULL = "Device ID cannot be null"; |
| ... | @@ -192,7 +193,10 @@ public class MastershipManager | ... | @@ -192,7 +193,10 @@ public class MastershipManager |
| 192 | listenerRegistry.removeListener(listener); | 193 | listenerRegistry.removeListener(listener); |
| 193 | } | 194 | } |
| 194 | 195 | ||
| 195 | - // FIXME: provide wiring to allow events to be triggered by changes within the store | 196 | + @Override |
| 197 | + public MetricsService metricsService() { | ||
| 198 | + return metricsService; | ||
| 199 | + } | ||
| 196 | 200 | ||
| 197 | // Posts the specified event to the local event dispatcher. | 201 | // Posts the specified event to the local event dispatcher. |
| 198 | private void post(MastershipEvent event) { | 202 | private void post(MastershipEvent event) { |
| ... | @@ -201,30 +205,6 @@ public class MastershipManager | ... | @@ -201,30 +205,6 @@ public class MastershipManager |
| 201 | } | 205 | } |
| 202 | } | 206 | } |
| 203 | 207 | ||
| 204 | - | ||
| 205 | - | ||
| 206 | - private Timer createTimer(String component, String feature, String name) { | ||
| 207 | - if (metricsService != null) { | ||
| 208 | - MetricsComponent c = metricsService.registerComponent(component); | ||
| 209 | - MetricsFeature f = c.registerFeature(feature); | ||
| 210 | - return metricsService.createTimer(c, f, name); | ||
| 211 | - } | ||
| 212 | - return null; | ||
| 213 | - } | ||
| 214 | - | ||
| 215 | - private static final Context startTimer(Timer timer) { | ||
| 216 | - if (timer != null) { | ||
| 217 | - return timer.time(); | ||
| 218 | - } | ||
| 219 | - return null; | ||
| 220 | - } | ||
| 221 | - | ||
| 222 | - private static final void stopTimer(Context context) { | ||
| 223 | - if (context != null) { | ||
| 224 | - context.stop(); | ||
| 225 | - } | ||
| 226 | - } | ||
| 227 | - | ||
| 228 | //callback for reacting to cluster events | 208 | //callback for reacting to cluster events |
| 229 | private class InternalClusterEventListener implements ClusterEventListener { | 209 | private class InternalClusterEventListener implements ClusterEventListener { |
| 230 | 210 | ... | ... |
| 1 | +/* | ||
| 2 | + * Copyright 2014 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * 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 | ||
| 7 | + * | ||
| 8 | + * 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. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +package org.onlab.metrics; | ||
| 18 | + | ||
| 19 | +import com.codahale.metrics.Timer; | ||
| 20 | +import com.codahale.metrics.Timer.Context; | ||
| 21 | + | ||
| 22 | +public final class MetricsUtil { | ||
| 23 | + | ||
| 24 | + /** | ||
| 25 | + * Starts the Metric Timer. | ||
| 26 | + * <p> | ||
| 27 | + * If the given timer was null, it will silently return null. | ||
| 28 | + * </p> | ||
| 29 | + * | ||
| 30 | + * @param timer timer to start | ||
| 31 | + * @return timing context, if timer was not null | ||
| 32 | + */ | ||
| 33 | + public static final Context startTimer(Timer timer) { | ||
| 34 | + if (timer != null) { | ||
| 35 | + return timer.time(); | ||
| 36 | + } | ||
| 37 | + return null; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + /** | ||
| 41 | + * Stops the Metric Timer context. | ||
| 42 | + * <p> | ||
| 43 | + * If the given context was null, it will silently be ignored. | ||
| 44 | + * </p> | ||
| 45 | + * | ||
| 46 | + * @param context timing context to stop, if not null. | ||
| 47 | + */ | ||
| 48 | + public static final void stopTimer(Context context) { | ||
| 49 | + if (context != null) { | ||
| 50 | + context.stop(); | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + // avoid instantiation | ||
| 55 | + private MetricsUtil() {} | ||
| 56 | +} |
-
Please register or login to post a comment