Skip to content

Commit

Permalink
First commit - WriteToTextfile
Browse files Browse the repository at this point in the history
Signed-off-by: Sevag Hanssian <[email protected]>
  • Loading branch information
sevagh committed Oct 31, 2018
1 parent f30f428 commit 9416ff2
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions prometheus/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,50 @@ func (r *Registry) Gather() ([]*dto.MetricFamily, error) {
return internal.NormalizeMetricFamilies(metricFamiliesByName), errs.MaybeUnwrap()
}

func (r *Registry) WriteToTextfile(path string) error {
metricFamilies, err := r.Gather()
if err != nil {
return err
}
output := []string{}
for _, metricFamily := range metricFamilies {
output = append(output, fmt.Sprintf("# HELP %s %s", metricFamily.GetName(), metricFamily.GetHelp()))
output = append(output, fmt.Sprintf("# TYPE %s %s", metricFamily.GetName(), metricFamily.GetType().String()))
for _, metric := range metricFamily.GetMetric() {
labelString := ""
if metric.GetLabel() != nil {
labelStrings := []string{}
for _, labelPair := range metric.GetLabel() {
labelStrings = append(labelStrings, fmt.Sprintf("%s=\"%s\"", labelPair.GetName(), labelPair.GetValue()))
}
labelString = fmt.Sprintf("{%s}", strings.Join(labelStrings, ","))
}
timestampString := ""
if metric.TimestampMs != nil {
timestampString = fmt.Sprintf(" %d", int(float64(metric.GetTimestampMs())*1000))
}
var value float64
switch metricFamily.GetType() {
case dto.MetricType_COUNTER:
value = metric.GetCounter().GetValue()
case dto.MetricType_GAUGE:
value = metric.GetGauge().GetValue()
case dto.MetricType_SUMMARY:
//value = metric.GetSummary().GetValue()
//what to do here
case dto.MetricType_HISTOGRAM:
//same
//value = metric.GetHistogram().GetValue()
case dto.MetricType_UNTYPED:
value = metric.GetUntyped().GetValue()
}
output = append(output, fmt.Sprintf("%s%s %f%s", metricFamily.GetName(), labelString, value, timestampString))
}
}
fmt.Println(strings.Join(output, "\n"))
return nil
}

// processMetric is an internal helper method only used by the Gather method.
func processMetric(
metric Metric,
Expand Down

0 comments on commit 9416ff2

Please sign in to comment.