github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/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.LabelNamespaceName, 29 core.LabelPodNamespaceUID, 30 core.LabelHostname, 31 core.LabelHostID, 32 } 33 34 type PodAggregator struct { 35 skippedMetrics map[string]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 newPods := make(map[string]*core.MetricSet) 44 45 // If pod already has pod-level metrics, it no longer needs to aggregates its container's metrics. 46 requireAggregate := make(map[string]bool) 47 for key, metricSet := range batch.MetricSets { 48 if metricSetType, found := metricSet.Labels[core.LabelMetricSetType.Key]; !found || metricSetType != core.MetricSetTypePodContainer { 49 continue 50 } 51 52 // Aggregating containers 53 podName, found := metricSet.Labels[core.LabelPodName.Key] 54 ns, found2 := metricSet.Labels[core.LabelNamespaceName.Key] 55 if !found || !found2 { 56 glog.Errorf("No namespace and/or pod info in container %s: %v", key, metricSet.Labels) 57 continue 58 } 59 60 podKey := core.PodKey(ns, podName) 61 pod, found := batch.MetricSets[podKey] 62 if !found { 63 pod, found = newPods[podKey] 64 if !found { 65 glog.V(2).Infof("Pod not found adding %s", podKey) 66 pod = this.podMetricSet(metricSet.Labels) 67 newPods[podKey] = pod 68 } 69 } 70 71 for metricName, metricValue := range metricSet.MetricValues { 72 if _, found := this.skippedMetrics[metricName]; found { 73 continue 74 } 75 76 aggregatedValue, found := pod.MetricValues[metricName] 77 if !found { 78 requireAggregate[podKey+metricName] = true 79 aggregatedValue = metricValue 80 } else { 81 if requireAggregate[podKey+metricName] { 82 if aggregatedValue.ValueType != metricValue.ValueType { 83 glog.Errorf("PodAggregator: inconsistent type in %s", metricName) 84 continue 85 } 86 87 switch aggregatedValue.ValueType { 88 case core.ValueInt64: 89 aggregatedValue.IntValue += metricValue.IntValue 90 case core.ValueFloat: 91 aggregatedValue.FloatValue += metricValue.FloatValue 92 default: 93 return nil, fmt.Errorf("PodAggregator: type not supported in %s", metricName) 94 } 95 } 96 } 97 98 pod.MetricValues[metricName] = aggregatedValue 99 } 100 } 101 for key, val := range newPods { 102 batch.MetricSets[key] = val 103 } 104 return batch, nil 105 } 106 107 func (this *PodAggregator) podMetricSet(labels map[string]string) *core.MetricSet { 108 newLabels := map[string]string{ 109 core.LabelMetricSetType.Key: core.MetricSetTypePod, 110 } 111 for _, l := range LabelsToPopulate { 112 if val, ok := labels[l.Key]; ok { 113 newLabels[l.Key] = val 114 } 115 } 116 return &core.MetricSet{ 117 MetricValues: make(map[string]core.MetricValue), 118 Labels: newLabels, 119 } 120 } 121 122 func NewPodAggregator() *PodAggregator { 123 skipped := make(map[string]struct{}) 124 for _, metric := range core.StandardMetrics { 125 if metric.MetricDescriptor.Type == core.MetricCumulative || 126 metric.MetricDescriptor.Type == core.MetricDelta { 127 skipped[metric.MetricDescriptor.Name] = struct{}{} 128 } 129 } 130 return &PodAggregator{ 131 skippedMetrics: skipped, 132 } 133 }