You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/metric/gauge.go

98 lines
1.8 KiB
Go

package metric
import (
prom "github.com/prometheus/client_golang/prometheus"
"github.com/zeromicro/go-zero/core/proc"
"github.com/zeromicro/go-zero/core/prometheus"
)
type (
// GaugeVecOpts is an alias of VectorOpts.
GaugeVecOpts VectorOpts
// GaugeVec represents a gauge vector.
GaugeVec interface {
// Set sets v to labels.
Set(v float64, labels ...string)
// Inc increments labels.
Inc(labels ...string)
// Dec decrements labels.
Dec(labels ...string)
// Add adds v to labels.
Add(v float64, labels ...string)
// Sub subtracts v to labels.
Sub(v float64, labels ...string)
close() bool
}
promGaugeVec struct {
gauge *prom.GaugeVec
}
)
// NewGaugeVec returns a GaugeVec.
func NewGaugeVec(cfg *GaugeVecOpts) GaugeVec {
if cfg == nil {
return nil
}
vec := prom.NewGaugeVec(
prom.GaugeOpts{
Namespace: cfg.Namespace,
Subsystem: cfg.Subsystem,
Name: cfg.Name,
Help: cfg.Help,
}, cfg.Labels)
prom.MustRegister(vec)
gv := &promGaugeVec{
gauge: vec,
}
proc.AddShutdownListener(func() {
gv.close()
})
return gv
}
func (gv *promGaugeVec) Set(v float64, labels ...string) {
if !prometheus.Enabled() {
return
}
gv.gauge.WithLabelValues(labels...).Set(v)
}
func (gv *promGaugeVec) Inc(labels ...string) {
if !prometheus.Enabled() {
return
}
gv.gauge.WithLabelValues(labels...).Inc()
}
func (gv *promGaugeVec) Dec(labels ...string) {
if !prometheus.Enabled() {
return
}
gv.gauge.WithLabelValues(labels...).Dec()
}
func (gv *promGaugeVec) Add(v float64, labels ...string) {
if !prometheus.Enabled() {
return
}
gv.gauge.WithLabelValues(labels...).Add(v)
}
func (gv *promGaugeVec) Sub(v float64, labels ...string) {
if !prometheus.Enabled() {
return
}
gv.gauge.WithLabelValues(labels...).Sub(v)
}
func (gv *promGaugeVec) close() bool {
return prom.Unregister(gv.gauge)
}