blob: 900ddea20f354b47bef5f950cc2769e0c5361323 [file] [log] [blame]
Jian Li7eed4172016-04-07 22:12:03 -07001/*
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 */
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 */
37public 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}