-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_wrapper.go
92 lines (83 loc) · 2.78 KB
/
metrics_wrapper.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package store
import "time"
//go:generate counterfeiter -o fakes/metrics_sender.go --fake-name MetricsSender . metricsSender
type metricsSender interface {
IncrementCounter(string)
SendDuration(string, time.Duration)
}
type MetricsWrapper struct {
Store Store
MetricsSender metricsSender
}
func (mw *MetricsWrapper) Create(policies []Policy) error {
startTime := time.Now()
err := mw.Store.Create(policies)
createTimeDuration := time.Now().Sub(startTime)
if err != nil {
mw.MetricsSender.IncrementCounter("StoreCreateError")
mw.MetricsSender.SendDuration("StoreCreateErrorTime", createTimeDuration)
} else {
mw.MetricsSender.SendDuration("StoreCreateSuccessTime", createTimeDuration)
}
return err
}
func (mw *MetricsWrapper) All() ([]Policy, error) {
startTime := time.Now()
policies, err := mw.Store.All()
allTimeDuration := time.Now().Sub(startTime)
if err != nil {
mw.MetricsSender.IncrementCounter("StoreAllError")
mw.MetricsSender.SendDuration("StoreAllErrorTime", allTimeDuration)
} else {
mw.MetricsSender.SendDuration("StoreAllSuccessTime", allTimeDuration)
}
return policies, err
}
func (mw *MetricsWrapper) Delete(policies []Policy) error {
startTime := time.Now()
err := mw.Store.Delete(policies)
deleteTimeDuration := time.Now().Sub(startTime)
if err != nil {
mw.MetricsSender.IncrementCounter("StoreDeleteError")
mw.MetricsSender.SendDuration("StoreDeleteErrorTime", deleteTimeDuration)
} else {
mw.MetricsSender.SendDuration("StoreDeleteSuccessTime", deleteTimeDuration)
}
return err
}
func (mw *MetricsWrapper) Tags() ([]Tag, error) {
startTime := time.Now()
tags, err := mw.Store.Tags()
tagsTimeDuration := time.Now().Sub(startTime)
if err != nil {
mw.MetricsSender.IncrementCounter("StoreTagsError")
mw.MetricsSender.SendDuration("StoreTagsErrorTime", tagsTimeDuration)
} else {
mw.MetricsSender.SendDuration("StoreTagsSuccessTime", tagsTimeDuration)
}
return tags, err
}
func (mw *MetricsWrapper) ByGuids(srcGuids, dstGuids []string) ([]Policy, error) {
startTime := time.Now()
policies, err := mw.Store.ByGuids(srcGuids, dstGuids)
byGuidsTimeDuration := time.Now().Sub(startTime)
if err != nil {
mw.MetricsSender.IncrementCounter("StoreByGuidsError")
mw.MetricsSender.SendDuration("StoreByGuidsErrorTime", byGuidsTimeDuration)
} else {
mw.MetricsSender.SendDuration("StoreByGuidsSuccessTime", byGuidsTimeDuration)
}
return policies, err
}
func (mw *MetricsWrapper) CheckDatabase() error {
startTime := time.Now()
err := mw.Store.CheckDatabase()
duration := time.Now().Sub(startTime)
if err != nil {
mw.MetricsSender.IncrementCounter("StoreCheckDatabaseError")
mw.MetricsSender.SendDuration("StoreCheckDatabaseErrorTime", duration)
} else {
mw.MetricsSender.SendDuration("StoreCheckDatabaseSuccessTime", duration)
}
return err
}