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

Function TestBatchHistogram

prometheus/go_collector_latest_test.go:151–231  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

149var sink interface{}
150
151func TestBatchHistogram(t *testing.T) {
152 goMetrics := collectGoMetrics(t, internal.GoCollectorOptions{
153 RuntimeMetricRules: []internal.GoCollectorRule{
154 {Matcher: regexp.MustCompile("/.*")},
155 },
156 })
157
158 var mhist Metric
159 for _, m := range goMetrics {
160 if m.Desc().fqName == "go_gc_heap_allocs_by_size_bytes" {
161 mhist = m
162 break
163 }
164 }
165 if mhist == nil {
166 t.Fatal("failed to find metric to test")
167 }
168 hist, ok := mhist.(*batchHistogram)
169 if !ok {
170 t.Fatal("found metric is not a runtime/metrics histogram")
171 }
172
173 // Make a bunch of allocations then do another collection.
174 //
175 // The runtime/metrics API tries to reuse memory where possible,
176 // so make sure that we didn't hang on to any of that memory in
177 // hist.
178 countsCopy := make([]uint64, len(hist.counts))
179 copy(countsCopy, hist.counts)
180 for i := 0; i < 100; i++ {
181 sink = make([]byte, 128)
182 }
183
184 collectGoMetrics(t, defaultGoCollectorOptions())
185 for i, v := range hist.counts {
186 if v != countsCopy[i] {
187 t.Error("counts changed during new collection")
188 break
189 }
190 }
191
192 // Get the runtime/metrics copy.
193 s := []metrics.Sample{
194 {Name: "/gc/heap/allocs-by-size:bytes"},
195 }
196 metrics.Read(s)
197 rmHist := s[0].Value.Float64Histogram()
198 wantBuckets := internal.RuntimeMetricsBucketsForUnit(rmHist.Buckets, "bytes")
199 // runtime/metrics histograms always have a +Inf bucket and are lower
200 // bound inclusive. In contrast, we have an implicit +Inf bucket and
201 // are upper bound inclusive, so we can chop off the first bucket
202 // (since the conversion to upper bound inclusive will shift all buckets
203 // down one index) and the +Inf for the last bucket.
204 wantBuckets = wantBuckets[1 : len(wantBuckets)-1]
205
206 // Check to make sure the output proto makes sense.
207 pb := &dto.Metric{}
208 hist.Write(pb)

Callers

nothing calls this directly

Calls 6

collectGoMetricsFunction · 0.85
DescMethod · 0.65
WriteMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected