diff --git a/prometheus/histogram.go b/prometheus/histogram.go index af434ae64..6e4cda7b7 100644 --- a/prometheus/histogram.go +++ b/prometheus/histogram.go @@ -678,14 +678,15 @@ func NewConstHistogramWithExemplar( if err := validateLabelValues(labelValues, len(desc.variableLabels)); err != nil { return nil, err } - return &constHistogram{ - desc: desc, - count: count, - sum: sum, - buckets: buckets, - exemplars: exemplars, - labelPairs: MakeLabelPairs(desc, labelValues), - }, nil + + h, err := NewConstHistogram(desc, count, sum, buckets, labelValues...) + if err != nil { + return nil, err + } + + h.(*constHistogram).exemplars = exemplars + + return h, nil } // MustNewConstHistogram is a version of NewConstHistogram that panics where @@ -698,11 +699,9 @@ func MustNewConstHistogramWithExemplar( exemplars []*dto.Exemplar, labelValues ...string, ) Metric { - m, err := NewConstHistogramWithExemplar(desc, count, sum, buckets, exemplars, labelValues...) - if err != nil { - panic(err) - } - return m + h := MustNewConstHistogram(desc, count, sum, buckets, labelValues...) + h.(*constHistogram).exemplars = exemplars + return h } type buckSort []*dto.Bucket diff --git a/prometheus/histogram_test.go b/prometheus/histogram_test.go index da5771544..b96eff9e1 100644 --- a/prometheus/histogram_test.go +++ b/prometheus/histogram_test.go @@ -424,24 +424,24 @@ func TestHistogramExemplar(t *testing.T) { } expectedExemplars := []*dto.Exemplar{ nil, - &dto.Exemplar{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{Name: proto.String("id"), Value: proto.String("2")}, + {Name: proto.String("id"), Value: proto.String("2")}, }, Value: proto.Float64(1.6), Timestamp: ts, }, nil, - &dto.Exemplar{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{Name: proto.String("id"), Value: proto.String("3")}, + {Name: proto.String("id"), Value: proto.String("3")}, }, Value: proto.Float64(4), Timestamp: ts, }, - &dto.Exemplar{ + { Label: []*dto.LabelPair{ - &dto.LabelPair{Name: proto.String("id"), Value: proto.String("4")}, + {Name: proto.String("id"), Value: proto.String("4")}, }, Value: proto.Float64(4.5), Timestamp: ts,