/
stats.go
201 lines (183 loc) · 3.86 KB
/
stats.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"runtime"
"strings"
"sync"
"time"
log "github.com/Sirupsen/logrus"
influx "github.com/influxdata/influxdb/client/v2"
)
type AccumulatedStats struct {
sync.Mutex
since time.Time
submittedTasks int
completedTasks int
failedTasks int
}
func (s *AccumulatedStats) Reset() {
s.Lock()
s.since = time.Now()
s.submittedTasks = 0
s.completedTasks = 0
s.failedTasks = 0
s.Unlock()
}
func (s *AccumulatedStats) With(f func(s *AccumulatedStats)) {
s.Lock()
f(s)
s.Unlock()
}
type Stats struct {
imp chan interface{}
mem runtime.MemStats
client influx.Client
influxConfig influx.HTTPConfig
accumulated *AccumulatedStats
bp influx.BatchPoints
}
func (s *Stats) initClient() (err error) {
log.WithField("Addr", s.influxConfig.Addr).Debug("connecting to influxdb")
s.client, err = influx.NewHTTPClient(s.influxConfig)
if err != nil {
return err
}
defer s.client.Close()
ping, _, err := s.client.Ping(time.Second)
if err != nil {
return err
}
log.WithField("ping", ping).Debug("influx ping")
s.bp, err = influx.NewBatchPoints(influx.BatchPointsConfig{
Database: "dicod",
Precision: "s",
})
return err
}
func (s *Stats) Pulse() {
s.imp <- nil
}
func (s *Stats) reportRuntimeStats() {
runtime.ReadMemStats(&s.mem)
s.newPoint(
"memory",
"total_alloc", s.mem.TotalAlloc,
"alloc", s.mem.Alloc,
"heap_sys", s.mem.HeapSys,
"next_gc", s.mem.NextGC,
)
s.newPoint("goroutines", runtime.NumGoroutine())
s.newPoint("next_task", currTaskID)
}
func (s *Stats) reportAccumulated() {
s.accumulated.Lock()
//sinceSec := float64(s.accumulated.since.Unix())
s.newPoint(
"tasks",
"submitted", s.accumulated.submittedTasks,
"completed", s.accumulated.completedTasks,
"failed", s.accumulated.failedTasks,
)
s.accumulated.Unlock()
s.accumulated.Reset()
}
func (s *Stats) newPoint(name string, values ...interface{}) {
var fields map[string]interface{}
if len(values) == 1 {
fields = map[string]interface{}{name: values[0]}
} else if len(values)%2 == 0 {
fields = make(map[string]interface{})
for i := 0; i < len(values)/2; i++ {
fields[values[i*2].(string)] = values[i*2+1]
}
} else {
panic("invalid number of args")
}
pt, err := influx.NewPoint(
strings.Replace(name, "_", "-", -1),
map[string]string{},
fields,
time.Now(),
)
s.bp.AddPoint(pt)
if err != nil {
log.Error(err)
}
}
func (s *Stats) sendPeriodicalReports() {
idleTicker := time.NewTicker(time.Minute)
ticker := time.NewTicker(time.Second)
for {
isImpulse := false
select {
case <-ticker.C:
anyActive := false
for w := range workers() {
anyActive = anyActive || w.active
}
if !anyActive {
continue
}
log.Debug("ticktock, active!")
case <-idleTicker.C:
log.Debug("ticktock, idle!")
case <-s.imp:
isImpulse = true
drainCh(s.imp)
}
var activeWorkers int
var numWorkers int
for w := range workers() {
numWorkers++
if w.active {
activeWorkers++
}
}
s.reportRuntimeStats()
if !isImpulse {
s.reportAccumulated()
}
s.newPoint("workers", "workers", numWorkers, "active", activeWorkers)
err := s.client.Write(s.bp)
if err != nil {
log.Error(err)
return
}
}
}
var stats Stats
var accumulatedStats AccumulatedStats
func init() {
go func() {
stats = Stats{
imp: make(chan interface{}, 50),
influxConfig: influx.HTTPConfig{ // TODO: read from config file
Addr: "http://influx.thuermchen.com",
Username: "dicod",
Password: "dicod",
},
}
err := stats.initClient()
if err != nil {
log.Error(err)
return
}
accumulatedStats.Reset()
stats.accumulated = &accumulatedStats
err = stats.client.Write(stats.bp)
if err != nil {
log.Error(err)
return
}
go stats.sendPeriodicalReports()
stats.Pulse()
}()
}
func drainCh(c chan interface{}) {
for {
select {
case <-c:
default:
return
}
}
}