-
Notifications
You must be signed in to change notification settings - Fork 2
/
avg_time.go
50 lines (39 loc) · 1017 Bytes
/
avg_time.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
41
42
43
44
45
46
47
48
49
50
package metrics
import (
"time"
"github.com/go-redis/redis/v7"
)
type (
AvgTimeMetric struct {
interval int // in minutes
avgCounter *AvgCounterMetric
}
AvgTimeMetricTimer struct {
names []string
avgCounter *AvgCounterMetric
startedAt int64
}
)
func NewAvgTimeMetric(client redis.UniversalClient, interval int, globalPrefix string) *AvgTimeMetric {
avgCounter := NewAvgCounterMetric(client, interval, globalPrefix)
return &AvgTimeMetric{
avgCounter: avgCounter,
interval: interval,
}
}
func (s AvgTimeMetric) Start(names ...string) *AvgTimeMetricTimer {
timer := &AvgTimeMetricTimer{names: names, avgCounter: s.avgCounter}
timer.Start()
return timer
}
func (s *AvgTimeMetricTimer) Start() {
s.startedAt = time.Now().UnixNano()
}
func (s AvgTimeMetricTimer) Stop() {
stoppedAt := time.Now().UnixNano()
dt := stoppedAt - s.startedAt
for _, name := range s.names {
// NOTE: second -> millisecond -> microsecond -> nanosecond
s.avgCounter.Add(name, dt/1000000)
}
}