Aggregate system metrics using metrics service
Change-Id: I617fa21973b7e01b92f311a6fa5687e1f0f870c2
Showing
4 changed files
with
249 additions
and
33 deletions
1 | +/* | ||
2 | + * Copyright 2016 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 | +package org.onosproject.cpman.impl; | ||
17 | + | ||
18 | +import com.codahale.metrics.Meter; | ||
19 | +import com.google.common.collect.Maps; | ||
20 | +import com.google.common.collect.Sets; | ||
21 | +import org.onlab.metrics.MetricsComponent; | ||
22 | +import org.onlab.metrics.MetricsFeature; | ||
23 | +import org.onlab.metrics.MetricsService; | ||
24 | +import org.onosproject.cpman.ControlMetricType; | ||
25 | +import org.onosproject.cpman.ControlResource; | ||
26 | +import org.slf4j.Logger; | ||
27 | + | ||
28 | +import java.util.Map; | ||
29 | +import java.util.Optional; | ||
30 | +import java.util.Set; | ||
31 | + | ||
32 | +import static org.slf4j.LoggerFactory.getLogger; | ||
33 | + | ||
34 | +/** | ||
35 | + * Aggregate system metrics. | ||
36 | + */ | ||
37 | +public class SystemMetricsAggregator { | ||
38 | + | ||
39 | + private final Logger log = getLogger(getClass()); | ||
40 | + | ||
41 | + private static final String DEFAULT_RESOURCE_NAME = "system"; | ||
42 | + private static final String DEFAULT_METER_SUFFIX = "rate"; | ||
43 | + private static final String DISK_RESOURCE_TYPE = "disk"; | ||
44 | + private static final String NETWORK_RESOURCE_TYPE = "network"; | ||
45 | + private final Map<ControlMetricType, Meter> meterMap = Maps.newHashMap(); | ||
46 | + private final Set<ControlMetricType> metricTypeSet = Sets.newHashSet(); | ||
47 | + | ||
48 | + public SystemMetricsAggregator(MetricsService metricsService, Optional<String> resName, String resType) { | ||
49 | + String resourceName = resName.isPresent() ? resName.get() : DEFAULT_RESOURCE_NAME; | ||
50 | + MetricsComponent mc = metricsService.registerComponent(resourceName); | ||
51 | + | ||
52 | + if (resName.isPresent()) { | ||
53 | + if (DISK_RESOURCE_TYPE.equals(resType)) { | ||
54 | + metricTypeSet.addAll(ControlResource.DISK_METRICS); | ||
55 | + } else if (NETWORK_RESOURCE_TYPE.equals(resType)) { | ||
56 | + metricTypeSet.addAll(ControlResource.NETWORK_METRICS); | ||
57 | + } else { | ||
58 | + log.warn("Not valid resource type {}", resType); | ||
59 | + } | ||
60 | + } else { | ||
61 | + metricTypeSet.addAll(ControlResource.MEMORY_METRICS); | ||
62 | + metricTypeSet.addAll(ControlResource.CPU_METRICS); | ||
63 | + } | ||
64 | + | ||
65 | + metricTypeSet.forEach(type -> { | ||
66 | + MetricsFeature metricsFeature = mc.registerFeature(type.toString()); | ||
67 | + Meter meter = metricsService.createMeter(mc, metricsFeature, DEFAULT_METER_SUFFIX); | ||
68 | + meterMap.putIfAbsent(type, meter); | ||
69 | + }); | ||
70 | + } | ||
71 | + | ||
72 | + /** | ||
73 | + * Increments metric value. | ||
74 | + * | ||
75 | + * @param type metric type | ||
76 | + * @param value metric value | ||
77 | + */ | ||
78 | + public void increment(ControlMetricType type, long value) { | ||
79 | + meterMap.get(type).mark(value); | ||
80 | + } | ||
81 | +} |
... | @@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.JsonNode; | ... | @@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.JsonNode; |
19 | import com.fasterxml.jackson.databind.node.ArrayNode; | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
20 | import com.fasterxml.jackson.databind.node.ObjectNode; | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
21 | import org.apache.commons.lang3.StringUtils; | 21 | import org.apache.commons.lang3.StringUtils; |
22 | +import org.onlab.metrics.MetricsService; | ||
22 | import org.onosproject.cpman.ControlMetric; | 23 | import org.onosproject.cpman.ControlMetric; |
23 | import org.onosproject.cpman.ControlMetricType; | 24 | import org.onosproject.cpman.ControlMetricType; |
24 | import org.onosproject.cpman.ControlPlaneMonitorService; | 25 | import org.onosproject.cpman.ControlPlaneMonitorService; |
... | @@ -26,6 +27,7 @@ import org.onosproject.cpman.ControlResource; | ... | @@ -26,6 +27,7 @@ import org.onosproject.cpman.ControlResource; |
26 | import org.onosproject.cpman.MetricValue; | 27 | import org.onosproject.cpman.MetricValue; |
27 | import org.onosproject.cpman.SystemInfo; | 28 | import org.onosproject.cpman.SystemInfo; |
28 | import org.onosproject.cpman.impl.DefaultSystemInfo; | 29 | import org.onosproject.cpman.impl.DefaultSystemInfo; |
30 | +import org.onosproject.cpman.impl.SystemMetricsAggregator; | ||
29 | import org.onosproject.cpman.impl.SystemInfoFactory; | 31 | import org.onosproject.cpman.impl.SystemInfoFactory; |
30 | import org.onosproject.rest.AbstractWebResource; | 32 | import org.onosproject.rest.AbstractWebResource; |
31 | import org.slf4j.Logger; | 33 | import org.slf4j.Logger; |
... | @@ -53,7 +55,9 @@ import static org.onlab.util.Tools.nullIsIllegal; | ... | @@ -53,7 +55,9 @@ import static org.onlab.util.Tools.nullIsIllegal; |
53 | public class SystemMetricsCollectorWebResource extends AbstractWebResource { | 55 | public class SystemMetricsCollectorWebResource extends AbstractWebResource { |
54 | 56 | ||
55 | private final Logger log = LoggerFactory.getLogger(getClass()); | 57 | private final Logger log = LoggerFactory.getLogger(getClass()); |
56 | - private final ControlPlaneMonitorService service = get(ControlPlaneMonitorService.class); | 58 | + private final ControlPlaneMonitorService monitorService = get(ControlPlaneMonitorService.class); |
59 | + private final MetricsService metricsService = get(MetricsService.class); | ||
60 | + | ||
57 | private static final int UPDATE_INTERVAL_IN_MINUTE = 1; | 61 | private static final int UPDATE_INTERVAL_IN_MINUTE = 1; |
58 | private static final String INVALID_SYSTEM_SPECS = "Invalid system specifications"; | 62 | private static final String INVALID_SYSTEM_SPECS = "Invalid system specifications"; |
59 | private static final String INVALID_RESOURCE_NAME = "Invalid resource name"; | 63 | private static final String INVALID_RESOURCE_NAME = "Invalid resource name"; |
... | @@ -68,6 +72,9 @@ public class SystemMetricsCollectorWebResource extends AbstractWebResource { | ... | @@ -68,6 +72,9 @@ public class SystemMetricsCollectorWebResource extends AbstractWebResource { |
68 | .stream().map(type -> toCamelCase(type.toString(), true)) | 72 | .stream().map(type -> toCamelCase(type.toString(), true)) |
69 | .collect(Collectors.toSet()); | 73 | .collect(Collectors.toSet()); |
70 | 74 | ||
75 | + private SystemMetricsAggregator systemAggr = | ||
76 | + new SystemMetricsAggregator(metricsService, Optional.ofNullable(null), "system"); | ||
77 | + | ||
71 | /** | 78 | /** |
72 | * Collects CPU metrics. | 79 | * Collects CPU metrics. |
73 | * | 80 | * |
... | @@ -98,28 +105,31 @@ public class SystemMetricsCollectorWebResource extends AbstractWebResource { | ... | @@ -98,28 +105,31 @@ public class SystemMetricsCollectorWebResource extends AbstractWebResource { |
98 | 105 | ||
99 | cm = new ControlMetric(ControlMetricType.CPU_LOAD, | 106 | cm = new ControlMetric(ControlMetricType.CPU_LOAD, |
100 | new MetricValue.Builder().load(cpuLoad).add()); | 107 | new MetricValue.Builder().load(cpuLoad).add()); |
101 | - service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); | 108 | + monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); |
109 | + systemAggr.increment(ControlMetricType.CPU_LOAD, cpuLoad); | ||
102 | 110 | ||
103 | cm = new ControlMetric(ControlMetricType.TOTAL_CPU_TIME, | 111 | cm = new ControlMetric(ControlMetricType.TOTAL_CPU_TIME, |
104 | new MetricValue.Builder().load(totalCpuTime).add()); | 112 | new MetricValue.Builder().load(totalCpuTime).add()); |
105 | - service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); | 113 | + monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); |
114 | + systemAggr.increment(ControlMetricType.TOTAL_CPU_TIME, totalCpuTime); | ||
106 | 115 | ||
107 | cm = new ControlMetric(ControlMetricType.SYS_CPU_TIME, | 116 | cm = new ControlMetric(ControlMetricType.SYS_CPU_TIME, |
108 | new MetricValue.Builder().load(sysCpuTime).add()); | 117 | new MetricValue.Builder().load(sysCpuTime).add()); |
109 | - service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); | 118 | + monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); |
119 | + systemAggr.increment(ControlMetricType.SYS_CPU_TIME, sysCpuTime); | ||
110 | 120 | ||
111 | cm = new ControlMetric(ControlMetricType.USER_CPU_TIME, | 121 | cm = new ControlMetric(ControlMetricType.USER_CPU_TIME, |
112 | new MetricValue.Builder().load(userCpuTime).add()); | 122 | new MetricValue.Builder().load(userCpuTime).add()); |
113 | - service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); | 123 | + monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); |
124 | + systemAggr.increment(ControlMetricType.USER_CPU_TIME, userCpuTime); | ||
114 | 125 | ||
115 | cm = new ControlMetric(ControlMetricType.CPU_IDLE_TIME, | 126 | cm = new ControlMetric(ControlMetricType.CPU_IDLE_TIME, |
116 | new MetricValue.Builder().load(cpuIdleTime).add()); | 127 | new MetricValue.Builder().load(cpuIdleTime).add()); |
117 | - service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); | 128 | + monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); |
129 | + systemAggr.increment(ControlMetricType.CPU_IDLE_TIME, cpuIdleTime); | ||
118 | 130 | ||
119 | } catch (IOException e) { | 131 | } catch (IOException e) { |
120 | throw new IllegalArgumentException(e.getMessage()); | 132 | throw new IllegalArgumentException(e.getMessage()); |
121 | - } catch (IllegalArgumentException iae) { | ||
122 | - log.error("[CPU] Illegal arguments in JSON input, msg: {}", iae.getMessage()); | ||
123 | } | 133 | } |
124 | return ok(root).build(); | 134 | return ok(root).build(); |
125 | } | 135 | } |
... | @@ -153,24 +163,26 @@ public class SystemMetricsCollectorWebResource extends AbstractWebResource { | ... | @@ -153,24 +163,26 @@ public class SystemMetricsCollectorWebResource extends AbstractWebResource { |
153 | 163 | ||
154 | cm = new ControlMetric(ControlMetricType.MEMORY_USED_RATIO, | 164 | cm = new ControlMetric(ControlMetricType.MEMORY_USED_RATIO, |
155 | new MetricValue.Builder().load(memUsedRatio).add()); | 165 | new MetricValue.Builder().load(memUsedRatio).add()); |
156 | - service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); | 166 | + monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); |
167 | + systemAggr.increment(ControlMetricType.MEMORY_USED_RATIO, memUsedRatio); | ||
157 | 168 | ||
158 | cm = new ControlMetric(ControlMetricType.MEMORY_FREE_RATIO, | 169 | cm = new ControlMetric(ControlMetricType.MEMORY_FREE_RATIO, |
159 | new MetricValue.Builder().load(memFreeRatio).add()); | 170 | new MetricValue.Builder().load(memFreeRatio).add()); |
160 | - service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); | 171 | + monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); |
172 | + systemAggr.increment(ControlMetricType.MEMORY_FREE_RATIO, memFreeRatio); | ||
161 | 173 | ||
162 | cm = new ControlMetric(ControlMetricType.MEMORY_USED, | 174 | cm = new ControlMetric(ControlMetricType.MEMORY_USED, |
163 | new MetricValue.Builder().load(memUsed).add()); | 175 | new MetricValue.Builder().load(memUsed).add()); |
164 | - service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); | 176 | + monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); |
177 | + systemAggr.increment(ControlMetricType.MEMORY_USED, memUsed); | ||
165 | 178 | ||
166 | cm = new ControlMetric(ControlMetricType.MEMORY_FREE, | 179 | cm = new ControlMetric(ControlMetricType.MEMORY_FREE, |
167 | new MetricValue.Builder().load(memFree).add()); | 180 | new MetricValue.Builder().load(memFree).add()); |
168 | - service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); | 181 | + monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, Optional.ofNullable(null)); |
182 | + systemAggr.increment(ControlMetricType.MEMORY_FREE, memFree); | ||
169 | 183 | ||
170 | } catch (IOException e) { | 184 | } catch (IOException e) { |
171 | throw new IllegalArgumentException(e.getMessage()); | 185 | throw new IllegalArgumentException(e.getMessage()); |
172 | - } catch (IllegalArgumentException iae) { | ||
173 | - log.error("[RAM] Illegal arguments in JSON input, msg: {}", iae.getMessage()); | ||
174 | } | 186 | } |
175 | return ok(root).build(); | 187 | return ok(root).build(); |
176 | } | 188 | } |
... | @@ -198,21 +210,24 @@ public class SystemMetricsCollectorWebResource extends AbstractWebResource { | ... | @@ -198,21 +210,24 @@ public class SystemMetricsCollectorWebResource extends AbstractWebResource { |
198 | JsonNode resourceName = node.get("resourceName"); | 210 | JsonNode resourceName = node.get("resourceName"); |
199 | nullIsIllegal(resourceName, INVALID_RESOURCE_NAME); | 211 | nullIsIllegal(resourceName, INVALID_RESOURCE_NAME); |
200 | 212 | ||
213 | + SystemMetricsAggregator diskAggr = new SystemMetricsAggregator(metricsService, | ||
214 | + Optional.of(resourceName.asText()), "disk"); | ||
215 | + | ||
201 | long readBytes = nullIsIllegal(node.get("readBytes").asLong(), INVALID_REQUEST); | 216 | long readBytes = nullIsIllegal(node.get("readBytes").asLong(), INVALID_REQUEST); |
202 | long writeBytes = nullIsIllegal(node.get("writeBytes").asLong(), INVALID_REQUEST); | 217 | long writeBytes = nullIsIllegal(node.get("writeBytes").asLong(), INVALID_REQUEST); |
203 | 218 | ||
204 | cm = new ControlMetric(ControlMetricType.DISK_READ_BYTES, | 219 | cm = new ControlMetric(ControlMetricType.DISK_READ_BYTES, |
205 | new MetricValue.Builder().load(readBytes).add()); | 220 | new MetricValue.Builder().load(readBytes).add()); |
206 | - service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText()); | 221 | + monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText()); |
222 | + diskAggr.increment(ControlMetricType.DISK_READ_BYTES, readBytes); | ||
207 | 223 | ||
208 | cm = new ControlMetric(ControlMetricType.DISK_WRITE_BYTES, | 224 | cm = new ControlMetric(ControlMetricType.DISK_WRITE_BYTES, |
209 | new MetricValue.Builder().load(writeBytes).add()); | 225 | new MetricValue.Builder().load(writeBytes).add()); |
210 | - service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText()); | 226 | + monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText()); |
227 | + diskAggr.increment(ControlMetricType.DISK_WRITE_BYTES, writeBytes); | ||
211 | } | 228 | } |
212 | } catch (IOException e) { | 229 | } catch (IOException e) { |
213 | throw new IllegalArgumentException(e.getMessage()); | 230 | throw new IllegalArgumentException(e.getMessage()); |
214 | - } catch (IllegalArgumentException iae) { | ||
215 | - log.error("[DISK] Illegal arguments in JSON input, msg: {}", iae.getMessage()); | ||
216 | } | 231 | } |
217 | return ok(root).build(); | 232 | return ok(root).build(); |
218 | } | 233 | } |
... | @@ -241,6 +256,9 @@ public class SystemMetricsCollectorWebResource extends AbstractWebResource { | ... | @@ -241,6 +256,9 @@ public class SystemMetricsCollectorWebResource extends AbstractWebResource { |
241 | JsonNode resourceName = node.get("resourceName"); | 256 | JsonNode resourceName = node.get("resourceName"); |
242 | nullIsIllegal(resourceName, INVALID_RESOURCE_NAME); | 257 | nullIsIllegal(resourceName, INVALID_RESOURCE_NAME); |
243 | 258 | ||
259 | + SystemMetricsAggregator networkAggr = new SystemMetricsAggregator(metricsService, | ||
260 | + Optional.of(resourceName.asText()), "network"); | ||
261 | + | ||
244 | long inBytes = nullIsIllegal(node.get("incomingBytes").asLong(), INVALID_REQUEST); | 262 | long inBytes = nullIsIllegal(node.get("incomingBytes").asLong(), INVALID_REQUEST); |
245 | long outBytes = nullIsIllegal(node.get("outgoingBytes").asLong(), INVALID_REQUEST); | 263 | long outBytes = nullIsIllegal(node.get("outgoingBytes").asLong(), INVALID_REQUEST); |
246 | long inPackets = nullIsIllegal(node.get("incomingPackets").asLong(), INVALID_REQUEST); | 264 | long inPackets = nullIsIllegal(node.get("incomingPackets").asLong(), INVALID_REQUEST); |
... | @@ -248,19 +266,23 @@ public class SystemMetricsCollectorWebResource extends AbstractWebResource { | ... | @@ -248,19 +266,23 @@ public class SystemMetricsCollectorWebResource extends AbstractWebResource { |
248 | 266 | ||
249 | cm = new ControlMetric(ControlMetricType.NW_INCOMING_BYTES, | 267 | cm = new ControlMetric(ControlMetricType.NW_INCOMING_BYTES, |
250 | new MetricValue.Builder().load(inBytes).add()); | 268 | new MetricValue.Builder().load(inBytes).add()); |
251 | - service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText()); | 269 | + monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText()); |
270 | + networkAggr.increment(ControlMetricType.NW_INCOMING_BYTES, inBytes); | ||
252 | 271 | ||
253 | cm = new ControlMetric(ControlMetricType.NW_OUTGOING_BYTES, | 272 | cm = new ControlMetric(ControlMetricType.NW_OUTGOING_BYTES, |
254 | new MetricValue.Builder().load(outBytes).add()); | 273 | new MetricValue.Builder().load(outBytes).add()); |
255 | - service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText()); | 274 | + monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText()); |
275 | + networkAggr.increment(ControlMetricType.NW_OUTGOING_BYTES, outBytes); | ||
256 | 276 | ||
257 | cm = new ControlMetric(ControlMetricType.NW_INCOMING_PACKETS, | 277 | cm = new ControlMetric(ControlMetricType.NW_INCOMING_PACKETS, |
258 | new MetricValue.Builder().load(inPackets).add()); | 278 | new MetricValue.Builder().load(inPackets).add()); |
259 | - service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText()); | 279 | + monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText()); |
280 | + networkAggr.increment(ControlMetricType.NW_INCOMING_PACKETS, inPackets); | ||
260 | 281 | ||
261 | cm = new ControlMetric(ControlMetricType.NW_OUTGOING_PACKETS, | 282 | cm = new ControlMetric(ControlMetricType.NW_OUTGOING_PACKETS, |
262 | new MetricValue.Builder().load(outPackets).add()); | 283 | new MetricValue.Builder().load(outPackets).add()); |
263 | - service.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText()); | 284 | + monitorService.updateMetric(cm, UPDATE_INTERVAL_IN_MINUTE, resourceName.asText()); |
285 | + networkAggr.increment(ControlMetricType.NW_OUTGOING_PACKETS, outPackets); | ||
264 | } | 286 | } |
265 | } catch (IOException e) { | 287 | } catch (IOException e) { |
266 | throw new IllegalArgumentException(e.getMessage()); | 288 | throw new IllegalArgumentException(e.getMessage()); | ... | ... |
... | @@ -15,28 +15,42 @@ | ... | @@ -15,28 +15,42 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.cpman.rest; | 16 | package org.onosproject.cpman.rest; |
17 | 17 | ||
18 | -import java.io.InputStream; | 18 | +import com.codahale.metrics.Counter; |
19 | -import java.net.HttpURLConnection; | 19 | +import com.codahale.metrics.Gauge; |
20 | -import java.util.Optional; | 20 | +import com.codahale.metrics.Histogram; |
21 | - | 21 | +import com.codahale.metrics.Meter; |
22 | -import javax.ws.rs.client.Entity; | 22 | +import com.codahale.metrics.Metric; |
23 | -import javax.ws.rs.client.WebTarget; | 23 | +import com.codahale.metrics.MetricFilter; |
24 | -import javax.ws.rs.core.MediaType; | 24 | +import com.codahale.metrics.MetricRegistry; |
25 | -import javax.ws.rs.core.Response; | 25 | +import com.codahale.metrics.Timer; |
26 | - | ||
27 | import org.glassfish.jersey.server.ResourceConfig; | 26 | import org.glassfish.jersey.server.ResourceConfig; |
28 | import org.junit.Before; | 27 | import org.junit.Before; |
29 | import org.junit.Test; | 28 | import org.junit.Test; |
29 | +import org.onlab.metrics.MetricsComponent; | ||
30 | +import org.onlab.metrics.MetricsFeature; | ||
31 | +import org.onlab.metrics.MetricsService; | ||
30 | import org.onlab.osgi.ServiceDirectory; | 32 | import org.onlab.osgi.ServiceDirectory; |
31 | import org.onlab.osgi.TestServiceDirectory; | 33 | import org.onlab.osgi.TestServiceDirectory; |
32 | import org.onlab.rest.BaseResource; | 34 | import org.onlab.rest.BaseResource; |
33 | import org.onosproject.cpman.ControlPlaneMonitorService; | 35 | import org.onosproject.cpman.ControlPlaneMonitorService; |
34 | import org.onosproject.cpman.SystemInfo; | 36 | import org.onosproject.cpman.SystemInfo; |
35 | import org.onosproject.cpman.impl.SystemInfoFactory; | 37 | import org.onosproject.cpman.impl.SystemInfoFactory; |
38 | +import org.onosproject.cpman.impl.SystemMetricsAggregator; | ||
36 | import org.onosproject.net.DeviceId; | 39 | import org.onosproject.net.DeviceId; |
37 | import org.onosproject.rest.resources.ResourceTest; | 40 | import org.onosproject.rest.resources.ResourceTest; |
38 | 41 | ||
42 | +import javax.ws.rs.client.Entity; | ||
43 | +import javax.ws.rs.client.WebTarget; | ||
44 | +import javax.ws.rs.core.MediaType; | ||
45 | +import javax.ws.rs.core.Response; | ||
46 | +import java.io.InputStream; | ||
47 | +import java.net.HttpURLConnection; | ||
48 | +import java.util.Collections; | ||
49 | +import java.util.Map; | ||
50 | +import java.util.Optional; | ||
51 | + | ||
39 | import static org.easymock.EasyMock.anyInt; | 52 | import static org.easymock.EasyMock.anyInt; |
53 | +import static org.easymock.EasyMock.anyLong; | ||
40 | import static org.easymock.EasyMock.anyObject; | 54 | import static org.easymock.EasyMock.anyObject; |
41 | import static org.easymock.EasyMock.anyString; | 55 | import static org.easymock.EasyMock.anyString; |
42 | import static org.easymock.EasyMock.createMock; | 56 | import static org.easymock.EasyMock.createMock; |
... | @@ -53,6 +67,10 @@ public class ControlMetricsCollectorResourceTest extends ResourceTest { | ... | @@ -53,6 +67,10 @@ public class ControlMetricsCollectorResourceTest extends ResourceTest { |
53 | 67 | ||
54 | final ControlPlaneMonitorService mockControlPlaneMonitorService = | 68 | final ControlPlaneMonitorService mockControlPlaneMonitorService = |
55 | createMock(ControlPlaneMonitorService.class); | 69 | createMock(ControlPlaneMonitorService.class); |
70 | + final MetricsService mockMetricsService = new MockMetricsService(); | ||
71 | + final MetricsComponent mockMetricsComponent = createMock(MetricsComponent.class); | ||
72 | + final SystemMetricsAggregator mockAggregator = createMock(SystemMetricsAggregator.class); | ||
73 | + | ||
56 | 74 | ||
57 | private static final String PREFIX = "collector"; | 75 | private static final String PREFIX = "collector"; |
58 | 76 | ||
... | @@ -70,7 +88,8 @@ public class ControlMetricsCollectorResourceTest extends ResourceTest { | ... | @@ -70,7 +88,8 @@ public class ControlMetricsCollectorResourceTest extends ResourceTest { |
70 | public void setUpTest() { | 88 | public void setUpTest() { |
71 | ServiceDirectory testDirectory = | 89 | ServiceDirectory testDirectory = |
72 | new TestServiceDirectory() | 90 | new TestServiceDirectory() |
73 | - .add(ControlPlaneMonitorService.class, mockControlPlaneMonitorService); | 91 | + .add(ControlPlaneMonitorService.class, mockControlPlaneMonitorService) |
92 | + .add(MetricsService.class, mockMetricsService); | ||
74 | BaseResource.setServiceDirectory(testDirectory); | 93 | BaseResource.setServiceDirectory(testDirectory); |
75 | } | 94 | } |
76 | 95 | ||
... | @@ -83,6 +102,11 @@ public class ControlMetricsCollectorResourceTest extends ResourceTest { | ... | @@ -83,6 +102,11 @@ public class ControlMetricsCollectorResourceTest extends ResourceTest { |
83 | (Optional<DeviceId>) anyObject()); | 102 | (Optional<DeviceId>) anyObject()); |
84 | expectLastCall().times(5); | 103 | expectLastCall().times(5); |
85 | replay(mockControlPlaneMonitorService); | 104 | replay(mockControlPlaneMonitorService); |
105 | + | ||
106 | + mockAggregator.increment(anyObject(), anyLong()); | ||
107 | + expectLastCall(); | ||
108 | + replay(mockAggregator); | ||
109 | + | ||
86 | basePostTest("cpu-metrics-post.json", PREFIX + "/cpu_metrics"); | 110 | basePostTest("cpu-metrics-post.json", PREFIX + "/cpu_metrics"); |
87 | } | 111 | } |
88 | 112 | ||
... | @@ -95,6 +119,7 @@ public class ControlMetricsCollectorResourceTest extends ResourceTest { | ... | @@ -95,6 +119,7 @@ public class ControlMetricsCollectorResourceTest extends ResourceTest { |
95 | (Optional<DeviceId>) anyObject()); | 119 | (Optional<DeviceId>) anyObject()); |
96 | expectLastCall().times(4); | 120 | expectLastCall().times(4); |
97 | replay(mockControlPlaneMonitorService); | 121 | replay(mockControlPlaneMonitorService); |
122 | + | ||
98 | basePostTest("memory-metrics-post.json", PREFIX + "/memory_metrics"); | 123 | basePostTest("memory-metrics-post.json", PREFIX + "/memory_metrics"); |
99 | } | 124 | } |
100 | 125 | ||
... | @@ -106,6 +131,7 @@ public class ControlMetricsCollectorResourceTest extends ResourceTest { | ... | @@ -106,6 +131,7 @@ public class ControlMetricsCollectorResourceTest extends ResourceTest { |
106 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyInt(), anyString()); | 131 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyInt(), anyString()); |
107 | expectLastCall().times(4); | 132 | expectLastCall().times(4); |
108 | replay(mockControlPlaneMonitorService); | 133 | replay(mockControlPlaneMonitorService); |
134 | + | ||
109 | basePostTest("disk-metrics-post.json", PREFIX + "/disk_metrics"); | 135 | basePostTest("disk-metrics-post.json", PREFIX + "/disk_metrics"); |
110 | } | 136 | } |
111 | 137 | ||
... | @@ -117,6 +143,7 @@ public class ControlMetricsCollectorResourceTest extends ResourceTest { | ... | @@ -117,6 +143,7 @@ public class ControlMetricsCollectorResourceTest extends ResourceTest { |
117 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyInt(), anyString()); | 143 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyInt(), anyString()); |
118 | expectLastCall().times(8); | 144 | expectLastCall().times(8); |
119 | replay(mockControlPlaneMonitorService); | 145 | replay(mockControlPlaneMonitorService); |
146 | + | ||
120 | basePostTest("network-metrics-post.json", PREFIX + "/network_metrics"); | 147 | basePostTest("network-metrics-post.json", PREFIX + "/network_metrics"); |
121 | } | 148 | } |
122 | 149 | ||
... | @@ -147,4 +174,90 @@ public class ControlMetricsCollectorResourceTest extends ResourceTest { | ... | @@ -147,4 +174,90 @@ public class ControlMetricsCollectorResourceTest extends ResourceTest { |
147 | Response response = baseTest(jsonFile, path); | 174 | Response response = baseTest(jsonFile, path); |
148 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); | 175 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); |
149 | } | 176 | } |
177 | + | ||
178 | + private class MockMetricsService implements MetricsService { | ||
179 | + | ||
180 | + @Override | ||
181 | + public MetricsComponent registerComponent(String name) { | ||
182 | + MetricsComponent metricsComponent = new MetricsComponent(name); | ||
183 | + return metricsComponent; | ||
184 | + } | ||
185 | + | ||
186 | + @Override | ||
187 | + public MetricRegistry getMetricRegistry() { | ||
188 | + return null; | ||
189 | + } | ||
190 | + | ||
191 | + @Override | ||
192 | + public Counter createCounter(MetricsComponent component, MetricsFeature feature, | ||
193 | + String metricName) { | ||
194 | + return null; | ||
195 | + } | ||
196 | + | ||
197 | + @Override | ||
198 | + public Histogram createHistogram(MetricsComponent component, | ||
199 | + MetricsFeature feature, String metricName) { | ||
200 | + return null; | ||
201 | + } | ||
202 | + | ||
203 | + @Override | ||
204 | + public Timer createTimer(MetricsComponent component, | ||
205 | + MetricsFeature feature, String metricName) { | ||
206 | + return null; | ||
207 | + } | ||
208 | + | ||
209 | + @Override | ||
210 | + public Meter createMeter(MetricsComponent component, | ||
211 | + MetricsFeature feature, String metricName) { | ||
212 | + return new Meter(); | ||
213 | + } | ||
214 | + | ||
215 | + @Override | ||
216 | + public <T extends Metric> T registerMetric(MetricsComponent component, | ||
217 | + MetricsFeature feature, | ||
218 | + String metricName, T metric) { | ||
219 | + return null; | ||
220 | + } | ||
221 | + | ||
222 | + @Override | ||
223 | + public boolean removeMetric(MetricsComponent component, | ||
224 | + MetricsFeature feature, String metricName) { | ||
225 | + return false; | ||
226 | + } | ||
227 | + | ||
228 | + @Override | ||
229 | + public Map<String, Timer> getTimers(MetricFilter filter) { | ||
230 | + return Collections.emptyMap(); | ||
231 | + } | ||
232 | + | ||
233 | + @Override | ||
234 | + public Map<String, Gauge> getGauges(MetricFilter filter) { | ||
235 | + return Collections.emptyMap(); | ||
236 | + } | ||
237 | + | ||
238 | + @Override | ||
239 | + public Map<String, Counter> getCounters(MetricFilter filter) { | ||
240 | + return Collections.emptyMap(); | ||
241 | + } | ||
242 | + | ||
243 | + @Override | ||
244 | + public Map<String, Meter> getMeters(MetricFilter filter) { | ||
245 | + return Collections.emptyMap(); | ||
246 | + } | ||
247 | + | ||
248 | + @Override | ||
249 | + public Map<String, Histogram> getHistograms(MetricFilter filter) { | ||
250 | + return Collections.emptyMap(); | ||
251 | + } | ||
252 | + | ||
253 | + @Override | ||
254 | + public Map<String, Metric> getMetrics() { | ||
255 | + return Collections.emptyMap(); | ||
256 | + } | ||
257 | + | ||
258 | + @Override | ||
259 | + public void removeMatching(MetricFilter filter) { | ||
260 | + | ||
261 | + } | ||
262 | + } | ||
150 | } | 263 | } | ... | ... |
... | @@ -35,7 +35,7 @@ public class MetricsComponent implements MetricsComponentRegistry { | ... | @@ -35,7 +35,7 @@ public class MetricsComponent implements MetricsComponentRegistry { |
35 | * | 35 | * |
36 | * @param newName name of the component | 36 | * @param newName name of the component |
37 | */ | 37 | */ |
38 | - MetricsComponent(final String newName) { | 38 | + public MetricsComponent(final String newName) { |
39 | name = newName; | 39 | name = newName; |
40 | } | 40 | } |
41 | 41 | ... | ... |
-
Please register or login to post a comment