-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.go
More file actions
150 lines (115 loc) · 3.34 KB
/
timer.go
File metadata and controls
150 lines (115 loc) · 3.34 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package stats
import (
"sync"
"time"
)
// TimerVector is a multi-dimensional timer that creates timer instances
// with specific label values.
type TimerVector interface {
// WithLabels returns a Timer with the specified label values.
// The number of values must match the number of labels defined for this vector.
WithLabels(...string) Timer
}
type timerVector struct {
entityVector
scope Scope
name string
opts timerOptions
}
// NewTimerVector creates a new timer vector with the given scope, name, labels, and options.
func NewTimerVector(scope Scope, name string, labels []string, opts ...TimerOption) TimerVector {
var tv = timerVector{
entityVector: entityVector{
marshaler: newDefaultMarshaler(),
labels: labels,
},
scope: scope,
name: name,
opts: defaultTimerOptions,
}
for _, opt := range opts {
opt(&tv.opts)
}
tv.newFunc = tv.newTimer
return &tv
}
func (tv *timerVector) newTimer(vs map[string]string) interface{} {
return newTimer(tv.scope.Scope("", vs), tv.name, tv.opts)
}
func (tv *timerVector) WithLabels(ls ...string) Timer {
return tv.entity(ls).(*timer)
}
// StopWatch represents an active timing measurement.
// Call Stop to record the elapsed duration.
type StopWatch interface {
// Stop records the elapsed time since Start was called.
Stop()
}
// Timer is a convenience wrapper around Histogram for measuring durations.
// It automatically records durations in seconds by default.
type Timer interface {
// Start begins a new timing measurement and returns a StopWatch.
Start() StopWatch
}
type timer struct {
Histogram
p sync.Pool
}
func (t *timer) Start() StopWatch {
var sw = t.p.Get().(*stopWatch)
sw.t0 = time.Now()
return sw
}
type stopWatch struct {
t0 time.Time
timer *timer
}
// TimerOption configures a Timer with custom settings.
type TimerOption func(*timerOptions)
type timerOptions struct {
hOpts []HistogramOption
suffix string
}
// WithHistogramOptions configures the underlying histogram with custom options.
func WithHistogramOptions(hOpts ...HistogramOption) TimerOption {
return func(opts *timerOptions) {
opts.hOpts = hOpts
}
}
// WithTimerSuffix configures a custom suffix for the timer's metric name.
// Default suffix is "_seconds".
func WithTimerSuffix(s string) TimerOption {
return func(opts *timerOptions) {
opts.suffix = s
}
}
var defaultTimerOptions = timerOptions{
suffix: "_seconds",
}
func (sw *stopWatch) Stop() {
sw.timer.Record(time.Since(sw.t0).Seconds())
sw.timer.p.Put(sw)
}
// NewTimer creates a new timer with the given scope, name, and options.
// The timer automatically appends "_seconds" suffix to the name (configurable via WithTimerSuffix).
func NewTimer(scope Scope, name string, tOpts ...TimerOption) Timer {
var opts = defaultTimerOptions
for _, opt := range tOpts {
opt(&opts)
}
return newTimer(scope, name, opts)
}
func newTimer(scope Scope, name string, opts timerOptions) *timer {
var t = timer{Histogram: scope.Histogram(name+opts.suffix, opts.hOpts...)}
t.p = sync.Pool{New: func() interface{} { return &stopWatch{timer: &t} }}
return &t
}
type noopTimer struct{}
func (nt noopTimer) Start() StopWatch {
return noopStopWatch{}
}
type noopStopWatch struct{}
func (nsw noopStopWatch) Stop() {}
// NoopTimer is a no-operation timer that does nothing.
// Useful for disabling timing measurements.
var NoopTimer Timer = noopTimer{}