github.com/timstclair/heapster@v0.20.0-alpha1/metrics/processors/cluster_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 "k8s.io/heapster/metrics/core"
    18  
    19  type ClusterAggregator struct {
    20  	MetricsToAggregate []string
    21  }
    22  
    23  func (this *ClusterAggregator) Name() string {
    24  	return "cluster_aggregator"
    25  }
    26  
    27  func (this *ClusterAggregator) Process(batch *core.DataBatch) (*core.DataBatch, error) {
    28  	result := core.DataBatch{
    29  		Timestamp:  batch.Timestamp,
    30  		MetricSets: make(map[string]*core.MetricSet),
    31  	}
    32  
    33  	for key, metricSet := range batch.MetricSets {
    34  		result.MetricSets[key] = metricSet
    35  		if metricSetType, found := metricSet.Labels[core.LabelMetricSetType.Key]; found &&
    36  			(metricSetType == core.MetricSetTypeNamespace || metricSetType == core.MetricSetTypeSystemContainer) {
    37  			clusterKey := core.ClusterKey()
    38  			cluster, found := result.MetricSets[clusterKey]
    39  			if !found {
    40  				cluster = clusterMetricSet()
    41  				result.MetricSets[clusterKey] = cluster
    42  			}
    43  			if err := aggregate(metricSet, cluster, this.MetricsToAggregate); err != nil {
    44  				return nil, err
    45  			}
    46  		}
    47  	}
    48  
    49  	return &result, nil
    50  }
    51  
    52  func clusterMetricSet() *core.MetricSet {
    53  	return &core.MetricSet{
    54  		MetricValues: make(map[string]core.MetricValue),
    55  		Labels: map[string]string{
    56  			core.LabelMetricSetType.Key: core.MetricSetTypeCluster,
    57  		},
    58  	}
    59  }