forked from minotar/imgd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.go
282 lines (256 loc) · 7.49 KB
/
status.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Todo: Could we instead use the prometheus.DefaultGatherer and Gather() to then use the Prometheus counters vs. duplicating it
package main
import (
"encoding/json"
"runtime"
"time"
)
// The different MessageTypes for statusCollectorMessage
const (
StatusTypeErrored = iota
StatusTypeRequested
StatusTypeAPIRequested
StatusTypeUserRequested
StatusTypeCacheUUID
StatusTypeCacheUserData
StatusTypeCacheSkin
StatusTypeCacheSkinTransient
)
type statusCollectorMessage struct {
// The type of message this is.
MessageType uint
// If MessageType == StatusTypeRequested, StatusTypeAPIRequested or StatusTypeErrored then this is the state we are reporting.
StatusType string
}
type cacheStats struct {
Hits uint
FreshHits uint
StaleHits uint
Misses uint
Errors uint
Length uint
Size uint64
}
type StatusCollector struct {
info struct {
// Number of bytes allocated to the process.
ImgdMem uint64
// Time in seconds the process has been running for
Uptime int64
// Number of times an error has been recorded.
Errored map[string]uint
// Number of times a request type has been requested.
Requested map[string]uint
// Number of times an API request type has been made.
APIRequested map[string]uint
// Numner of times UUID vs. Username was requested
UserRequested map[string]uint
// Cache stats for the Username->UUID cache
CacheUUID cacheStats
// Cache stats for the UUID->UserData cache
CacheUserData cacheStats
// Cache stats for the SkinPath->Skin cache
CacheSkin cacheStats
// Cache stats for the SkinPath->SkinError cache
CacheSkinTransient cacheStats
}
// Unix timestamp the process was booted at.
StartedAt int64
// Channel for feeding in input data.
inputData chan statusCollectorMessage
}
func MakeStatsCollector() *StatusCollector {
collector := &StatusCollector{}
collector.StartedAt = time.Now().Unix()
collector.info.Errored = map[string]uint{}
collector.info.Requested = map[string]uint{}
collector.info.APIRequested = map[string]uint{}
collector.info.UserRequested = map[string]uint{}
collector.inputData = make(chan statusCollectorMessage, 5)
// Run a function every five seconds to collect time-based info.
go func() {
ticker := time.NewTicker(time.Second * 5)
for {
select {
case <-ticker.C:
collector.Collect()
case msg := <-collector.inputData:
collector.handleMessage(msg)
}
}
}()
return collector
}
// Message handler function, called inside goroutine.
func (s *StatusCollector) handleMessage(msg statusCollectorMessage) {
switch msg.MessageType {
case StatusTypeErrored:
err := msg.StatusType
errorCounter.WithLabelValues(err).Inc()
if _, exists := s.info.Errored[err]; exists {
s.info.Errored[err]++
} else {
s.info.Errored[err] = 1
}
case StatusTypeRequested:
req := msg.StatusType
requestCounter.WithLabelValues(req).Inc()
if _, exists := s.info.Requested[req]; exists {
s.info.Requested[req]++
} else {
s.info.Requested[req] = 1
}
case StatusTypeAPIRequested:
req := msg.StatusType
apiCounter.WithLabelValues(req).Inc()
if _, exists := s.info.APIRequested[req]; exists {
s.info.APIRequested[req]++
} else {
s.info.APIRequested[req] = 1
}
case StatusTypeUserRequested:
req := msg.StatusType
userCounter.WithLabelValues(req).Inc()
if _, exists := s.info.UserRequested[req]; exists {
s.info.UserRequested[req]++
} else {
s.info.UserRequested[req] = 1
}
case StatusTypeCacheUUID:
req := msg.StatusType
cacheCounter.WithLabelValues("cacheUUID", req).Inc()
switch req {
case "hit":
s.info.CacheUUID.Hits++
case "fresh":
s.info.CacheUUID.FreshHits++
case "stale":
s.info.CacheUUID.StaleHits++
case "miss":
s.info.CacheUUID.Misses++
case "error":
s.info.CacheUUID.Errors++
}
case StatusTypeCacheUserData:
req := msg.StatusType
cacheCounter.WithLabelValues("cacheUserData", req).Inc()
switch req {
case "hit":
s.info.CacheUserData.Hits++
case "fresh":
s.info.CacheUserData.FreshHits++
case "stale":
s.info.CacheUserData.StaleHits++
case "miss":
s.info.CacheUserData.Misses++
case "error":
s.info.CacheUserData.Errors++
}
case StatusTypeCacheSkin:
req := msg.StatusType
cacheCounter.WithLabelValues("cacheSkin", req).Inc()
switch req {
case "hit":
s.info.CacheSkin.Hits++
case "fresh":
s.info.CacheSkin.FreshHits++
case "stale":
s.info.CacheSkin.StaleHits++
case "miss":
s.info.CacheSkin.Misses++
case "error":
s.info.CacheSkin.Errors++
}
case StatusTypeCacheSkinTransient:
req := msg.StatusType
cacheCounter.WithLabelValues("cacheSkinTransient", req).Inc()
switch req {
case "hit":
s.info.CacheSkinTransient.Hits++
case "fresh":
s.info.CacheSkinTransient.FreshHits++
case "stale":
s.info.CacheSkinTransient.StaleHits++
case "miss":
s.info.CacheSkinTransient.Misses++
case "error":
s.info.CacheSkinTransient.Errors++
}
}
}
// Encodes the info struct to a JSON string byte slice
func (s *StatusCollector) ToJSON() []byte {
results, _ := json.Marshal(s.info)
return results
}
// "cron" function that updates current information
func (s *StatusCollector) Collect() {
memstats := &runtime.MemStats{}
runtime.ReadMemStats(memstats)
s.info.ImgdMem = memstats.Alloc
s.info.Uptime = time.Now().Unix() - s.StartedAt
s.info.CacheUUID.Length = cache["cacheUUID"].Len()
s.info.CacheUUID.Size = cache["cacheUUID"].Size()
s.info.CacheUserData.Length = cache["cacheUserData"].Len()
s.info.CacheUserData.Size = cache["cacheUserData"].Size()
s.info.CacheSkin.Length = cache["cacheSkin"].Len()
s.info.CacheSkin.Size = cache["cacheSkin"].Size()
s.info.CacheSkinTransient.Length = cache["cacheSkinTransient"].Len()
s.info.CacheSkinTransient.Size = cache["cacheSkinTransient"].Size()
}
// Increments the error counter for the specific type.
func (s *StatusCollector) Errored(errorType string) {
s.inputData <- statusCollectorMessage{
MessageType: StatusTypeErrored,
StatusType: errorType,
}
}
// Increments the request counter for the specific type.
func (s *StatusCollector) Requested(reqType string) {
s.inputData <- statusCollectorMessage{
MessageType: StatusTypeRequested,
StatusType: reqType,
}
}
// Increments the request counter for the specific type.
func (s *StatusCollector) APIRequested(reqType string) {
s.inputData <- statusCollectorMessage{
MessageType: StatusTypeAPIRequested,
StatusType: reqType,
}
}
// Increments the request counter for the specific type.
func (s *StatusCollector) UserRequested(reqType string) {
s.inputData <- statusCollectorMessage{
MessageType: StatusTypeUserRequested,
StatusType: reqType,
}
}
// Increments the cache counter of the specifc type ("hit" or "miss").
func (s *StatusCollector) CacheUUID(statType string) {
s.inputData <- statusCollectorMessage{
MessageType: StatusTypeCacheUUID,
StatusType: statType,
}
}
// Increments the cache counter of the specifc type ("hit" or "miss").
func (s *StatusCollector) CacheUserData(statType string) {
s.inputData <- statusCollectorMessage{
MessageType: StatusTypeCacheUserData,
StatusType: statType,
}
}
// Increments the cache counter of the specifc type ("hit" or "miss").
func (s *StatusCollector) CacheSkin(statType string) {
s.inputData <- statusCollectorMessage{
MessageType: StatusTypeCacheSkin,
StatusType: statType,
}
}
// Increments the cache counter of the specifc type ("hit" or "miss").
func (s *StatusCollector) CacheSkinTransient(statType string) {
s.inputData <- statusCollectorMessage{
MessageType: StatusTypeCacheSkinTransient,
StatusType: statType,
}
}