-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
527 lines (416 loc) · 11.2 KB
/
main.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
// memory-backed carbon store: speaks graphite in, zipper out
// At the moment, shares a lot of code with grobian/carbonserver
package main
import (
"bufio"
"encoding/json"
"errors"
"expvar"
"flag"
"fmt"
"math"
"net"
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
pb "github.com/go-graphite/carbonzipper/carbonzipperpb3"
"github.com/go-graphite/carbonzipper/mlog"
"github.com/go-graphite/carbonzipper/mstats"
"github.com/peterbourgon/g2g"
"github.com/go-graphite/carbonmem/mwhisper"
)
var BuildVersion = "(development build)"
var Metrics = struct {
FindRequests *expvar.Int
FetchRequests *expvar.Int
}{
FindRequests: expvar.NewInt("findRequests"),
FetchRequests: expvar.NewInt("fetchRequests"),
}
var logger mlog.Level
func parseTopK(query string) (string, int32, bool) {
// prefix.blah.*.TopK.10m => "prefix.blah.*", 600, true
var idx int
if idx = strings.Index(query, ".TopK."); idx == -1 {
// not found
return "", 0, false
}
prefix := query[:idx]
timeIdx := idx + len(".TopK.")
// look for number followed by 'm' or 's'
unitsIdx := timeIdx
for unitsIdx < len(query) && '0' <= query[unitsIdx] && query[unitsIdx] <= '9' {
unitsIdx++
}
// ran off the end or no numbers present
if unitsIdx == len(query) || unitsIdx == timeIdx {
return "", 0, false
}
multiplier := 0
switch query[unitsIdx] {
case 's':
multiplier = 1
case 'm':
multiplier = 60
default:
// unknown units
return "", 0, false
}
if unitsIdx != len(query)-1 {
return "", 0, false
}
timeUnits, err := strconv.Atoi(query[timeIdx:unitsIdx])
if err != nil {
return "", 0, false
}
return prefix, int32(timeUnits * multiplier), true
}
func findHandler(w http.ResponseWriter, req *http.Request) {
Metrics.FindRequests.Add(1)
query := req.FormValue("query")
format := req.FormValue("format")
if format != "json" && format != "protobuf3" && format != "protobuf" {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
matches := findMetrics(query)
response := pb.GlobResponse{
Name: query,
Matches: matches,
}
var b []byte
switch format {
case "json":
w.Header().Set("Content-Type", "application/json")
b, _ = json.Marshal(response)
case "protobuf", "protobuf3":
w.Header().Set("Content-Type", "application/protobuf")
b, _ = response.Marshal()
}
w.Write(b)
}
func hasMetaCharacters(query string) bool {
return strings.IndexByte(query, '*') != -1 || strings.IndexByte(query, '[') != -1 || strings.IndexByte(query, '?') != -1
}
func findMetrics(query string) []pb.GlobMatch {
var topk string
var globs []mwhisper.Glob
if strings.Count(query, ".") < Whispers.prefix {
globs = Whispers.Glob(query)
} else {
if m := Whispers.Fetch(query); m != nil {
if prefix, seconds, ok := parseTopK(query); ok {
topk = query[len(prefix):]
if hasMetaCharacters(query) {
globs = m.TopK(prefix, seconds)
} else {
globs = m.Find(prefix)
}
} else {
globs = m.Find(query)
}
}
}
var matches []pb.GlobMatch
paths := make(map[string]struct{}, len(globs))
for _, g := range globs {
// fix up metric name
metric := g.Metric + topk
if _, ok := paths[metric]; !ok {
m := pb.GlobMatch{
Path: metric,
IsLeaf: g.IsLeaf,
}
matches = append(matches, m)
paths[metric] = struct{}{}
}
}
return matches
}
func renderHandler(w http.ResponseWriter, req *http.Request) {
Metrics.FetchRequests.Add(1)
target := req.FormValue("target")
format := req.FormValue("format")
from := req.FormValue("from")
until := req.FormValue("until")
frint, _ := strconv.Atoi(from)
unint, _ := strconv.Atoi(until)
if unint < frint {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
if format != "json" && format != "protobuf3" && format != "protobuf" {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
matches := findMetrics(target)
var multi pb.MultiFetchResponse
for _, m := range matches {
target := m.GetPath()
var metric string
if prefix, _, ok := parseTopK(target); ok {
metric = prefix
} else {
metric = target
}
metrics := Whispers.Fetch(metric)
if metrics == nil {
continue
}
points := metrics.Fetch(metric, int32(frint), int32(unint))
if points == nil {
continue
}
fromTime := points.From
untilTime := points.Until
step := points.Step
response := pb.FetchResponse{
Name: target,
StartTime: fromTime,
StopTime: untilTime,
StepTime: step,
Values: make([]float64, len(points.Values)),
IsAbsent: make([]bool, len(points.Values)),
}
for i, p := range points.Values {
if math.IsNaN(p) {
response.Values[i] = 0
response.IsAbsent[i] = true
} else {
response.Values[i] = p
response.IsAbsent[i] = false
}
}
multi.Metrics = append(multi.Metrics, response)
}
var b []byte
switch format {
case "json":
w.Header().Set("Content-Type", "application/json")
b, _ = json.Marshal(multi)
case "protobuf", "protobuf3":
w.Header().Set("Content-Type", "application/protobuf")
b, _ = multi.Marshal()
}
w.Write(b)
}
func graphiteServer(port int) {
ln, e := net.Listen("tcp", ":"+strconv.Itoa(port))
if e != nil {
logger.Fatalln("listen error:", e)
}
logger.Logln("graphite server starting on port", port)
for {
conn, err := ln.Accept()
if err != nil {
logger.Logln(err)
continue
}
go func(c net.Conn) {
scanner := bufio.NewScanner(c)
for scanner.Scan() {
metric, count, epoch, err := parseGraphite(scanner.Bytes())
if err != nil {
continue
}
metrics := Whispers.FetchOrCreate(metric)
metrics.Set(int32(epoch), metric, uint64(count))
}
if err := scanner.Err(); err != nil {
logger.Logf("graphite server: error during scan: %v", err)
}
c.Close()
}(conn)
}
}
func isspace(c byte) bool {
return c == ' ' || c == '\t' || c == '\r' || c == '\n'
}
var errParseError = errors.New("graphite: parse error")
func token(b []byte) ([]byte, []byte) {
if len(b) == 0 {
return nil, nil
}
// munch space
for len(b) > 0 && isspace(b[0]) {
b = b[1:]
}
var i int
for i < len(b) && !isspace(b[i]) {
i++
}
return b[:i], b[i:]
}
func parseGraphite(b []byte) (metric string, count int, epoch int, err error) {
var tok []byte
tok, b = token(b)
if len(tok) == 0 {
return "", 0, 0, errParseError
}
metric = string(tok)
tok, b = token(b)
if len(tok) == 0 {
return "", 0, 0, errParseError
}
count, err = strconv.Atoi(string(tok))
if err != nil {
return "", 0, 0, errParseError
}
tok, b = token(b)
if len(tok) == 0 {
return "", 0, 0, errParseError
}
epoch, err = strconv.Atoi(string(tok))
if err != nil {
return "", 0, 0, errParseError
}
// check for extra stuff
tok, b = token(b)
if len(tok) != 0 || len(b) != 0 {
return "", 0, 0, errParseError
}
return metric, count, epoch, nil
}
type whispers struct {
sync.RWMutex
metrics map[string]*mwhisper.Whisper
windowSize int
epochSize int
epoch0 int
prefix int
}
var Whispers whispers
func findNodePrefix(prefix int, metric string) string {
var found int
for i, c := range metric {
if c == '.' {
found++
if found >= prefix {
return metric[:i]
}
}
}
return metric
}
func (w *whispers) FetchOrCreate(metric string) *mwhisper.Whisper {
m := w.Fetch(metric)
if m == nil {
prefix := findNodePrefix(w.prefix, metric)
var ok bool
w.Lock()
m, ok = w.metrics[prefix]
if !ok {
m = mwhisper.NewWhisper(int32(w.epoch0), w.epochSize, w.windowSize, mwhisper.TrigramCutoff(100000))
w.metrics[prefix] = m
}
w.Unlock()
}
return m
}
func (w *whispers) Fetch(metric string) *mwhisper.Whisper {
prefix := findNodePrefix(w.prefix, metric)
w.RLock()
m := w.metrics[prefix]
w.RUnlock()
return m
}
func (w *whispers) Glob(query string) []mwhisper.Glob {
query = strings.Replace(query, ".", "/", -1)
slashes := strings.Count(query, "/")
w.RLock()
var glob []mwhisper.Glob
for m := range w.metrics {
qm := strings.Replace(m, ".", "/", slashes)
if trim := strings.Index(qm, "."); trim != -1 {
qm = qm[:trim]
m = m[:trim]
}
if match, err := filepath.Match(query, qm); err == nil && match {
glob = append(glob, mwhisper.Glob{Metric: m})
}
}
w.RUnlock()
return glob
}
func main() {
Whispers.metrics = make(map[string]*mwhisper.Whisper)
flag.IntVar(&Whispers.windowSize, "w", 600, "window size")
flag.IntVar(&Whispers.epochSize, "e", 60, "epoch window size")
flag.IntVar(&Whispers.epoch0, "epoch0", 0, "epoch0")
flag.IntVar(&Whispers.prefix, "prefix", 0, "prefix nodes to shard on")
port := flag.Int("p", 8001, "port to listen on (http)")
gport := flag.Int("gp", 2003, "port to listen on (graphite)")
verbose := flag.Bool("v", false, "verbose logging")
logdir := flag.String("logdir", "/var/log/carbonmem/", "logging directory")
logtostdout := flag.Bool("stdout", false, "log also to stdout")
graphiteHost := flag.String("graphite", "", "graphite destination host")
interval := flag.Duration("interval", 60*time.Second, "interval to report internal statistics to graphite")
flag.Parse()
if *logdir == "" {
mlog.SetRawStream(os.Stdout)
} else {
mlog.SetOutput(*logdir, "carbonmem", *logtostdout)
}
if *verbose {
logger = mlog.Debug
}
expvar.NewString("BuildVersion").Set(BuildVersion)
logger.Logln("starting carbonmem", BuildVersion)
expvar.Publish("Whispers", expvar.Func(func() interface{} {
m := make(map[string]int)
Whispers.RLock()
for k, v := range Whispers.metrics {
m[k] = v.Len()
}
Whispers.RUnlock()
return m
}))
if Whispers.epoch0 == 0 {
Whispers.epoch0 = int(time.Now().Unix())
}
if envhost := os.Getenv("GRAPHITEHOST") + ":" + os.Getenv("GRAPHITEPORT"); envhost != ":" || *graphiteHost != "" {
var host string
switch {
case envhost != ":" && *graphiteHost != "":
host = *graphiteHost
case envhost != ":":
host = envhost
case *graphiteHost != "":
host = *graphiteHost
}
logger.Logln("Using graphite host", host)
logger.Logln("setting stats interval to", *interval)
// register our metrics with graphite
graphite := g2g.NewGraphite(host, *interval, 10*time.Second)
hostname, _ := os.Hostname()
hostname = strings.Replace(hostname, ".", "_", -1)
graphite.Register(fmt.Sprintf("carbon.mem.%s.find_requests", hostname), Metrics.FindRequests)
graphite.Register(fmt.Sprintf("carbon.mem.%s.fetch_requests", hostname), Metrics.FetchRequests)
go mstats.Start(*interval)
graphite.Register(fmt.Sprintf("carbon.mem.%s.alloc", hostname), &mstats.Alloc)
graphite.Register(fmt.Sprintf("carbon.mem.%s.total_alloc", hostname), &mstats.TotalAlloc)
graphite.Register(fmt.Sprintf("carbon.mem.%s.num_gc", hostname), &mstats.NumGC)
graphite.Register(fmt.Sprintf("carbon.mem.%s.pause_ns", hostname), &mstats.PauseNS)
}
go graphiteServer(*gport)
http.HandleFunc("/metrics/find/", accessHandler(*verbose, findHandler))
http.HandleFunc("/render/", accessHandler(*verbose, renderHandler))
logger.Logln("http server starting on port", *port)
logger.Fatalln(http.ListenAndServe(":"+strconv.Itoa(*port), nil))
}
func accessHandler(verbose bool, handler http.HandlerFunc) http.HandlerFunc {
if !verbose {
return handler
}
return func(w http.ResponseWriter, r *http.Request) {
t0 := time.Now()
handler(w, r)
since := time.Since(t0)
logger.Logln(r.RequestURI, since.Nanoseconds()/int64(time.Millisecond))
}
}