/
main.go
79 lines (63 loc) · 1.59 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
package main
import (
"bufio"
"bytes"
"encoding/hex"
"flag"
"log"
"os"
"strconv"
"time"
"github.com/dt/go-metrics-reporting"
)
const (
RequestFlag = '1'
ResponseFlag = '2'
ReplayedResponseFlag = '3'
)
type SettingDefs struct {
src string
dst string
graphite string
prefix string
}
var Settings SettingDefs
func main() {
flag.StringVar(&Settings.src, "src", "src", "name for traffic src")
flag.StringVar(&Settings.dst, "dst", "dst", "name for traffic dst")
flag.StringVar(&Settings.graphite, "graphite", "", "name for traffic dst")
flag.StringVar(&Settings.prefix, "prefix", "", "prefix for reported timings")
flag.Parse()
if Settings.graphite == "" || Settings.prefix == "" {
log.Fatal("must supply graphite server and prefix")
}
report.NewRecorder().
ReportTo(Settings.graphite, Settings.prefix).
LogToConsole().
SetAsDefault()
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
encoded := scanner.Bytes()
buf := make([]byte, hex.DecodedLen(len(encoded)))
hex.Decode(buf, encoded)
kind := buf[0]
if kind == RequestFlag {
os.Stdout.Write(encoded)
}
go handle(kind, buf)
}
}
func handle(kind byte, buf []byte) {
headerSize := bytes.IndexByte(buf, '\n') + 1
header := buf[:headerSize-1]
meta := bytes.Split(header, []byte(" "))
//log.Println("meta", meta)
id := string(meta[1])
ts, _ := strconv.ParseInt(string(meta[2]), 10, 64)
log.Println(kind, id, ts)
if kind == ResponseFlag {
report.Time(Settings.src, time.Duration(ts))
} else if kind == ReplayedResponseFlag {
report.Time(Settings.src, time.Duration(ts))
}
}