func convertToDistributionValue()

in prometheus-to-sd/translator/translator.go [356:406]


func convertToDistributionValue(h *dto.Histogram) *distribution.Distribution {
	count := int64(h.GetSampleCount())
	mean := float64(0)
	dev := float64(0)
	var bounds []float64
	var values []int64

	if count > 0 {
		mean = h.GetSampleSum() / float64(count)
	}

	prevVal := uint64(0)
	lower := float64(0)
	infSeen := false
	for _, b := range h.Bucket {
		upper := b.GetUpperBound()
		if !math.IsInf(b.GetUpperBound(), 1) {
			bounds = append(bounds, b.GetUpperBound())
		} else {
			infSeen = true
			upper = lower
		}
		val := b.GetCumulativeCount() - prevVal
		x := (lower + upper) / float64(2)
		dev += float64(val) * (x - mean) * (x - mean)

		values = append(values, int64(b.GetCumulativeCount()-prevVal))

		lower = b.GetUpperBound()
		prevVal = b.GetCumulativeCount()
	}

	// +Inf Bucket is implicit so it needs to be added
	if !infSeen && count > int64(prevVal) {
		values = append(values, count-int64(prevVal))
	}

	return &distribution.Distribution{
		Count:                 count,
		Mean:                  mean,
		SumOfSquaredDeviation: dev,
		BucketOptions: &distribution.Distribution_BucketOptions{
			Options: &distribution.Distribution_BucketOptions_ExplicitBuckets{
				ExplicitBuckets: &distribution.Distribution_BucketOptions_Explicit{
					Bounds: bounds,
				},
			},
		},
		BucketCounts: values,
	}
}