1 /* 2 Copyright 2019 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package metrics 18 19 import ( 20 "time" 21 22 "github.com/prometheus/client_golang/prometheus" 23 ) 24 25 // ValueType is an enumeration of metric types that represent a simple value. 26 type ValueType int 27 28 // Possible values for the ValueType enum. 29 const ( 30 _ ValueType = iota 31 CounterValue 32 GaugeValue 33 UntypedValue 34 ) 35 36 func (vt *ValueType) toPromValueType() prometheus.ValueType { 37 return prometheus.ValueType(*vt) 38 } 39 40 // NewLazyConstMetric is a helper of MustNewConstMetric. 41 // 42 // Note: If the metrics described by the desc is hidden, the metrics will not be created. 43 func NewLazyConstMetric(desc *Desc, valueType ValueType, value float64, labelValues ...string) Metric { 44 if desc.IsHidden() { 45 return nil 46 } 47 return prometheus.MustNewConstMetric(desc.toPrometheusDesc(), valueType.toPromValueType(), value, labelValues...) 48 } 49 50 // NewConstMetric is a helper of NewConstMetric. 51 // 52 // Note: If the metrics described by the desc is hidden, the metrics will not be created. 53 func NewConstMetric(desc *Desc, valueType ValueType, value float64, labelValues ...string) (Metric, error) { 54 if desc.IsHidden() { 55 return nil, nil 56 } 57 return prometheus.NewConstMetric(desc.toPrometheusDesc(), valueType.toPromValueType(), value, labelValues...) 58 } 59 60 // NewLazyMetricWithTimestamp is a helper of NewMetricWithTimestamp. 61 // 62 // Warning: the Metric 'm' must be the one created by NewLazyConstMetric(), 63 // otherwise, no stability guarantees would be offered. 64 func NewLazyMetricWithTimestamp(t time.Time, m Metric) Metric { 65 if m == nil { 66 return nil 67 } 68 69 return prometheus.NewMetricWithTimestamp(t, m) 70 } 71