/
stats_reporter.go
93 lines (77 loc) 路 2.31 KB
/
stats_reporter.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
93
package delegatedrouting
import (
"sync/atomic"
"time"
)
type statsReporter struct {
s *stats
totalCidsFunc func() int
totalChunksFunc func() int
currentChunkSizeFunc func() int
statsTicker chan bool
}
type stats struct {
putAdsSent int64
removeAdsSent int64
cidsProcessed int64
existingCidsProcessed int64
cidsExpired int64
delegatedRoutingCallsReceived int64
delegatedRoutingCallsProcessed int64
chunkCacheMisses int64
chunksNotFound int64
}
func newStatsReporter(totalCidsFunc func() int, totalChunksFunc func() int, currentChunkSizeFunc func() int) *statsReporter {
return &statsReporter{
s: &stats{},
totalCidsFunc: totalCidsFunc,
totalChunksFunc: totalChunksFunc,
currentChunkSizeFunc: currentChunkSizeFunc,
}
}
func (reporter *statsReporter) incPutAdsSent() {
reporter.s.putAdsSent++
}
func (reporter *statsReporter) incRemoveAdsSent() {
reporter.s.removeAdsSent++
}
func (reporter *statsReporter) incCidsProcessed() {
reporter.s.cidsProcessed++
}
func (reporter *statsReporter) incExistingCidsProcessed() {
reporter.s.existingCidsProcessed++
}
func (reporter *statsReporter) incCidsExpired() {
reporter.s.cidsExpired++
}
func (reporter *statsReporter) incDelegatedRoutingCallsReceived() {
// needs to be threadsafe as it gets called from the webserver handler
atomic.AddInt64(&reporter.s.delegatedRoutingCallsReceived, 1)
}
func (reporter *statsReporter) incDelegatedRoutingCallsProcessed() {
reporter.s.delegatedRoutingCallsProcessed++
}
func (reporter *statsReporter) incChunkCacheMisses() {
reporter.s.chunkCacheMisses++
}
func (reporter *statsReporter) incChunksNotFound() {
reporter.s.chunksNotFound++
}
func (reporter *statsReporter) start() {
reporter.statsTicker = make(chan bool)
ticker := time.NewTicker(statsPrintFrequency)
go func() {
for {
select {
case <-reporter.statsTicker:
ticker.Stop()
return
case <-ticker.C:
log.Infof("stats: %+v, totalCids: %d, totalChunks: %d, currentChunkSize: %d", reporter.s, reporter.totalCidsFunc(), reporter.totalChunksFunc(), reporter.currentChunkSizeFunc())
}
}
}()
}
func (reporter *statsReporter) shutdown() {
reporter.statsTicker <- true
}