forked from segmentio/stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
54 lines (45 loc) · 1.11 KB
/
handler.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
package statstest
import (
"sync"
"sync/atomic"
"time"
"github.com/vertoforce/stats"
)
var _ stats.Handler = (*Handler)(nil)
var _ stats.Flusher = (*Handler)(nil)
// Handler is a stats handler that can record measures for inspection.
type Handler struct {
sync.Mutex
measures []stats.Measure
flush int32
}
// HandleMeasures process a variadic list of stats.Measure.
func (h *Handler) HandleMeasures(time time.Time, measures ...stats.Measure) {
h.Lock()
for _, m := range measures {
h.measures = append(h.measures, m.Clone())
}
h.Unlock()
}
// Measures returns a copy of the handled measures.
func (h *Handler) Measures() []stats.Measure {
h.Lock()
m := make([]stats.Measure, len(h.measures))
copy(m, h.measures)
h.Unlock()
return m
}
// Flush Increments Flush counter.
func (h *Handler) Flush() {
atomic.AddInt32(&h.flush, 1)
}
// FlushCalls returns the number of times `Flush` has been invoked.
func (h *Handler) FlushCalls() int {
return int(atomic.LoadInt32(&h.flush))
}
// Clear removes all measures held by Handler.
func (h *Handler) Clear() {
h.Lock()
h.measures = h.measures[:0]
h.Unlock()
}