123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package metric
- import (
- "git.i2edu.net/i2/go-zero/core/proc"
- prom "github.com/prometheus/client_golang/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)
- // Add adds v to labels.
- Add(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) Inc(labels ...string) {
- gv.gauge.WithLabelValues(labels...).Inc()
- }
- func (gv *promGaugeVec) Add(v float64, labels ...string) {
- gv.gauge.WithLabelValues(labels...).Add(v)
- }
- func (gv *promGaugeVec) Set(v float64, labels ...string) {
- gv.gauge.WithLabelValues(labels...).Set(v)
- }
- func (gv *promGaugeVec) close() bool {
- return prom.Unregister(gv.gauge)
- }
|