blob: 19887a1710d27b7f80ca231ccc58461564e0477a [file] [log] [blame]
Jian Li7eed4172016-04-07 22:12:03 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Li7eed4172016-04-07 22:12:03 -07003 *
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 */
16package org.onosproject.cpman.impl;
17
18import com.codahale.metrics.Meter;
19import com.google.common.collect.Maps;
20import com.google.common.collect.Sets;
21import org.onlab.metrics.MetricsComponent;
22import org.onlab.metrics.MetricsFeature;
23import org.onlab.metrics.MetricsService;
24import org.onosproject.cpman.ControlMetricType;
25import org.onosproject.cpman.ControlResource;
26import org.slf4j.Logger;
27
28import java.util.Map;
29import java.util.Optional;
30import java.util.Set;
31
32import static org.slf4j.LoggerFactory.getLogger;
33
34/**
35 * Aggregate system metrics.
36 */
Jian Lia73fce32016-04-11 17:08:35 -070037public final class SystemMetricsAggregator {
Jian Li7eed4172016-04-07 22:12:03 -070038
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 Lia73fce32016-04-11 17:08:35 -070045 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 Li7eed4172016-04-07 22:12:03 -070048
Jian Lia73fce32016-04-11 17:08:35 -070049 private MetricsService metricsService;
Jian Li7eed4172016-04-07 22:12:03 -070050
Jian Lia73fce32016-04-11 17:08:35 -070051 public static SystemMetricsAggregator getInstance() {
52 return SingletonHelper.INSTANCE;
Jian Li7eed4172016-04-07 22:12:03 -070053 }
54
55 /**
Jian Lia73fce32016-04-11 17:08:35 -070056 * Configures metric services.
Jian Li7eed4172016-04-07 22:12:03 -070057 *
Jian Lia73fce32016-04-11 17:08:35 -070058 * @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 Li7eed4172016-04-07 22:12:03 -070069 * @param value metric value
70 */
71 public void increment(ControlMetricType type, long value) {
Jian Lia73fce32016-04-11 17:08:35 -070072 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 Li7eed4172016-04-07 22:12:03 -0700148 }
149}