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/counter.go

60 lines
1.2 KiB
Go

4 years ago
package metric
import (
prom "github.com/prometheus/client_golang/prometheus"
"github.com/tal-tech/go-zero/core/proc"
4 years ago
)
type (
// A CounterVecOpts is an alias of VectorOpts.
4 years ago
CounterVecOpts VectorOpts
// CounterVec interface represents a counter vector.
4 years ago
CounterVec interface {
// Inc increments labels.
4 years ago
Inc(lables ...string)
// Add adds labels with v.
4 years ago
Add(v float64, labels ...string)
close() bool
}
promCounterVec struct {
counter *prom.CounterVec
}
)
// NewCounterVec returns a CounterVec.
4 years ago
func NewCounterVec(cfg *CounterVecOpts) CounterVec {
if cfg == nil {
return nil
}
vec := prom.NewCounterVec(prom.CounterOpts{
Namespace: cfg.Namespace,
Subsystem: cfg.Subsystem,
Name: cfg.Name,
Help: cfg.Help,
}, cfg.Labels)
prom.MustRegister(vec)
cv := &promCounterVec{
counter: vec,
}
proc.AddShutdownListener(func() {
cv.close()
})
return cv
}
func (cv *promCounterVec) Inc(labels ...string) {
cv.counter.WithLabelValues(labels...).Inc()
}
func (cv *promCounterVec) Add(v float64, lables ...string) {
cv.counter.WithLabelValues(lables...).Add(v)
}
func (cv *promCounterVec) close() bool {
return prom.Unregister(cv.counter)
}