Skip to content

Commit 44d8019

Browse files
Use Oberve histogram method to properly populate histogram buckets (#843)
1 parent 7468975 commit 44d8019

1 file changed

Lines changed: 5 additions & 28 deletions

File tree

pkg/gatherer/gatherer.go

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
cloudcost_exporter "github.com/grafana/cloudcost-exporter"
99
"github.com/grafana/cloudcost-exporter/pkg/provider"
1010
"github.com/prometheus/client_golang/prometheus"
11-
io_prometheus_client "github.com/prometheus/client_model/go"
1211
)
1312

1413
var gathererDurationHistogramVec = prometheus.NewHistogramVec(
@@ -38,26 +37,13 @@ var gathererTotalCounterVec = prometheus.NewCounterVec(
3837
)
3938

4039
func emitHistogramMetric(ch chan<- prometheus.Metric, collectorName string, duration float64) {
41-
ch <- prometheus.MustNewConstHistogram(
42-
gathererDurationHistogramVec.WithLabelValues(collectorName).(prometheus.Histogram).Desc(),
43-
1,
44-
duration,
45-
nil,
46-
collectorName,
47-
)
40+
h := gathererDurationHistogramVec.WithLabelValues(collectorName).(prometheus.Histogram)
41+
h.Observe(duration)
42+
ch <- h
4843

4944
counter := gathererTotalCounterVec.WithLabelValues(collectorName)
5045
counter.Inc()
51-
52-
m := &io_prometheus_client.Metric{}
53-
if err := counter.Write(m); err == nil && m.Counter != nil {
54-
ch <- prometheus.MustNewConstMetric(
55-
gathererTotalCounterVec.WithLabelValues(collectorName).Desc(),
56-
prometheus.CounterValue,
57-
m.GetCounter().GetValue(),
58-
collectorName,
59-
)
60-
}
46+
ch <- counter
6147
}
6248

6349
// CollectWithGatherer collects metrics from a collector and uses the Gatherer interface to detect errors.
@@ -94,16 +80,7 @@ func CollectWithGatherer(ctx context.Context, c provider.Collector, ch chan<- pr
9480
)
9581
errorCounter := gathererErrorCounterVec.WithLabelValues(c.Name())
9682
errorCounter.Inc()
97-
98-
m := &io_prometheus_client.Metric{}
99-
if err := errorCounter.Write(m); err == nil && m.Counter != nil {
100-
ch <- prometheus.MustNewConstMetric(
101-
gathererErrorCounterVec.WithLabelValues(c.Name()).Desc(),
102-
prometheus.CounterValue,
103-
m.GetCounter().GetValue(),
104-
c.Name(),
105-
)
106-
}
83+
ch <- errorCounter
10784
}
10885

10986
emitHistogramMetric(ch, c.Name(), duration)

0 commit comments

Comments
 (0)