-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.go
86 lines (76 loc) · 2.3 KB
/
timer.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
package epimetheus
import (
"fmt"
"strings"
"time"
"github.com/cactus/go-statsd-client/statsd"
"github.com/prometheus/client_golang/prometheus"
)
// Timer keeps the contents of underlying timer, including labels
type Timer struct {
watcher *prometheus.HistogramVec
client *statsd.Statter
prefix string
labels []string
}
// newTimer creates a prometheus.HistogramVec and register it only if isPrometheusEnabled is true otherwise it keeps
// watcher unregistered to avoid multiple register error in development setups.
func newTimer(namespace, subsystem, name string, labelNames []string, client *statsd.Statter, isPrometheusEnabled bool) *Timer {
opts := prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
}
vec := prometheus.NewHistogramVec(opts, labelNames)
if isPrometheusEnabled {
prometheus.MustRegister(vec)
}
return &Timer{
watcher: vec,
labels: labelNames,
client: client,
prefix: strings.Join([]string{namespace, subsystem, name}, "."),
}
}
// RunWithError gets a function with error and lables and run it with measurment
func (t *Timer) RunWithError(work func() error, labelValues ...string) error {
start := time.Now()
err := work()
end := time.Now()
if indx := t.hasLabel("ok"); indx > 0 {
labelValues[indx] = fmt.Sprint(err == nil)
}
t.register(start, end, labelValues)
return err
}
// RunVoid gets a void function and lables and run it with measurment
func (t *Timer) RunVoid(work func(), labelValues ...string) {
start := time.Now()
work()
end := time.Now()
t.register(start, end, labelValues)
}
// Start begins a RunningTimer and then returns it
func (w *Timer) Start() time.Time {
return time.Now()
}
// Done finalize the current RunningTimer
func (t *Timer) Done(start time.Time, labelValues ...string) {
end := time.Now()
t.register(start, end, labelValues)
}
func (t *Timer) register(start time.Time, end time.Time, labelValues []string) {
duration := end.Sub(start)
t.watcher.WithLabelValues(labelValues...).Observe(duration.Seconds())
metaLabel := t.prefix + "." + strings.Join(labelValues, ".")
(*t.client).Timing(metaLabel, int64(duration/time.Millisecond), 1.0)
(*t.client).Inc(metaLabel, 1, 1.0)
}
func (w *Timer) hasLabel(label string) int {
for i, l := range w.labels {
if l == label {
return i
}
}
return -1
}