From 1e2a12b3d6dfb0b7d20fedd4c4d38a63ee22c846 Mon Sep 17 00:00:00 2001 From: #Suyghur Date: Thu, 26 Oct 2023 07:13:42 -0500 Subject: [PATCH] feat(metric): added `Dec()` and `Sub()` in `GaugeVec` interface (#3666) --- core/metric/gauge.go | 24 +++++++++++++++++++++--- core/metric/gauge_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/core/metric/gauge.go b/core/metric/gauge.go index 52ff841d..4dfd7243 100644 --- a/core/metric/gauge.go +++ b/core/metric/gauge.go @@ -16,8 +16,12 @@ type ( 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 } @@ -50,6 +54,14 @@ 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 @@ -58,6 +70,13 @@ func (gv *promGaugeVec) Inc(labels ...string) { 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 @@ -66,12 +85,11 @@ func (gv *promGaugeVec) Add(v float64, labels ...string) { gv.gauge.WithLabelValues(labels...).Add(v) } -func (gv *promGaugeVec) Set(v float64, labels ...string) { +func (gv *promGaugeVec) Sub(v float64, labels ...string) { if !prometheus.Enabled() { return } - - gv.gauge.WithLabelValues(labels...).Set(v) + gv.gauge.WithLabelValues(labels...).Sub(v) } func (gv *promGaugeVec) close() bool { diff --git a/core/metric/gauge_test.go b/core/metric/gauge_test.go index fb070260..1ebb2a3f 100644 --- a/core/metric/gauge_test.go +++ b/core/metric/gauge_test.go @@ -40,6 +40,23 @@ func TestGaugeInc(t *testing.T) { assert.Equal(t, float64(2), r) } +func TestGaugeDec(t *testing.T) { + startAgent() + gaugeVec := NewGaugeVec(&GaugeVecOpts{ + Namespace: "rpc_client", + Subsystem: "requests", + Name: "duration_ms", + Help: "rpc server requests duration(ms).", + Labels: []string{"path"}, + }) + defer gaugeVec.close() + gv, _ := gaugeVec.(*promGaugeVec) + gv.Dec("/users") + gv.Dec("/users") + r := testutil.ToFloat64(gv.gauge) + assert.Equal(t, float64(-2), r) +} + func TestGaugeAdd(t *testing.T) { startAgent() gaugeVec := NewGaugeVec(&GaugeVecOpts{ @@ -57,6 +74,23 @@ func TestGaugeAdd(t *testing.T) { assert.Equal(t, float64(20), r) } +func TestGaugeSub(t *testing.T) { + startAgent() + gaugeVec := NewGaugeVec(&GaugeVecOpts{ + Namespace: "rpc_client", + Subsystem: "request", + Name: "duration_ms", + Help: "rpc server requests duration(ms).", + Labels: []string{"path"}, + }) + defer gaugeVec.close() + gv, _ := gaugeVec.(*promGaugeVec) + gv.Sub(-100, "/classroom") + gv.Sub(30, "/classroom") + r := testutil.ToFloat64(gv.gauge) + assert.Equal(t, float64(70), r) +} + func TestGaugeSet(t *testing.T) { startAgent() gaugeVec := NewGaugeVec(&GaugeVecOpts{