github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/processors/namespace_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 "github.com/golang/glog" 19 "k8s.io/heapster/metrics/core" 20 ) 21 22 type NamespaceAggregator struct { 23 MetricsToAggregate []string 24 } 25 26 func (this *NamespaceAggregator) Name() string { 27 return "namespace_aggregator" 28 } 29 30 func (this *NamespaceAggregator) Process(batch *core.DataBatch) (*core.DataBatch, error) { 31 namespaces := make(map[string]*core.MetricSet) 32 for key, metricSet := range batch.MetricSets { 33 if metricSetType, found := metricSet.Labels[core.LabelMetricSetType.Key]; !found || metricSetType != core.MetricSetTypePod { 34 continue 35 } 36 37 namespaceName, found := metricSet.Labels[core.LabelNamespaceName.Key] 38 if !found { 39 glog.Errorf("No namespace info in pod %s: %v", key, metricSet.Labels) 40 continue 41 } 42 43 namespaceKey := core.NamespaceKey(namespaceName) 44 namespace, found := namespaces[namespaceKey] 45 if !found { 46 if nsFromBatch, found := batch.MetricSets[namespaceKey]; found { 47 namespace = nsFromBatch 48 } else { 49 namespace = namespaceMetricSet(namespaceName, metricSet.Labels[core.LabelPodNamespaceUID.Key]) 50 namespaces[namespaceKey] = namespace 51 } 52 } 53 54 if err := aggregate(metricSet, namespace, this.MetricsToAggregate); err != nil { 55 return nil, err 56 } 57 58 } 59 for key, val := range namespaces { 60 batch.MetricSets[key] = val 61 } 62 return batch, nil 63 } 64 65 func namespaceMetricSet(namespaceName, uid string) *core.MetricSet { 66 return &core.MetricSet{ 67 MetricValues: make(map[string]core.MetricValue), 68 Labels: map[string]string{ 69 core.LabelMetricSetType.Key: core.MetricSetTypeNamespace, 70 core.LabelNamespaceName.Key: namespaceName, 71 core.LabelPodNamespaceUID.Key: uid, 72 }, 73 } 74 }