From 96eeddd552342e55a7be220e5da2ab33db9c197c Mon Sep 17 00:00:00 2001 From: Shivanth Date: Fri, 18 Oct 2024 11:44:31 +0200 Subject: [PATCH] Fix test and references --- prometheus/histogram.go | 30 +++++++++++++++++------------- prometheus/histogram_test.go | 8 ++++---- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/prometheus/histogram.go b/prometheus/histogram.go index efaa561e3..a6dcadb9b 100644 --- a/prometheus/histogram.go +++ b/prometheus/histogram.go @@ -1844,7 +1844,7 @@ type constNativeHistogram struct { nativeHistogramSchema int32 nativeHistogramZeroThreshold float64 nativeHistogramMaxZeroThreshold float64 - createdTimestamp time.Time + createdTimestamp time.Time nativeExemplars []*dto.Exemplar positiveBuckets map[int]int64 @@ -1852,29 +1852,33 @@ type constNativeHistogram struct { zeroBucket uint64 } -func NewconstNativeHistogram(desc *Desc, count uint64, sum float64, postiveBuckets, negativeBuckets map[int]int64, zeroBucket uint64, - labelPairs []*dto.LabelPair, nativeHistogramSchema int32, nativeHistogramZeroThreshold float64, - nativeHistogramMaxZeroThreshold float64, nativeHistogramMaxBuckets uint32, - nativeHistogramMinResetDuration time.Duration, +func NewConstNativeHistogram(desc *Desc, count uint64, sum float64, postiveBuckets, negativeBuckets map[int]int64, zeroBucket uint64, + nativeHistogramSchema int32, nativeHistogramZeroThreshold float64, + nativeHistogramMaxZeroThreshold float64, createdTimestamp time.Time, nativeExemplars []*dto.Exemplar, -) (constNativeHistogram, error) { - return constNativeHistogram{ + labelValues ...string, +) (Metric, error) { + if desc.err != nil { + return nil, desc.err + } + if err := validateLabelValues(labelValues, len(desc.variableLabels.names)); err != nil { + return nil, err + } + return &constNativeHistogram{ desc: desc, count: count, sum: sum, positiveBuckets: postiveBuckets, negativeBuckets: negativeBuckets, zeroBucket: zeroBucket, - labelPairs: labelPairs, + labelPairs: MakeLabelPairs(desc, labelValues), nativeHistogramSchema: nativeHistogramSchema, nativeHistogramZeroThreshold: nativeHistogramZeroThreshold, nativeHistogramMaxZeroThreshold: nativeHistogramMaxZeroThreshold, - nativeHistogramMaxBuckets: nativeHistogramMaxBuckets, - nativeHistogramMinResetDuration: nativeHistogramMinResetDuration, - timeStamp: timeStamp, + createdTimestamp: createdTimestamp, nativeExemplars: nativeExemplars, - } + }, nil } func (h *constNativeHistogram) Desc() *Desc { @@ -1883,7 +1887,7 @@ func (h *constNativeHistogram) Desc() *Desc { func (h *constNativeHistogram) Write(out *dto.Metric) error { his := &dto.Histogram{ - CreatedTimestamp: timestamppb.New(h.timeStamp), + CreatedTimestamp: timestamppb.New(h.createdTimestamp), Schema: &h.nativeHistogramSchema, ZeroThreshold: &h.nativeHistogramZeroThreshold, Exemplars: h.nativeExemplars, diff --git a/prometheus/histogram_test.go b/prometheus/histogram_test.go index 6cd26186d..d910a7443 100644 --- a/prometheus/histogram_test.go +++ b/prometheus/histogram_test.go @@ -1971,21 +1971,21 @@ func TestConstNativeHistogram(t *testing.T) { n := atomic.LoadUint64(&_his.countAndHotIdx) hotIdx := n >> 63 cold := _his.counts[hotIdx] - consthist := NewconstNativeHistogram(_his.Desc(), + consthist, err := NewConstNativeHistogram(_his.Desc(), cold.count, math.Float64frombits(cold.sumBits), SyncMaptomap(&cold.nativeHistogramBucketsPositive), SyncMaptomap(&cold.nativeHistogramBucketsNegative), cold.nativeHistogramZeroBucket, - _his.labelPairs, cold.nativeHistogramSchema, math.Float64frombits(cold.nativeHistogramZeroThresholdBits), _his.nativeHistogramMaxZeroThreshold, - _his.nativeHistogramMaxBuckets, - _his.nativeHistogramMinResetDuration, _his.lastResetTime, _his.nativeExemplars.exemplars, ) + if err != nil { + t.Fatal("unexpected error writing metric", err) + } m2 := &dto.Metric{} if err := consthist.Write(m2); err != nil {