github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/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 log 16 17 import ( 18 "bytes" 19 "fmt" 20 "sort" 21 22 "github.com/golang/glog" 23 "k8s.io/heapster/metrics/core" 24 ) 25 26 type LogSink struct { 27 } 28 29 func (this *LogSink) Name() string { 30 return "Log Sink" 31 } 32 33 func (this *LogSink) Stop() { 34 // Do nothing. 35 } 36 37 func batchToString(batch *core.DataBatch) string { 38 var buffer bytes.Buffer 39 buffer.WriteString(fmt.Sprintf("DataBatch Timestamp: %s\n\n", batch.Timestamp)) 40 for _, key := range sortedMetricSetKeys(batch.MetricSets) { 41 ms := batch.MetricSets[key] 42 buffer.WriteString(fmt.Sprintf("MetricSet: %s\n", key)) 43 padding := " " 44 buffer.WriteString(fmt.Sprintf("%sScrape time: %v %v\n", padding, ms.ScrapeTime, ms.ScrapeTime.UnixNano())) 45 buffer.WriteString(fmt.Sprintf("%sCreate time: %v %v\n", padding, ms.CollectionStartTime, ms.CollectionStartTime.UnixNano())) 46 buffer.WriteString(fmt.Sprintf("%sLabels:\n", padding)) 47 for _, labelName := range sortedLabelKeys(ms.Labels) { 48 labelValue := ms.Labels[labelName] 49 buffer.WriteString(fmt.Sprintf("%s%s%s = %s\n", padding, padding, labelName, labelValue)) 50 } 51 buffer.WriteString(fmt.Sprintf("%sMetrics:\n", padding)) 52 for _, metricName := range sortedMetricValueKeys(ms.MetricValues) { 53 metricValue := ms.MetricValues[metricName] 54 if core.ValueInt64 == metricValue.ValueType { 55 buffer.WriteString(fmt.Sprintf("%s%s%s = %d\n", padding, padding, metricName, metricValue.IntValue)) 56 } else if core.ValueFloat == metricValue.ValueType { 57 buffer.WriteString(fmt.Sprintf("%s%s%s = %f\n", padding, padding, metricName, metricValue.FloatValue)) 58 } else { 59 buffer.WriteString(fmt.Sprintf("%s%s%s = ?\n", padding, padding, metricName)) 60 } 61 } 62 buffer.WriteString(fmt.Sprintf("%sLabeled Metrics:\n", padding)) 63 for _, metric := range ms.LabeledMetrics { 64 if core.ValueInt64 == metric.ValueType { 65 buffer.WriteString(fmt.Sprintf("%s%s%s = %d\n", padding, padding, metric.Name, metric.IntValue)) 66 } else if core.ValueFloat == metric.ValueType { 67 buffer.WriteString(fmt.Sprintf("%s%s%s = %f\n", padding, padding, metric.Name, metric.FloatValue)) 68 } else { 69 buffer.WriteString(fmt.Sprintf("%s%s%s = ?\n", padding, padding, metric.Name)) 70 } 71 for labelName, labelValue := range metric.Labels { 72 buffer.WriteString(fmt.Sprintf("%s%s%s%s = %s\n", padding, padding, padding, labelName, labelValue)) 73 } 74 } 75 buffer.WriteString("\n") 76 } 77 return buffer.String() 78 } 79 80 func (this *LogSink) ExportData(batch *core.DataBatch) { 81 glog.Info(batchToString(batch)) 82 } 83 84 func NewLogSink() *LogSink { 85 return &LogSink{} 86 } 87 88 func sortedMetricSetKeys(m map[string]*core.MetricSet) []string { 89 keys := make([]string, len(m)) 90 i := 0 91 for k := range m { 92 keys[i] = k 93 i++ 94 } 95 sort.Strings(keys) 96 return keys 97 } 98 99 func sortedLabelKeys(m map[string]string) []string { 100 keys := make([]string, len(m)) 101 i := 0 102 for k := range m { 103 keys[i] = k 104 i++ 105 } 106 sort.Strings(keys) 107 return keys 108 } 109 110 func sortedMetricValueKeys(m map[string]core.MetricValue) []string { 111 keys := make([]string, len(m)) 112 i := 0 113 for k := range m { 114 keys[i] = k 115 i++ 116 } 117 sort.Strings(keys) 118 return keys 119 }