From 713e6eb604002f6afff0bcfd8edcd7a15685b1e4 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Mon, 17 Sep 2018 11:50:42 +0200 Subject: [PATCH] Let NewConst... functions detect invalid Desc The error of the invalid Desc is returned in that case. Signed-off-by: beorn7 --- prometheus/histogram.go | 5 ++++- prometheus/summary.go | 5 ++++- prometheus/value.go | 6 +++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/prometheus/histogram.go b/prometheus/histogram.go index a1868d2..7086b76 100644 --- a/prometheus/histogram.go +++ b/prometheus/histogram.go @@ -554,7 +554,7 @@ func (h *constHistogram) Write(out *dto.Metric) error { // bucket. // // NewConstHistogram returns an error if the length of labelValues is not -// consistent with the variable labels in Desc. +// consistent with the variable labels in Desc or if Desc is invalid. func NewConstHistogram( desc *Desc, count uint64, @@ -562,6 +562,9 @@ func NewConstHistogram( buckets map[float64]uint64, labelValues ...string, ) (Metric, error) { + if desc.err != nil { + return nil, desc.err + } if err := validateLabelValues(labelValues, len(desc.variableLabels)); err != nil { return nil, err } diff --git a/prometheus/summary.go b/prometheus/summary.go index 38ee98c..12ff034 100644 --- a/prometheus/summary.go +++ b/prometheus/summary.go @@ -586,7 +586,7 @@ func (s *constSummary) Write(out *dto.Metric) error { // map[float64]float64{0.5: 0.23, 0.99: 0.56} // // NewConstSummary returns an error if the length of labelValues is not -// consistent with the variable labels in Desc. +// consistent with the variable labels in Desc or if Desc is invalid. func NewConstSummary( desc *Desc, count uint64, @@ -594,6 +594,9 @@ func NewConstSummary( quantiles map[float64]float64, labelValues ...string, ) (Metric, error) { + if desc.err != nil { + return nil, desc.err + } if err := validateLabelValues(labelValues, len(desc.variableLabels)); err != nil { return nil, err } diff --git a/prometheus/value.go b/prometheus/value.go index 5e7d4b6..eb248f1 100644 --- a/prometheus/value.go +++ b/prometheus/value.go @@ -77,8 +77,12 @@ func (v *valueFunc) Write(out *dto.Metric) error { // operations. However, when implementing custom Collectors, it is useful as a // throw-away metric that is generated on the fly to send it to Prometheus in // the Collect method. NewConstMetric returns an error if the length of -// labelValues is not consistent with the variable labels in Desc. +// labelValues is not consistent with the variable labels in Desc or if Desc is +// invalid. func NewConstMetric(desc *Desc, valueType ValueType, value float64, labelValues ...string) (Metric, error) { + if desc.err != nil { + return nil, desc.err + } if err := validateLabelValues(labelValues, len(desc.variableLabels)); err != nil { return nil, err }