github.com/jonaz/heapster@v1.3.0-beta.0.0.20170208112634-cd3c15ca3d29/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 skippedMetrics map[string]struct{} 37 } 38 39 func (this *PodAggregator) Name() string { 40 return "pod_aggregator" 41 } 42 43 func (this *PodAggregator) Process(batch *core.DataBatch) (*core.DataBatch, error) { 44 newPods := make(map[string]*core.MetricSet) 45 46 for key, metricSet := range batch.MetricSets { 47 if metricSetType, found := metricSet.Labels[core.LabelMetricSetType.Key]; found && metricSetType == core.MetricSetTypePodContainer { 48 // Aggregating containers 49 podName, found := metricSet.Labels[core.LabelPodName.Key] 50 ns, found2 := metricSet.Labels[core.LabelNamespaceName.Key] 51 if found && found2 { 52 podKey := core.PodKey(ns, podName) 53 pod, found := batch.MetricSets[podKey] 54 if !found { 55 pod, found = newPods[podKey] 56 if !found { 57 glog.V(2).Infof("Pod not found adding %s", podKey) 58 pod = this.podMetricSet(metricSet.Labels) 59 newPods[podKey] = pod 60 } 61 } 62 63 for metricName, metricValue := range metricSet.MetricValues { 64 if _, found := this.skippedMetrics[metricName]; found { 65 continue 66 } 67 68 aggregatedValue, found := pod.MetricValues[metricName] 69 if found { 70 if aggregatedValue.ValueType != metricValue.ValueType { 71 glog.Errorf("PodAggregator: inconsistent type in %s", metricName) 72 continue 73 } 74 75 switch aggregatedValue.ValueType { 76 case core.ValueInt64: 77 aggregatedValue.IntValue += metricValue.IntValue 78 case core.ValueFloat: 79 aggregatedValue.FloatValue += metricValue.FloatValue 80 default: 81 return nil, fmt.Errorf("PodAggregator: type not supported in %s", metricName) 82 } 83 } else { 84 aggregatedValue = metricValue 85 } 86 pod.MetricValues[metricName] = aggregatedValue 87 } 88 } else { 89 glog.Errorf("No namespace and/or pod info in container %s: %v", key, metricSet.Labels) 90 continue 91 } 92 } 93 } 94 for key, val := range newPods { 95 batch.MetricSets[key] = val 96 } 97 return batch, nil 98 } 99 100 func (this *PodAggregator) podMetricSet(labels map[string]string) *core.MetricSet { 101 newLabels := map[string]string{ 102 core.LabelMetricSetType.Key: core.MetricSetTypePod, 103 } 104 for _, l := range LabelsToPopulate { 105 if val, ok := labels[l.Key]; ok { 106 newLabels[l.Key] = val 107 } 108 } 109 return &core.MetricSet{ 110 MetricValues: make(map[string]core.MetricValue), 111 Labels: newLabels, 112 } 113 } 114 115 func NewPodAggregator() *PodAggregator { 116 skipped := make(map[string]struct{}) 117 for _, metric := range core.StandardMetrics { 118 if metric.MetricDescriptor.Type == core.MetricCumulative || 119 metric.MetricDescriptor.Type == core.MetricDelta { 120 skipped[metric.MetricDescriptor.Name] = struct{}{} 121 } 122 } 123 return &PodAggregator{ 124 skippedMetrics: skipped, 125 } 126 }