forked from Conflux-Chain/confura
/
timer_updater.go
40 lines (33 loc) · 986 Bytes
/
timer_updater.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package metrics
import (
"time"
"github.com/ethereum/go-ethereum/metrics"
)
// TimerUpdater is used to update timer metric with native defer syntax.
type TimerUpdater struct {
underlying metrics.Timer
start time.Time
}
// NewTimerUpdater creates an instance to update timer metric.
func NewTimerUpdater(timer metrics.Timer) TimerUpdater {
return TimerUpdater{
underlying: timer,
start: time.Now(),
}
}
// NewTimerUpdaterByName creates an instance to update timer metric
// of specified name.
func NewTimerUpdaterByName(name string) TimerUpdater {
return TimerUpdater{
underlying: metrics.GetOrRegisterTimer(name, nil),
start: time.Now(),
}
}
// Update updates the underlying timer metric.
func (updater *TimerUpdater) Update() {
updater.underlying.UpdateSince(updater.start)
}
// Update updates the underlying timer metric with duration.
func (updater *TimerUpdater) UpdateDuration(duration time.Duration) {
updater.underlying.Update(duration)
}