Use code that already existed

Signed-off-by: Sevag Hanssian <sevag.hanssian@gmail.com>
This commit is contained in:
Sevag Hanssian 2018-11-02 08:37:08 -07:00
parent 1d54dabd43
commit 42e6616334
No known key found for this signature in database
GPG Key ID: 15472D208B254CF4
2 changed files with 22 additions and 89 deletions

View File

@ -16,15 +16,17 @@ package prometheus
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath"
"runtime" "runtime"
"sort" "sort"
"strconv"
"strings" "strings"
"sync" "sync"
"unicode/utf8" "unicode/utf8"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/prometheus/common/expfmt"
dto "github.com/prometheus/client_model/go" dto "github.com/prometheus/client_model/go"
@ -535,100 +537,29 @@ func (r *Registry) Gather() ([]*dto.MetricFamily, error) {
return internal.NormalizeMetricFamilies(metricFamiliesByName), errs.MaybeUnwrap() return internal.NormalizeMetricFamilies(metricFamiliesByName), errs.MaybeUnwrap()
} }
func (r *Registry) WriteToTextfile(path string) error { // WriteToTextfile formats the metrics of the provided Gatherer interface and
metricFamilies, err := r.Gather() // emits them in text format to the path. Intended for use with the node_exporter
// textfile collector
func WriteToTextfile(filename string, g Gatherer) error {
tmp, err := ioutil.TempFile(filepath.Dir(filename), filepath.Base(filename))
if err != nil { if err != nil {
return err return err
} }
outputMap := map[string][]string{} defer os.Remove(tmp.Name())
metricNames := []string{}
for _, metricFamily := range metricFamilies { mfs, err := g.Gather()
output := []string{} if err != nil {
output = append(output, fmt.Sprintf("# HELP %s %s", metricFamily.GetName(), metricFamily.GetHelp())) return err
output = append(output, fmt.Sprintf("# TYPE %s %s", metricFamily.GetName(), strings.ToLower(metricFamily.GetType().String()))) }
for _, metric := range metricFamily.GetMetric() { for _, mf := range mfs {
labelStrings := []string{} if _, err := expfmt.MetricFamilyToText(tmp, mf); err != nil {
if metric.GetLabel() != nil { return err
for _, labelPair := range metric.GetLabel() {
labelStrings = append(labelStrings, fmt.Sprintf("%s=\"%s\"", labelPair.GetName(), labelPair.GetValue()))
}
}
timestampString := ""
if metric.TimestampMs != nil {
timestampString = fmt.Sprintf(" %d", int(float64(metric.GetTimestampMs())*1000))
}
switch metricFamily.GetType() {
case dto.MetricType_COUNTER:
value := strconv.FormatFloat(metric.GetCounter().GetValue(), 'f', -1, 64)
labelString := fmt.Sprintf("{%s}", strings.Join(labelStrings, ","))
output = append(output, fmt.Sprintf("%s%s %s%s", metricFamily.GetName(), labelString, value, timestampString))
case dto.MetricType_GAUGE:
value := strconv.FormatFloat(metric.GetGauge().GetValue(), 'f', -1, 64)
labelString := fmt.Sprintf("{%s}", strings.Join(labelStrings, ","))
output = append(output, fmt.Sprintf("%s%s %s%s", metricFamily.GetName(), labelString, value, timestampString))
case dto.MetricType_UNTYPED:
value := strconv.FormatFloat(metric.GetUntyped().GetValue(), 'f', -1, 64)
labelString := fmt.Sprintf("{%s}", strings.Join(labelStrings, ","))
output = append(output, fmt.Sprintf("%s%s %s%s", metricFamily.GetName(), labelString, value, timestampString))
case dto.MetricType_SUMMARY:
labelString := fmt.Sprintf("{%s}", strings.Join(labelStrings, ","))
count := metric.GetSummary().GetSampleCount()
sum := strconv.FormatFloat(metric.GetSummary().GetSampleSum(), 'f', -1, 64)
for _, quantile := range metric.GetSummary().GetQuantile() {
quantileName := strconv.FormatFloat(quantile.GetQuantile(), 'f', -1, 64)
quantileLabelStrings := append(labelStrings, fmt.Sprintf("quantile=\"%s\"", quantileName))
loopLabelString := fmt.Sprintf("{%s}", strings.Join(quantileLabelStrings, ","))
value := strconv.FormatFloat(quantile.GetValue(), 'f', -1, 64)
output = append(output, fmt.Sprintf("%s%s %s%s", metricFamily.GetName(), loopLabelString, value, timestampString))
}
output = append(output, fmt.Sprintf("%s_sum%s %s%s", metricFamily.GetName(), labelString, sum, timestampString))
output = append(output, fmt.Sprintf("%s_count%s %d%s", metricFamily.GetName(), labelString, count, timestampString))
case dto.MetricType_HISTOGRAM:
labelString := fmt.Sprintf("{%s}", strings.Join(labelStrings, ","))
count := metric.GetHistogram().GetSampleCount()
sum := strconv.FormatFloat(metric.GetHistogram().GetSampleSum(), 'f', -1, 64)
for _, bucket := range metric.GetHistogram().GetBucket() {
bucketUpperBound := strconv.FormatFloat(bucket.GetUpperBound(), 'f', -1, 64)
bucketLabelStrings := append(labelStrings, fmt.Sprintf("le=\"%s\"", bucketUpperBound))
loopLabelString := fmt.Sprintf("{%s}", strings.Join(bucketLabelStrings, ","))
value := bucket.GetCumulativeCount()
output = append(output, fmt.Sprintf("%s_bucket%s %d%s", metricFamily.GetName(), loopLabelString, value, timestampString))
}
infBucketLabelStrings := append(labelStrings, "le=\"+Inf\"")
infLabelString := fmt.Sprintf("{%s}", strings.Join(infBucketLabelStrings, ","))
output = append(output, fmt.Sprintf("%s_bucket%s %d%s", metricFamily.GetName(), infLabelString, count, timestampString))
output = append(output, fmt.Sprintf("%s_sum%s %s%s", metricFamily.GetName(), labelString, sum, timestampString))
output = append(output, fmt.Sprintf("%s_count%s %d%s", metricFamily.GetName(), labelString, count, timestampString))
}
} }
outputMap[metricFamily.GetName()] = output
metricNames = append(metricNames, metricFamily.GetName())
} }
if err := tmp.Close(); err != nil {
tmppath := fmt.Sprintf("%s.%d", path, os.Getpid())
f, err := os.Create(tmppath)
if err != nil {
return err return err
} }
return os.Rename(tmp.Name(), filename)
sort.Strings(metricNames)
outStr := ""
for _, metricName := range metricNames {
outStr = fmt.Sprintf("%s%s\n", outStr, strings.Join(outputMap[metricName], "\n"))
}
_, err = f.WriteString(outStr)
if err != nil {
return err
}
if err := os.Rename(tmppath, path); err != nil {
return err
}
return nil
} }
// processMetric is an internal helper method only used by the Gather method. // processMetric is an internal helper method only used by the Gather method.

View File

@ -958,7 +958,9 @@ test_summary_count{name="foo"} 2
} }
defer os.Remove(tmpfile.Name()) defer os.Remove(tmpfile.Name())
registry.WriteToTextfile(tmpfile.Name()) if err := prometheus.WriteToTextfile(tmpfile.Name(), registry); err != nil {
t.Fatal(err)
}
fileBytes, err := ioutil.ReadFile(tmpfile.Name()) fileBytes, err := ioutil.ReadFile(tmpfile.Name())
if err != nil { if err != nil {