-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcounter.go
339 lines (302 loc) · 7.54 KB
/
counter.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"sort"
"strings"
"sync"
"time"
"github.com/go-chi/chi"
"github.com/wcharczuk/go-chart"
)
type metric struct {
mutex sync.Mutex
datapoints map[string]datapoint
}
type metricList struct {
Metrics []string `json:"metrics"`
}
type datapoint struct {
Value float64 `json:"value"`
Count int `json:"count"`
Average float64 `json:"average"`
}
type error struct {
Error string `json:"error"`
}
type status struct {
Status string `json:"status"`
}
var data map[string]*metric
var globalLock sync.Mutex
const persistenceInterval = 30 * time.Second
const lruInterval = 5 * time.Minute
const retentionDuration = time.Duration(6 * time.Hour)
const dataDir = "data"
func getMetricList(w http.ResponseWriter, r *http.Request) {
globalLock.Lock()
var metrics []string
for k := range data {
metrics = append(metrics, k)
}
sort.Strings(metrics)
globalLock.Unlock()
w.Header().Set("Content-Type", "application/json")
response, err := json.Marshal(metricList{Metrics: metrics})
if err != nil {
panic(err)
}
w.Write(response)
}
func getMetric(w http.ResponseWriter, r *http.Request) {
metricName := chi.URLParam(r, "metricName")
w.Header().Set("Content-Type", "application/json")
// Find the requested data
globalLock.Lock()
m, ok := data[metricName]
globalLock.Unlock()
if ok {
m.mutex.Lock()
response, err := json.Marshal(m.datapoints)
if err != nil {
panic(err)
}
m.mutex.Unlock()
w.Write(response)
} else {
response, err := json.Marshal(error{Error: "Could not find metric"})
if err != nil {
panic(err)
}
w.Write(response)
}
}
func getMetricChart(w http.ResponseWriter, r *http.Request) {
metricName := chi.URLParam(r, "metricName")
dimensionName := chi.URLParam(r, "dimensionName")
zoneName, _ := time.Now().Zone()
// Find the requested data
globalLock.Lock()
m, ok := data[metricName]
globalLock.Unlock()
if ok {
m.mutex.Lock()
// Get list of keys in order
var keys []string
for k := range m.datapoints {
keys = append(keys, k)
}
sort.Strings(keys)
// Build up dataset
XValues := make([]time.Time, 0)
YValues := make([]float64, 0)
for _, k := range keys {
v := m.datapoints[k]
t, err := time.Parse("2006-01-02 15:04 MST", fmt.Sprintf("%s %s", k, zoneName))
if err != nil {
fmt.Println("Could not parse time value")
continue
}
XValues = append(XValues, t)
switch dimensionName {
case "sum":
YValues = append(YValues, v.Value)
case "count":
YValues = append(YValues, float64(v.Count))
default:
YValues = append(YValues, v.Average)
}
}
// Generate chart
graph := chart.Chart{
XAxis: chart.XAxis{
ValueFormatter: chart.TimeMinuteValueFormatter,
},
Series: []chart.Series{
chart.TimeSeries{
XValues: XValues,
YValues: YValues,
},
},
}
m.mutex.Unlock()
w.Header().Set("Content-Type", "image/png")
graph.Render(chart.PNG, w)
} else {
w.Header().Set("Content-Type", "application/json")
response, err := json.Marshal(error{Error: "Could not find metric"})
if err != nil {
panic(err)
}
w.Write(response)
}
}
func writeMetrics(writeBuffer chan map[string]datapoint) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var sample map[string]datapoint
err := decoder.Decode(&sample)
if err != nil {
w.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(error{Error: "Could not parse incoming metrics"})
w.WriteHeader(http.StatusBadRequest)
w.Write(response)
return
}
// Queue data for processing and return response
writeBuffer <- sample
w.Header().Set("Content-Type", "application/json")
response, _ := json.Marshal(status{Status: "Queued"})
w.Write(response)
}
}
func appendMetrics(writeBuffer chan map[string]datapoint) {
for {
sample := <-writeBuffer
timeKey := time.Now().Format("2006-01-02 15:04")
for metricName, d := range sample {
globalLock.Lock()
m, ok := data[metricName]
globalLock.Unlock()
// Write count as 1 if no value received
if d.Count == 0 {
d.Count = 1
}
// Increment point if it exists, otherwise create a new point
if ok {
m.mutex.Lock()
if point, ok := m.datapoints[timeKey]; ok {
point.Count += d.Count
point.Value += d.Value
point.Average = point.Value / float64(point.Count)
m.datapoints[timeKey] = point
} else {
point := datapoint{Count: d.Count, Value: d.Value, Average: d.Value / float64(d.Count)}
m.datapoints[timeKey] = point
}
m.mutex.Unlock()
} else {
globalLock.Lock()
data[metricName] = &metric{
datapoints: make(map[string]datapoint, 0),
}
globalLock.Unlock()
}
}
}
}
func restore() {
globalLock.Lock()
files, err := ioutil.ReadDir(dataDir)
if err != nil {
fmt.Println("Data directory could not be read, restore skipped")
return
}
for _, f := range files {
metricName := strings.Replace(f.Name(), ".json", "", 1)
datapoints := make(map[string]datapoint, 0)
file, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", dataDir, f.Name()))
if err != nil {
fmt.Printf("Could not read %s/%s\n", dataDir, f.Name())
continue
}
json.Unmarshal(file, &datapoints)
data[metricName] = &metric{
datapoints: datapoints,
}
}
globalLock.Unlock()
}
func lru() {
for {
// Determine cut-off point
oldestTimeKey := time.Now().Add(-1 * retentionDuration).Format("2006-01-02 15:04")
// Get unique metric names
var metricNames []string
globalLock.Lock()
for metricName := range data {
metricNames = append(metricNames, metricName)
}
globalLock.Unlock()
for _, metricName := range metricNames {
// Get metric
globalLock.Lock()
m, ok := data[metricName]
if !ok {
continue
}
globalLock.Unlock()
// Remove old data
m.mutex.Lock()
for timeKey := range m.datapoints {
if timeKey < oldestTimeKey {
delete(m.datapoints, timeKey)
if debugMode() {
fmt.Printf("Removing old datapoint %s for %s\n", timeKey, metricName)
}
}
}
m.mutex.Unlock()
}
time.Sleep(lruInterval)
}
}
func persist() {
if _, err := os.Stat(dataDir); os.IsNotExist(err) {
os.Mkdir(dataDir, 0700)
}
for {
time.Sleep(persistenceInterval)
globalLock.Lock()
for metricName, m := range data {
m.mutex.Lock()
file, err := os.OpenFile(
fmt.Sprintf("%s/%s.json", dataDir, metricName),
os.O_WRONLY|os.O_CREATE|os.O_TRUNC,
os.ModePerm,
)
if err != nil {
fmt.Printf("Could not write %s to disk\n", metricName)
continue
}
encoder := json.NewEncoder(file)
encoder.Encode(m.datapoints)
file.Close()
if debugMode() {
fmt.Printf("Persisted %s to disk\n", metricName)
}
m.mutex.Unlock()
}
globalLock.Unlock()
}
}
func debugMode() bool {
return os.Getenv("DEBUG") != ""
}
func main() {
// Set up router
r := chi.NewRouter()
writeBuffer := make(chan map[string]datapoint, 1000)
r.Get("/metric", getMetricList)
r.Post("/metric", writeMetrics(writeBuffer))
r.Get("/metric/{metricName:[a-zA-Z0-9-.]+}", getMetric)
r.Get("/metric/{metricName:[a-zA-Z0-9-.]+}/{dimensionName:[a-z]+}.png", getMetricChart)
// Set up global data store
data = make(map[string]*metric)
restore()
// Set up buffered writer for incoming data
go appendMetrics(writeBuffer)
// LRU goroutine
go lru()
// Peristence goroutine
go persist()
// Start server
port := "8080"
fmt.Printf("Counter server started on port %s\n", port)
if err := http.ListenAndServe(fmt.Sprintf(":%s", port), r); err != nil {
panic(err)
}
}