github.com/safing/portbase@v0.19.5/metrics/metric_gauge.go (about) 1 package metrics 2 3 import ( 4 vm "github.com/VictoriaMetrics/metrics" 5 ) 6 7 // Gauge is a gauge metric. 8 type Gauge struct { 9 *metricBase 10 *vm.Gauge 11 } 12 13 // NewGauge registers a new gauge metric. 14 func NewGauge(id string, labels map[string]string, fn func() float64, opts *Options) (*Gauge, error) { 15 // Ensure that there are options. 16 if opts == nil { 17 opts = &Options{} 18 } 19 20 // Make base. 21 base, err := newMetricBase(id, labels, *opts) 22 if err != nil { 23 return nil, err 24 } 25 26 // Create metric struct. 27 m := &Gauge{ 28 metricBase: base, 29 } 30 31 // Create metric in set 32 m.Gauge = m.set.NewGauge(m.LabeledID(), fn) 33 34 // Register metric. 35 err = register(m) 36 if err != nil { 37 return nil, err 38 } 39 40 return m, nil 41 } 42 43 // CurrentValue returns the current gauge value. 44 func (g *Gauge) CurrentValue() float64 { 45 return g.Get() 46 }