github.com/timstclair/heapster@v0.20.0-alpha1/metrics/processors/node_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  	"k8s.io/heapster/metrics/core"
    22  )
    23  
    24  type NodeAggregator struct {
    25  	MetricsToAggregate []string
    26  }
    27  
    28  func (this *NodeAggregator) Name() string {
    29  	return "node_aggregator"
    30  }
    31  
    32  func (this *NodeAggregator) Process(batch *core.DataBatch) (*core.DataBatch, error) {
    33  	result := core.DataBatch{
    34  		Timestamp:  batch.Timestamp,
    35  		MetricSets: make(map[string]*core.MetricSet),
    36  	}
    37  
    38  	for key, metricSet := range batch.MetricSets {
    39  		result.MetricSets[key] = metricSet
    40  		if metricSetType, found := metricSet.Labels[core.LabelMetricSetType.Key]; found && metricSetType == core.MetricSetTypePod {
    41  			// Aggregating pods
    42  			if nodeName, found := metricSet.Labels[core.LabelNodename.Key]; found {
    43  				nodeKey := core.NodeKey(nodeName)
    44  				node, found := result.MetricSets[nodeKey]
    45  				if !found {
    46  					if node, found = batch.MetricSets[nodeKey]; !found {
    47  						glog.Warningf("Failed to find node: %s", nodeKey)
    48  						continue
    49  					}
    50  				}
    51  				if err := aggregate(metricSet, node, this.MetricsToAggregate); err != nil {
    52  					return nil, err
    53  				}
    54  			} else {
    55  				return nil, fmt.Errorf("No node info in pod %s: %v", key, metricSet.Labels)
    56  			}
    57  		}
    58  	}
    59  
    60  	return &result, nil
    61  }