浏览代码

chore: simplify prometheus check (#3672)

Kevin Wan 1 年之前
父节点
当前提交
199e86050e
共有 5 个文件被更改,包括 49 次插入60 次删除
  1. 8 13
      core/metric/counter.go
  2. 25 35
      core/metric/gauge.go
  3. 3 6
      core/metric/histogram.go
  4. 10 0
      core/metric/metric.go
  5. 3 6
      core/metric/summary.go

+ 8 - 13
core/metric/counter.go

@@ -3,7 +3,6 @@ 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 (
@@ -47,20 +46,16 @@ func NewCounterVec(cfg *CounterVecOpts) CounterVec {
 	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) {
-	if !prometheus.Enabled() {
-		return
-	}
+	update(func() {
+		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 {

+ 25 - 35
core/metric/gauge.go

@@ -3,7 +3,6 @@ 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 (
@@ -36,13 +35,12 @@ func NewGaugeVec(cfg *GaugeVecOpts) GaugeVec {
 		return nil
 	}
 
-	vec := prom.NewGaugeVec(
-		prom.GaugeOpts{
-			Namespace: cfg.Namespace,
-			Subsystem: cfg.Subsystem,
-			Name:      cfg.Name,
-			Help:      cfg.Help,
-		}, cfg.Labels)
+	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,
@@ -54,42 +52,34 @@ func NewGaugeVec(cfg *GaugeVecOpts) GaugeVec {
 	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) Add(v float64, labels ...string) {
+	update(func() {
+		gv.gauge.WithLabelValues(labels...).Add(v)
+	})
 }
 
 func (gv *promGaugeVec) Dec(labels ...string) {
-	if !prometheus.Enabled() {
-		return
-	}
-	gv.gauge.WithLabelValues(labels...).Dec()
+	update(func() {
+		gv.gauge.WithLabelValues(labels...).Dec()
+	})
 }
 
-func (gv *promGaugeVec) Add(v float64, labels ...string) {
-	if !prometheus.Enabled() {
-		return
-	}
+func (gv *promGaugeVec) Inc(labels ...string) {
+	update(func() {
+		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) {
-	if !prometheus.Enabled() {
-		return
-	}
-	gv.gauge.WithLabelValues(labels...).Sub(v)
+	update(func() {
+		gv.gauge.WithLabelValues(labels...).Sub(v)
+	})
 }
 
 func (gv *promGaugeVec) close() bool {

+ 3 - 6
core/metric/histogram.go

@@ -3,7 +3,6 @@ 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 (
@@ -54,11 +53,9 @@ func NewHistogramVec(cfg *HistogramVecOpts) HistogramVec {
 }
 
 func (hv *promHistogramVec) Observe(v int64, labels ...string) {
-	if !prometheus.Enabled() {
-		return
-	}
-
-	hv.histogram.WithLabelValues(labels...).Observe(float64(v))
+	update(func() {
+		hv.histogram.WithLabelValues(labels...).Observe(float64(v))
+	})
 }
 
 func (hv *promHistogramVec) close() bool {

+ 10 - 0
core/metric/metric.go

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

+ 3 - 6
core/metric/summary.go

@@ -3,7 +3,6 @@ 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 (
@@ -53,11 +52,9 @@ func NewSummaryVec(cfg *SummaryVecOpts) SummaryVec {
 }
 
 func (sv *promSummaryVec) Observe(v float64, labels ...string) {
-	if !prometheus.Enabled() {
-		return
-	}
-
-	sv.summary.WithLabelValues(labels...).Observe(v)
+	update(func() {
+		sv.summary.WithLabelValues(labels...).Observe(v)
+	})
 }
 
 func (sv *promSummaryVec) close() bool {