From 90b1123f267a7d1b2ed7f485932904a8026c2f3c Mon Sep 17 00:00:00 2001 From: Peter Hunt Date: Fri, 4 Nov 2022 14:25:55 -0400 Subject: [PATCH] component-base/metrics: Add NewConstMetric function so that a caller can use the metrics.Metric structure but still handle errors Signed-off-by: Peter Hunt Kubernetes-commit: 435606b109d7bda5562d2a584985cae5148a2340 --- metrics/value.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/metrics/value.go b/metrics/value.go index bf8a6b8f..b525bb60 100644 --- a/metrics/value.go +++ b/metrics/value.go @@ -47,6 +47,16 @@ func NewLazyConstMetric(desc *Desc, valueType ValueType, value float64, labelVal return prometheus.MustNewConstMetric(desc.toPrometheusDesc(), valueType.toPromValueType(), value, labelValues...) } +// NewConstMetric is a helper of NewConstMetric. +// +// Note: If the metrics described by the desc is hidden, the metrics will not be created. +func NewConstMetric(desc *Desc, valueType ValueType, value float64, labelValues ...string) (Metric, error) { + if desc.IsHidden() { + return nil, nil + } + return prometheus.NewConstMetric(desc.toPrometheusDesc(), valueType.toPromValueType(), value, labelValues...) +} + // NewLazyMetricWithTimestamp is a helper of NewMetricWithTimestamp. // // Warning: the Metric 'm' must be the one created by NewLazyConstMetric(),