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