-
Notifications
You must be signed in to change notification settings - Fork 926
/
eventlogger.go
107 lines (80 loc) · 2.17 KB
/
eventlogger.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package bot
import (
"github.com/jonas747/yagpdb/bot/eventsystem"
"github.com/jonas747/yagpdb/common"
"sync"
"sync/atomic"
"time"
)
const EventLoggerPeriodDuration = time.Second * 10
var (
EventLogger = &eventLogger{}
)
type eventLogger struct {
sync.Mutex
totalStats [][]*int64
lastPeriod [][]int64
perPeriod [][]int64
numShards int
}
func (e *eventLogger) init(numShards int) {
e.totalStats = make([][]*int64, numShards)
e.lastPeriod = make([][]int64, numShards)
e.perPeriod = make([][]int64, numShards)
// Initialize these
for i, _ := range e.totalStats {
e.totalStats[i] = make([]*int64, len(eventsystem.AllEvents))
e.lastPeriod[i] = make([]int64, len(eventsystem.AllEvents))
e.perPeriod[i] = make([]int64, len(eventsystem.AllEvents))
for j, _ := range e.totalStats[i] {
e.totalStats[i][j] = new(int64)
}
}
}
func (e *eventLogger) run() {
ticker := time.NewTicker(EventLoggerPeriodDuration)
for {
<-ticker.C
e.flushStats()
}
}
func (e *eventLogger) GetStats() (total [][]int64, perPeriod [][]int64) {
e.Lock()
total = make([][]int64, len(e.totalStats))
perPeriod = make([][]int64, len(e.totalStats))
for i := 0; i < len(e.totalStats); i++ {
perPeriod[i] = make([]int64, len(e.totalStats[i]))
total[i] = make([]int64, len(e.totalStats[i]))
for j := 0; j < len(e.totalStats[i]); j++ {
perPeriod[i][j] = e.perPeriod[i][j]
total[i][j] = atomic.LoadInt64(e.totalStats[i][j])
}
}
e.Unlock()
return
}
func (e *eventLogger) flushStats() {
totalPerPeriod := int64(0)
e.Lock()
for i := 0; i < len(e.totalStats); i++ {
for j := 0; j < len(e.totalStats[i]); j++ {
currentVal := atomic.LoadInt64(e.totalStats[i][j])
e.perPeriod[i][j] = currentVal - e.lastPeriod[i][j]
e.lastPeriod[i][j] = currentVal
totalPerPeriod += e.perPeriod[i][j]
}
}
e.Unlock()
if common.Statsd != nil {
common.Statsd.Count("discord.processed.events", totalPerPeriod, nil, EventLoggerPeriodDuration.Seconds())
}
}
func (e *eventLogger) handleEvent(evt *eventsystem.EventData) {
if evt.Session == nil {
return
}
if int(evt.Type) >= len(eventsystem.AllEvents) {
return
}
atomic.AddInt64(e.totalStats[evt.Session.ShardID][evt.Type], 1)
}