github.com/timstclair/heapster@v0.20.0-alpha1/metrics/processors/pod_aggregator.go (about) 1 // Copyright 2015 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package processors 16 17 import ( 18 "fmt" 19 20 "github.com/golang/glog" 21 22 "k8s.io/heapster/metrics/core" 23 ) 24 25 var LabelsToPopulate = []core.LabelDescriptor{ 26 core.LabelPodId, 27 core.LabelPodName, 28 core.LabelPodNamespace, 29 core.LabelNamespaceName, 30 core.LabelPodNamespaceUID, 31 core.LabelHostname, 32 core.LabelHostID, 33 } 34 35 type PodAggregator struct { 36 } 37 38 func (this *PodAggregator) Name() string { 39 return "pod_aggregator" 40 } 41 42 func (this *PodAggregator) Process(batch *core.DataBatch) (*core.DataBatch, error) { 43 result := core.DataBatch{ 44 Timestamp: batch.Timestamp, 45 MetricSets: make(map[string]*core.MetricSet), 46 } 47 for key, metricSet := range batch.MetricSets { 48 result.MetricSets[key] = metricSet 49 if metricSetType, found := metricSet.Labels[core.LabelMetricSetType.Key]; found && metricSetType == core.MetricSetTypePodContainer { 50 // Aggregating containers 51 podName, found := metricSet.Labels[core.LabelPodName.Key] 52 ns, found2 := metricSet.Labels[core.LabelNamespaceName.Key] 53 if found && found2 { 54 podKey := core.PodKey(ns, podName) 55 pod, found := result.MetricSets[podKey] 56 if !found { 57 pod, found = batch.MetricSets[podKey] 58 if !found { 59 glog.V(2).Infof("Pod not found adding %s", podKey) 60 pod = this.podMetricSet(metricSet.Labels) 61 result.MetricSets[podKey] = pod 62 } 63 } 64 65 for metricName, metricValue := range metricSet.MetricValues { 66 aggregatedValue, found := pod.MetricValues[metricName] 67 if found { 68 if aggregatedValue.ValueType != metricValue.ValueType { 69 glog.Errorf("PodAggregator: inconsistent type in %s", metricName) 70 continue 71 } 72 73 switch aggregatedValue.ValueType { 74 case core.ValueInt64: 75 aggregatedValue.IntValue += metricValue.IntValue 76 case core.ValueFloat: 77 aggregatedValue.FloatValue += metricValue.FloatValue 78 default: 79 return nil, fmt.Errorf("PodAggregator: type not supported in %s", metricName) 80 } 81 } else { 82 aggregatedValue = metricValue 83 } 84 pod.MetricValues[metricName] = aggregatedValue 85 } 86 } else { 87 glog.Errorf("No namespace and/or pod info in container %s: %v", key, metricSet.Labels) 88 continue 89 } 90 } 91 } 92 93 return &result, nil 94 } 95 96 func (this *PodAggregator) podMetricSet(labels map[string]string) *core.MetricSet { 97 newLabels := map[string]string{ 98 core.LabelMetricSetType.Key: core.MetricSetTypePod, 99 } 100 for _, l := range LabelsToPopulate { 101 if val, ok := labels[l.Key]; ok { 102 newLabels[l.Key] = val 103 } 104 } 105 return &core.MetricSet{ 106 MetricValues: make(map[string]core.MetricValue), 107 Labels: newLabels, 108 } 109 }