Skip to content

Commit

Permalink
feat: add timer metrics (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
hwjiangkai authored Sep 15, 2022
1 parent 18f3cbb commit 81995ed
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/timer/timingwheel/timingwheel.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ func (tw *timingWheel) Push(ctx context.Context, e *ce.Event) bool {
"expiration": tm.getExpiration().Format(time.RFC3339Nano),
})

metrics.TimerScheduledEventDelayTime.WithLabelValues(metrics.LabelScheduledEventDelayTime).
Observe(time.Until(tm.getExpiration()).Seconds())

if tm.hasExpired() {
// Already expired
return tw.getDistributionStation().push(ctx, tm) == nil
Expand Down Expand Up @@ -400,7 +403,11 @@ func (tw *timingWheel) runReceivingStation(ctx context.Context) {
defer wg.Done()
waitCtx, cancel := context.WithCancel(ctx)
wait.Until(func() {
startTime := time.Now()
if tw.Push(ctx, e) {
metrics.TimerPushEventTime.WithLabelValues(metrics.LabelTimerPushScheduledEventTime).
Observe(time.Since(startTime).Seconds())
metrics.TimerPushEventTPSCounterVec.WithLabelValues(metrics.LabelTimer).Inc()
cancel()
} else {
log.Warning(ctx, "push event to timingwheel failed, retry until it succeed", map[string]interface{}{
Expand Down Expand Up @@ -499,7 +506,11 @@ func (tw *timingWheel) runDistributionStation(ctx context.Context) {
defer wg.Done()
waitCtx, cancel := context.WithCancel(ctx)
wait.Until(func() {
startTime := time.Now()
if err = tw.deliver(ctx, e); err == nil {
metrics.TimerDeliverEventTime.WithLabelValues(metrics.LabelTimerDeliverScheduledEventTime).
Observe(time.Since(startTime).Seconds())
metrics.TimerDeliverEventTPSCounterVec.WithLabelValues(metrics.LabelTimer).Inc()
cancel()
} else {
log.Warning(ctx, "deliver event failed, retry until it succeed", map[string]interface{}{
Expand Down
8 changes: 8 additions & 0 deletions observability/metrics/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const (
LabelTrigger = "trigger"
LabelResult = "result"
LabelBlock = "block"

LabelTimer = "timer"
)

const (
Expand All @@ -35,3 +37,9 @@ const (
LabelSegmentDeletedBecauseCreateFailed = "segment_create_failed"
LabelSegmentDeletedBecauseDeleted = "segment_deleted"
)

const (
LabelScheduledEventDelayTime = "scheduled_event_delay_time"
LabelTimerPushScheduledEventTime = "push_scheduled_event_time"
LabelTimerDeliverScheduledEventTime = "deliver_scheduled_event_time"
)
5 changes: 5 additions & 0 deletions observability/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func RegisterTimerMetrics() {
prometheus.MustRegister(TimingWheelTickGauge)
prometheus.MustRegister(TimingWheelSizeGauge)
prometheus.MustRegister(TimingWheelLayersGauge)
prometheus.MustRegister(TimerPushEventTPSCounterVec)
prometheus.MustRegister(TimerDeliverEventTPSCounterVec)
prometheus.MustRegister(TimerScheduledEventDelayTime)
prometheus.MustRegister(TimerPushEventTime)
prometheus.MustRegister(TimerDeliverEventTime)
}

func RegisterSegmentServerMetrics() {
Expand Down
35 changes: 35 additions & 0 deletions observability/metrics/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,39 @@ var (
Name: "timingwheel_layers",
Help: "The layers of timingwheel.",
})

TimerPushEventTPSCounterVec = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: moduleOfTimer,
Name: "push_scheduled_event_count",
Help: "Total scheduled events for push",
}, []string{LabelTimer})

TimerDeliverEventTPSCounterVec = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: moduleOfTimer,
Name: "deliver_scheduled_event_count",
Help: "Total scheduled events for deliver",
}, []string{LabelTimer})

TimerScheduledEventDelayTime = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: moduleOfTriggerWorker,
Name: "scheduled_event_delay_time",
Help: "The time of scheduled event delay deliver",
}, []string{LabelScheduledEventDelayTime})

TimerPushEventTime = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: moduleOfTriggerWorker,
Name: "push_scheduled_event_time",
Help: "The time of timer push scheduled event",
}, []string{LabelTimerPushScheduledEventTime})

TimerDeliverEventTime = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: moduleOfTriggerWorker,
Name: "deliver_scheduled_event_time",
Help: "The time of timer deliver scheduled event",
}, []string{LabelTimerDeliverScheduledEventTime})
)

0 comments on commit 81995ed

Please sign in to comment.