From f2992f9e3faaa53d99a17147c5a1d1b796c0c247 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=B6=85?= Date: Mon, 31 Jul 2023 13:28:07 +0800 Subject: [PATCH] timer: refactor timer runtime metrics to make it more flexible to use (#45644) close pingcap/tidb#45610 --- timer/metrics/metrics.go | 16 +++++++--------- timer/runtime/BUILD.bazel | 1 - timer/runtime/runtime.go | 11 +++++++++-- timer/runtime/runtime_test.go | 21 ++++++--------------- 4 files changed, 22 insertions(+), 27 deletions(-) diff --git a/timer/metrics/metrics.go b/timer/metrics/metrics.go index d61ce75ea03b8..7ef91d644bbb8 100644 --- a/timer/metrics/metrics.go +++ b/timer/metrics/metrics.go @@ -20,12 +20,9 @@ import ( "github.com/prometheus/client_golang/prometheus" ) -// Timer metrics var ( + // TimerEventCounter is the counter for timer events TimerEventCounter *prometheus.CounterVec - - TimerFullRefreshCounter prometheus.Counter - TimerPartialRefreshCounter prometheus.Counter ) // InitTimerMetrics initializes timers metrics. @@ -37,13 +34,14 @@ func InitTimerMetrics() { Name: "timer_event_count", Help: "Counter of timer event.", }, []string{"scope", "type"}) - - rtScope := "runtime" - TimerFullRefreshCounter = TimerEventCounter.WithLabelValues(rtScope, "full_refresh_timers") - TimerPartialRefreshCounter = TimerEventCounter.WithLabelValues(rtScope, "partial_refresh_timers") } // TimerHookWorkerCounter creates a counter for a hook's event func TimerHookWorkerCounter(hookClass string, event string) prometheus.Counter { - return TimerEventCounter.WithLabelValues(fmt.Sprintf("hook.%s", hookClass), event) + return TimerScopeCounter(fmt.Sprintf("hook.%s", hookClass), event) +} + +// TimerScopeCounter creates a counter for a scope +func TimerScopeCounter(scope string, event string) prometheus.Counter { + return TimerEventCounter.WithLabelValues(scope, event) } diff --git a/timer/runtime/BUILD.bazel b/timer/runtime/BUILD.bazel index d64f1cdd421cd..aa15ebab0bcfe 100644 --- a/timer/runtime/BUILD.bazel +++ b/timer/runtime/BUILD.bazel @@ -37,7 +37,6 @@ go_test( deps = [ "//testkit/testsetup", "//timer/api", - "//timer/metrics", "//util/mock", "@com_github_google_uuid//:uuid", "@com_github_pingcap_errors//:errors", diff --git a/timer/runtime/runtime.go b/timer/runtime/runtime.go index 7bd3e3a6a6fa6..f8198a46b1bb3 100644 --- a/timer/runtime/runtime.go +++ b/timer/runtime/runtime.go @@ -25,6 +25,7 @@ import ( "github.com/pingcap/tidb/timer/api" "github.com/pingcap/tidb/timer/metrics" "github.com/pingcap/tidb/util/logutil" + "github.com/prometheus/client_golang/prometheus" "go.uber.org/zap" "golang.org/x/exp/maps" ) @@ -60,6 +61,9 @@ func NewTimerRuntimeBuilder(groupID string, store *api.TimerStore) *TimerRuntime workerRespCh: make(chan *triggerEventResponse, workerRespChanCap), workers: make(map[string]*hookWorker), nowFunc: time.Now, + // metrics + fullRefreshTimerCounter: metrics.TimerScopeCounter(fmt.Sprintf("runtime.%s", groupID), "full_refresh_timers"), + partialRefreshTimerCounter: metrics.TimerScopeCounter(fmt.Sprintf("runtime.%s", groupID), "partial_refresh_timers"), }, } } @@ -101,6 +105,9 @@ type TimerGroupRuntime struct { workers map[string]*hookWorker // nowFunc is only used by test nowFunc func() time.Time + // metrics + fullRefreshTimerCounter prometheus.Counter + partialRefreshTimerCounter prometheus.Counter } // Start starts the TimerGroupRuntime @@ -211,7 +218,7 @@ func (rt *TimerGroupRuntime) loop() { } func (rt *TimerGroupRuntime) fullRefreshTimers() { - metrics.TimerFullRefreshCounter.Inc() + rt.fullRefreshTimerCounter.Inc() timers, err := rt.store.List(rt.ctx, rt.cond) if err != nil { rt.logger.Error("error occurs when fullRefreshTimers", zap.Error(err)) @@ -356,7 +363,7 @@ func (rt *TimerGroupRuntime) partialRefreshTimers(timerIDs map[string]struct{}) return false } - metrics.TimerPartialRefreshCounter.Inc() + rt.partialRefreshTimerCounter.Inc() cond := rt.buildTimerIDsCond(timerIDs) timers, err := rt.store.List(rt.ctx, cond) if err != nil { diff --git a/timer/runtime/runtime_test.go b/timer/runtime/runtime_test.go index a5146f08b792b..77b12095acff9 100644 --- a/timer/runtime/runtime_test.go +++ b/timer/runtime/runtime_test.go @@ -24,7 +24,6 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/tidb/timer/api" - "github.com/pingcap/tidb/timer/metrics" mockutil "github.com/pingcap/tidb/util/mock" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" @@ -67,6 +66,8 @@ func TestRuntimeStartStop(t *testing.T) { runtime := NewTimerRuntimeBuilder("g1", store). RegisterHookFactory("hook1", hookFactory). Build() + require.NotNil(t, runtime.fullRefreshTimerCounter) + require.NotNil(t, runtime.partialRefreshTimerCounter) runtime.Start() require.True(t, runtime.Running()) @@ -390,16 +391,11 @@ func TestNextTryTriggerDuration(t *testing.T) { } func TestFullRefreshTimers(t *testing.T) { - origFullRefreshCounter := metrics.TimerFullRefreshCounter - defer func() { - metrics.TimerFullRefreshCounter = origFullRefreshCounter - }() - fullRefreshCounter := &mockutil.MetricsCounter{} - metrics.TimerFullRefreshCounter = fullRefreshCounter - mockCore, mockStore := newMockStore() runtime := NewTimerRuntimeBuilder("g1", mockStore).Build() + require.NotNil(t, runtime.fullRefreshTimerCounter) + runtime.fullRefreshTimerCounter = fullRefreshCounter runtime.cond = &api.TimerCond{Namespace: api.NewOptionalVal("n1")} runtime.initCtx() @@ -459,18 +455,13 @@ func TestFullRefreshTimers(t *testing.T) { } func TestBatchHandlerWatchResponses(t *testing.T) { - origPartialRefreshCounter := metrics.TimerPartialRefreshCounter - defer func() { - metrics.TimerPartialRefreshCounter = origPartialRefreshCounter - }() - partialRefreshCounter := &mockutil.MetricsCounter{} - metrics.TimerPartialRefreshCounter = partialRefreshCounter - mockCore, mockStore := newMockStore() runtime := NewTimerRuntimeBuilder("g1", mockStore).Build() + require.NotNil(t, runtime.partialRefreshTimerCounter) runtime.cond = &api.TimerCond{Namespace: api.NewOptionalVal("n1")} runtime.initCtx() + runtime.partialRefreshTimerCounter = partialRefreshCounter timers := make([]*api.TimerRecord, 7) for i := 0; i < len(timers); i++ {