Skip to content

Commit

Permalink
timer: refactor timer runtime metrics to make it more flexible to use
Browse files Browse the repository at this point in the history
  • Loading branch information
lcwangchao committed Jul 28, 2023
1 parent ac0c90e commit 020edbc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
16 changes: 7 additions & 9 deletions timer/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
1 change: 0 additions & 1 deletion timer/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 9 additions & 2 deletions timer/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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"),
},
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 6 additions & 15 deletions timer/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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++ {
Expand Down

0 comments on commit 020edbc

Please sign in to comment.