MCPcopy
hub / github.com/prometheus/prometheus / ToFloat

Method ToFloat

model/histogram/histogram.go:365–409  ·  view source on GitHub ↗

ToFloat returns a FloatHistogram representation of the Histogram. It is a deep copy (e.g. spans are not shared). The function accepts a FloatHistogram as an argument whose memory will be reused and overwritten if provided. If this argument is nil, a new FloatHistogram will be allocated.

(fh *FloatHistogram)

Source from the content-addressed store, hash-verified

363// argument whose memory will be reused and overwritten if provided. If this
364// argument is nil, a new FloatHistogram will be allocated.
365func (h *Histogram) ToFloat(fh *FloatHistogram) *FloatHistogram {
366 if fh == nil {
367 fh = &FloatHistogram{}
368 }
369 fh.CounterResetHint = h.CounterResetHint
370 fh.Schema = h.Schema
371 fh.Count = float64(h.Count)
372 fh.Sum = h.Sum
373
374 if h.UsesCustomBuckets() {
375 fh.ZeroThreshold = 0
376 fh.ZeroCount = 0
377 fh.NegativeSpans = clearIfNotNil(fh.NegativeSpans)
378 fh.NegativeBuckets = clearIfNotNil(fh.NegativeBuckets)
379 // Custom values are interned, it's ok to copy by reference.
380 fh.CustomValues = h.CustomValues
381 } else {
382 fh.ZeroThreshold = h.ZeroThreshold
383 fh.ZeroCount = float64(h.ZeroCount)
384
385 fh.NegativeSpans = resize(fh.NegativeSpans, len(h.NegativeSpans))
386 copy(fh.NegativeSpans, h.NegativeSpans)
387
388 fh.NegativeBuckets = resize(fh.NegativeBuckets, len(h.NegativeBuckets))
389 var currentNegative float64
390 for i, b := range h.NegativeBuckets {
391 currentNegative += float64(b)
392 fh.NegativeBuckets[i] = currentNegative
393 }
394 // Custom values are interned, no need to reset.
395 fh.CustomValues = nil
396 }
397
398 fh.PositiveSpans = resize(fh.PositiveSpans, len(h.PositiveSpans))
399 copy(fh.PositiveSpans, h.PositiveSpans)
400
401 fh.PositiveBuckets = resize(fh.PositiveBuckets, len(h.PositiveBuckets))
402 var currentPositive float64
403 for i, b := range h.PositiveBuckets {
404 currentPositive += float64(b)
405 fh.PositiveBuckets[i] = currentPositive
406 }
407
408 return fh
409}
410
411func resize[T any](items []T, n int) []T {
412 if cap(items) < n {

Calls 3

UsesCustomBucketsMethod · 0.95
clearIfNotNilFunction · 0.85
resizeFunction · 0.70