MCPcopy
hub / github.com/prometheus/client_golang / ToFloat64

Function ToFloat64

prometheus/testutil/testutil.go:82–119  ·  view source on GitHub ↗

ToFloat64 collects all Metrics from the provided Collector. It expects that this results in exactly one Metric being collected, which must be a Gauge, Counter, or Untyped. In all other cases, ToFloat64 panics. ToFloat64 returns the value of the collected Metric. The Collector provided is typically

(c prometheus.Collector)

Source from the content-addressed store, hash-verified

80// considering Prometheus metrics) and then expose the number with a
81// prometheus.GaugeFunc.
82func ToFloat64(c prometheus.Collector) float64 {
83 var (
84 m prometheus.Metric
85 mCount int
86 mChan = make(chan prometheus.Metric)
87 done = make(chan struct{})
88 )
89
90 go func() {
91 for m = range mChan {
92 mCount++
93 }
94 close(done)
95 }()
96
97 c.Collect(mChan)
98 close(mChan)
99 <-done
100
101 if mCount != 1 {
102 panic(fmt.Errorf("collected %d metrics instead of exactly 1", mCount))
103 }
104
105 pb := &dto.Metric{}
106 if err := m.Write(pb); err != nil {
107 panic(fmt.Errorf("error happened while collecting metrics: %w", err))
108 }
109 if pb.Gauge != nil {
110 return pb.Gauge.GetValue()
111 }
112 if pb.Counter != nil {
113 return pb.Counter.GetValue()
114 }
115 if pb.Untyped != nil {
116 return pb.Untyped.GetValue()
117 }
118 panic(fmt.Errorf("collected a non-gauge/counter/untyped metric: %s", pb))
119}
120
121// CollectAndCount registers the provided Collector with a newly created
122// pedantic Registry. It then calls GatherAndCount with that Registry and with

Callers 1

TestToFloat64Function · 0.85

Calls 3

CollectMethod · 0.65
WriteMethod · 0.65
GetValueMethod · 0.45

Tested by 1

TestToFloat64Function · 0.68