github.com/timstclair/heapster@v0.20.0-alpha1/metrics/sinks/factory.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 sinks
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"github.com/golang/glog"
    22  	"k8s.io/heapster/common/flags"
    23  	"k8s.io/heapster/metrics/core"
    24  	"k8s.io/heapster/metrics/sinks/gcm"
    25  	"k8s.io/heapster/metrics/sinks/hawkular"
    26  	"k8s.io/heapster/metrics/sinks/influxdb"
    27  	"k8s.io/heapster/metrics/sinks/kafka"
    28  	"k8s.io/heapster/metrics/sinks/log"
    29  	"k8s.io/heapster/metrics/sinks/metric"
    30  	"k8s.io/heapster/metrics/sinks/riemann"
    31  )
    32  
    33  type SinkFactory struct {
    34  }
    35  
    36  func (this *SinkFactory) Build(uri flags.Uri) (core.DataSink, error) {
    37  	switch uri.Key {
    38  	case "gcm":
    39  		return gcm.CreateGCMSink(&uri.Val)
    40  	case "hawkular":
    41  		return hawkular.NewHawkularSink(&uri.Val)
    42  	case "influxdb":
    43  		return influxdb.CreateInfluxdbSink(&uri.Val)
    44  	case "kafka":
    45  		return kafka.NewKafkaSink(&uri.Val)
    46  	case "log":
    47  		return logsink.NewLogSink(), nil
    48  	case "metric":
    49  		return metricsink.NewMetricSink(140*time.Second, 15*time.Minute, []string{
    50  			core.MetricCpuUsageRate.MetricDescriptor.Name,
    51  			core.MetricMemoruUsage.MetricDescriptor.Name}), nil
    52  	case "riemann":
    53  		return riemann.CreateRiemannSink(&uri.Val)
    54  	default:
    55  		return nil, fmt.Errorf("Sink not recognized: %s", uri.Key)
    56  	}
    57  }
    58  
    59  func (this *SinkFactory) BuildAll(uris flags.Uris) (*metricsink.MetricSink, []core.DataSink) {
    60  	result := make([]core.DataSink, 0, len(uris))
    61  	var metric *metricsink.MetricSink
    62  	for _, uri := range uris {
    63  		sink, err := this.Build(uri)
    64  		if err != nil {
    65  			glog.Errorf("Failed to create sink: %v", err)
    66  			continue
    67  		}
    68  		if uri.Key == "metric" {
    69  			metric = sink.(*metricsink.MetricSink)
    70  		}
    71  		result = append(result, sink)
    72  	}
    73  	if metric == nil {
    74  		uri := flags.Uri{}
    75  		uri.Set("metric")
    76  		sink, err := this.Build(uri)
    77  		if err == nil {
    78  			result = append(result, sink)
    79  			metric = sink.(*metricsink.MetricSink)
    80  		} else {
    81  			glog.Errorf("Error while creating metric sink: %v", err)
    82  		}
    83  	}
    84  	return metric, result
    85  }
    86  
    87  func NewSinkFactory() *SinkFactory {
    88  	return &SinkFactory{}
    89  }