Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame] | 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 | */ |
Jian Li | a73fce3 | 2016-04-11 17:08:35 -0700 | [diff] [blame] | 37 | public final class SystemMetricsAggregator { |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame] | 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"; |
Jian Li | a73fce3 | 2016-04-11 17:08:35 -0700 | [diff] [blame] | 45 | private final Map<ControlMetricType, Meter> systemMap = Maps.newHashMap(); |
| 46 | private final Map<String, Map<ControlMetricType, Meter>> diskMap = Maps.newHashMap(); |
| 47 | private final Map<String, Map<ControlMetricType, Meter>> networkMap = Maps.newHashMap(); |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame] | 48 | |
Jian Li | a73fce3 | 2016-04-11 17:08:35 -0700 | [diff] [blame] | 49 | private MetricsService metricsService; |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame] | 50 | |
Jian Li | a73fce3 | 2016-04-11 17:08:35 -0700 | [diff] [blame] | 51 | public static SystemMetricsAggregator getInstance() { |
| 52 | return SingletonHelper.INSTANCE; |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** |
Jian Li | a73fce3 | 2016-04-11 17:08:35 -0700 | [diff] [blame] | 56 | * Configures metric services. |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame] | 57 | * |
Jian Li | a73fce3 | 2016-04-11 17:08:35 -0700 | [diff] [blame] | 58 | * @param service metrics service |
| 59 | */ |
| 60 | public void setMetricsService(MetricsService service) { |
| 61 | |
| 62 | metricsService = service; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Increments system metric value. |
| 67 | * |
| 68 | * @param type metric type |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame] | 69 | * @param value metric value |
| 70 | */ |
| 71 | public void increment(ControlMetricType type, long value) { |
Jian Li | a73fce3 | 2016-04-11 17:08:35 -0700 | [diff] [blame] | 72 | systemMap.get(type).mark(value); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Increments disk or network metric value. |
| 77 | * |
| 78 | * @param resourceName resource name |
| 79 | * @param resourceType resource type |
| 80 | * @param type control metric type |
| 81 | * @param value metric value |
| 82 | */ |
| 83 | public void increment(String resourceName, String resourceType, ControlMetricType type, long value) { |
| 84 | if (DISK_RESOURCE_TYPE.equals(resourceType) && diskMap.containsKey(resourceName)) { |
| 85 | diskMap.get(resourceName).get(type).mark(value); |
| 86 | } |
| 87 | |
| 88 | if (NETWORK_RESOURCE_TYPE.equals(resourceType) && networkMap.containsKey(resourceName)) { |
| 89 | networkMap.get(resourceName).get(type).mark(value); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Adds a set of new monitoring metric types. |
| 95 | * |
| 96 | * @param optResourceName optional resource name, null denotes system metric |
| 97 | * @param resType resource type |
| 98 | */ |
| 99 | public void addMetrics(Optional<String> optResourceName, String resType) { |
| 100 | Set<ControlMetricType> metricTypeSet = Sets.newHashSet(); |
| 101 | String resourceName = optResourceName.isPresent() ? |
| 102 | optResourceName.get() : DEFAULT_RESOURCE_NAME; |
| 103 | |
| 104 | MetricsComponent metricsComponent = metricsService.registerComponent(resourceName); |
| 105 | |
| 106 | if (optResourceName.isPresent()) { |
| 107 | if (!diskMap.containsKey(resourceName) && DISK_RESOURCE_TYPE.equals(resType)) { |
| 108 | metricTypeSet.addAll(ControlResource.DISK_METRICS); |
| 109 | diskMap.putIfAbsent(resourceName, |
| 110 | getMeterMap(metricTypeSet, metricsComponent, metricsService)); |
| 111 | metricsService.notifyReporters(); |
| 112 | } else if (!networkMap.containsKey(resourceName) && NETWORK_RESOURCE_TYPE.equals(resType)) { |
| 113 | metricTypeSet.addAll(ControlResource.NETWORK_METRICS); |
| 114 | networkMap.putIfAbsent(resourceName, |
| 115 | getMeterMap(metricTypeSet, metricsComponent, metricsService)); |
| 116 | metricsService.notifyReporters(); |
| 117 | } else { |
| 118 | return; |
| 119 | } |
| 120 | } else { |
| 121 | if (systemMap.isEmpty()) { |
| 122 | metricTypeSet.addAll(ControlResource.MEMORY_METRICS); |
| 123 | metricTypeSet.addAll(ControlResource.CPU_METRICS); |
| 124 | |
| 125 | systemMap.putAll(getMeterMap(metricTypeSet, metricsComponent, metricsService)); |
| 126 | metricsService.notifyReporters(); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | private Map<ControlMetricType, Meter> getMeterMap(Set<ControlMetricType> types, |
| 132 | MetricsComponent component, |
| 133 | MetricsService service) { |
| 134 | Map<ControlMetricType, Meter> meterMap = Maps.newHashMap(); |
| 135 | types.forEach(type -> { |
| 136 | MetricsFeature metricsFeature = component.registerFeature(type.toString()); |
| 137 | Meter meter = service.createMeter(component, metricsFeature, DEFAULT_METER_SUFFIX); |
| 138 | meterMap.putIfAbsent(type, meter); |
| 139 | }); |
| 140 | return meterMap; |
| 141 | } |
| 142 | |
| 143 | private SystemMetricsAggregator() { |
| 144 | } |
| 145 | |
| 146 | private static class SingletonHelper { |
| 147 | private static final SystemMetricsAggregator INSTANCE = new SystemMetricsAggregator(); |
Jian Li | 7eed417 | 2016-04-07 22:12:03 -0700 | [diff] [blame] | 148 | } |
| 149 | } |