chore: simplify prometheus check (#3672)

master
Kevin Wan 1 year ago committed by GitHub
parent 1e2a12b3d6
commit 199e86050e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,7 +3,6 @@ package metric
import ( import (
prom "github.com/prometheus/client_golang/prometheus" prom "github.com/prometheus/client_golang/prometheus"
"github.com/zeromicro/go-zero/core/proc" "github.com/zeromicro/go-zero/core/proc"
"github.com/zeromicro/go-zero/core/prometheus"
) )
type ( type (
@ -47,20 +46,16 @@ func NewCounterVec(cfg *CounterVecOpts) CounterVec {
return cv return cv
} }
func (cv *promCounterVec) Inc(labels ...string) {
if !prometheus.Enabled() {
return
}
cv.counter.WithLabelValues(labels...).Inc()
}
func (cv *promCounterVec) Add(v float64, labels ...string) { func (cv *promCounterVec) Add(v float64, labels ...string) {
if !prometheus.Enabled() { update(func() {
return cv.counter.WithLabelValues(labels...).Add(v)
} })
}
cv.counter.WithLabelValues(labels...).Add(v) func (cv *promCounterVec) Inc(labels ...string) {
update(func() {
cv.counter.WithLabelValues(labels...).Inc()
})
} }
func (cv *promCounterVec) close() bool { func (cv *promCounterVec) close() bool {

@ -3,7 +3,6 @@ package metric
import ( import (
prom "github.com/prometheus/client_golang/prometheus" prom "github.com/prometheus/client_golang/prometheus"
"github.com/zeromicro/go-zero/core/proc" "github.com/zeromicro/go-zero/core/proc"
"github.com/zeromicro/go-zero/core/prometheus"
) )
type ( type (
@ -36,13 +35,12 @@ func NewGaugeVec(cfg *GaugeVecOpts) GaugeVec {
return nil return nil
} }
vec := prom.NewGaugeVec( vec := prom.NewGaugeVec(prom.GaugeOpts{
prom.GaugeOpts{ Namespace: cfg.Namespace,
Namespace: cfg.Namespace, Subsystem: cfg.Subsystem,
Subsystem: cfg.Subsystem, Name: cfg.Name,
Name: cfg.Name, Help: cfg.Help,
Help: cfg.Help, }, cfg.Labels)
}, cfg.Labels)
prom.MustRegister(vec) prom.MustRegister(vec)
gv := &promGaugeVec{ gv := &promGaugeVec{
gauge: vec, gauge: vec,
@ -54,42 +52,34 @@ func NewGaugeVec(cfg *GaugeVecOpts) GaugeVec {
return gv return gv
} }
func (gv *promGaugeVec) Set(v float64, labels ...string) { func (gv *promGaugeVec) Add(v float64, labels ...string) {
if !prometheus.Enabled() { update(func() {
return gv.gauge.WithLabelValues(labels...).Add(v)
} })
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) { func (gv *promGaugeVec) Dec(labels ...string) {
if !prometheus.Enabled() { update(func() {
return gv.gauge.WithLabelValues(labels...).Dec()
} })
gv.gauge.WithLabelValues(labels...).Dec()
} }
func (gv *promGaugeVec) Add(v float64, labels ...string) { func (gv *promGaugeVec) Inc(labels ...string) {
if !prometheus.Enabled() { update(func() {
return gv.gauge.WithLabelValues(labels...).Inc()
} })
}
gv.gauge.WithLabelValues(labels...).Add(v) func (gv *promGaugeVec) Set(v float64, labels ...string) {
update(func() {
gv.gauge.WithLabelValues(labels...).Set(v)
})
} }
func (gv *promGaugeVec) Sub(v float64, labels ...string) { func (gv *promGaugeVec) Sub(v float64, labels ...string) {
if !prometheus.Enabled() { update(func() {
return gv.gauge.WithLabelValues(labels...).Sub(v)
} })
gv.gauge.WithLabelValues(labels...).Sub(v)
} }
func (gv *promGaugeVec) close() bool { func (gv *promGaugeVec) close() bool {

@ -3,7 +3,6 @@ package metric
import ( import (
prom "github.com/prometheus/client_golang/prometheus" prom "github.com/prometheus/client_golang/prometheus"
"github.com/zeromicro/go-zero/core/proc" "github.com/zeromicro/go-zero/core/proc"
"github.com/zeromicro/go-zero/core/prometheus"
) )
type ( type (
@ -54,11 +53,9 @@ func NewHistogramVec(cfg *HistogramVecOpts) HistogramVec {
} }
func (hv *promHistogramVec) Observe(v int64, labels ...string) { func (hv *promHistogramVec) Observe(v int64, labels ...string) {
if !prometheus.Enabled() { update(func() {
return hv.histogram.WithLabelValues(labels...).Observe(float64(v))
} })
hv.histogram.WithLabelValues(labels...).Observe(float64(v))
} }
func (hv *promHistogramVec) close() bool { func (hv *promHistogramVec) close() bool {

@ -1,5 +1,7 @@
package metric package metric
import "github.com/zeromicro/go-zero/core/prometheus"
// A VectorOpts is a general configuration. // A VectorOpts is a general configuration.
type VectorOpts struct { type VectorOpts struct {
Namespace string Namespace string
@ -8,3 +10,11 @@ type VectorOpts struct {
Help string Help string
Labels []string Labels []string
} }
func update(fn func()) {
if !prometheus.Enabled() {
return
}
fn()
}

@ -3,7 +3,6 @@ package metric
import ( import (
prom "github.com/prometheus/client_golang/prometheus" prom "github.com/prometheus/client_golang/prometheus"
"github.com/zeromicro/go-zero/core/proc" "github.com/zeromicro/go-zero/core/proc"
"github.com/zeromicro/go-zero/core/prometheus"
) )
type ( type (
@ -53,11 +52,9 @@ func NewSummaryVec(cfg *SummaryVecOpts) SummaryVec {
} }
func (sv *promSummaryVec) Observe(v float64, labels ...string) { func (sv *promSummaryVec) Observe(v float64, labels ...string) {
if !prometheus.Enabled() { update(func() {
return sv.summary.WithLabelValues(labels...).Observe(v)
} })
sv.summary.WithLabelValues(labels...).Observe(v)
} }
func (sv *promSummaryVec) close() bool { func (sv *promSummaryVec) close() bool {

Loading…
Cancel
Save