github.com/timstclair/heapster@v0.20.0-alpha1/metrics/sinks/log/log_sink.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 logsink
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  
    21  	"github.com/golang/glog"
    22  	"k8s.io/heapster/metrics/core"
    23  )
    24  
    25  type LogSink struct {
    26  }
    27  
    28  func (this *LogSink) Name() string {
    29  	return "LogSink"
    30  }
    31  
    32  func (this *LogSink) Stop() {
    33  	// Do nothing.
    34  }
    35  
    36  func batchToString(batch *core.DataBatch) string {
    37  	var buffer bytes.Buffer
    38  	buffer.WriteString(fmt.Sprintf("DataBatch     Timestamp: %s\n\n", batch.Timestamp))
    39  	for key, ms := range batch.MetricSets {
    40  		buffer.WriteString(fmt.Sprintf("MetricSet: %s\n", key))
    41  		padding := "   "
    42  		buffer.WriteString(fmt.Sprintf("%sLabels:\n", padding))
    43  		for labelName, labelValue := range ms.Labels {
    44  			buffer.WriteString(fmt.Sprintf("%s%s%s = %s\n", padding, padding, labelName, labelValue))
    45  		}
    46  		buffer.WriteString(fmt.Sprintf("%sMetrics:\n", padding))
    47  		for metricName, metricValue := range ms.MetricValues {
    48  			if core.ValueInt64 == metricValue.ValueType {
    49  				buffer.WriteString(fmt.Sprintf("%s%s%s = %d\n", padding, padding, metricName, metricValue.IntValue))
    50  			} else if core.ValueFloat == metricValue.ValueType {
    51  				buffer.WriteString(fmt.Sprintf("%s%s%s = %f\n", padding, padding, metricName, metricValue.FloatValue))
    52  			} else {
    53  				buffer.WriteString(fmt.Sprintf("%s%s%s = ?\n", padding, padding, metricName))
    54  			}
    55  		}
    56  		buffer.WriteString("\n")
    57  	}
    58  	return buffer.String()
    59  }
    60  
    61  func (this *LogSink) ExportData(batch *core.DataBatch) {
    62  	glog.Info(batchToString(batch))
    63  }
    64  
    65  func NewLogSink() *LogSink {
    66  	return &LogSink{}
    67  }