Skip to content

Commit

Permalink
Extend collector/collector_test.go to test metric name and help
Browse files Browse the repository at this point in the history
  • Loading branch information
Guido Iaquinti committed Jan 28, 2021
1 parent 9a43c02 commit efab67d
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,44 @@
package collector

import (
"reflect"
"strings"

"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
)

type labelMap map[string]string

type MetricResult struct {
name string
help string
labels labelMap
value float64
metricType dto.MetricType
}

func readMetric(m prometheus.Metric) MetricResult {

a := m.Desc()
pb := &dto.Metric{}
m.Write(pb)
labels := make(labelMap, len(pb.Label))

name := reflect.ValueOf(a).Elem().FieldByName("fqName").String()
help := reflect.ValueOf(a).Elem().FieldByName("help").String()
labels := labelMap{}

for _, v := range pb.Label {
labels[v.GetName()] = v.GetValue()
}

if pb.Gauge != nil {
return MetricResult{labels: labels, value: pb.GetGauge().GetValue(), metricType: dto.MetricType_GAUGE}
return MetricResult{name: name, help: help, labels: labels, value: pb.GetGauge().GetValue(), metricType: dto.MetricType_GAUGE}
}
if pb.Counter != nil {
return MetricResult{labels: labels, value: pb.GetCounter().GetValue(), metricType: dto.MetricType_COUNTER}
return MetricResult{name: name, help: help, labels: labels, value: pb.GetCounter().GetValue(), metricType: dto.MetricType_COUNTER}
}
if pb.Untyped != nil {
return MetricResult{labels: labels, value: pb.GetUntyped().GetValue(), metricType: dto.MetricType_UNTYPED}
return MetricResult{name: name, help: help, labels: labels, value: pb.GetUntyped().GetValue(), metricType: dto.MetricType_UNTYPED}
}
panic("Unsupported metric type")
}
Expand Down

0 comments on commit efab67d

Please sign in to comment.