/
pinba-router.go
169 lines (151 loc) · 3.94 KB
/
pinba-router.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
package main
import (
"flag"
"fmt"
"log"
"net"
"net/url"
"time"
"github.com/golang/protobuf/proto"
client "github.com/influxdb/influxdb/client/v2"
"github.com/mkevac/gopinba/Pinba"
)
var (
inAddr = flag.String("in", "0.0.0.0:30002", "incoming socket")
)
func main() {
flag.Parse()
addr, err := net.ResolveUDPAddr("udp4", *inAddr)
if err != nil {
log.Fatalf("Can't resolve address: '%v'", err)
}
sock, err := net.ListenUDP("udp4", addr)
if err != nil {
log.Fatalf("Can't open UDP socket: '%v'", err)
}
log.Printf("Start listening on udp://%v\n", *inAddr)
defer sock.Close()
u, _ := url.Parse("http://localhost:8086")
c := client.NewClient(client.Config{
URL: u,
Username: "username",
Password: "password",
})
sinkChan := make(chan Pinba.Request)
bpc := client.BatchPointsConfig{
Precision: "us",
Database: "pinba",
}
sink := NewInfluxDBSink(100, sinkChan, c, bpc)
go sink.run()
for {
var buf = make([]byte, 65536)
rlen, _, err := sock.ReadFromUDP(buf)
if err != nil {
log.Fatalf("Error on sock.ReadFrom, %v", err)
}
if rlen == 0 {
continue
}
request := &Pinba.Request{}
proto.Unmarshal(buf[0:rlen], request)
log.Printf("%v", request)
sinkChan <- *request
}
}
type InfluxDBSink struct {
bufferLen int
input chan Pinba.Request
aggregated chan []Pinba.Request
aggregator Aggregator
client client.Client
BatchPointsConfig client.BatchPointsConfig
}
func NewInfluxDBSink(bufferLen int, input chan Pinba.Request, client client.Client, batchPointsConfig client.BatchPointsConfig) *InfluxDBSink {
log.Printf("Sink create")
ch := make(chan []Pinba.Request)
aggregator := NewAggregator(bufferLen, input, ch)
sink := &InfluxDBSink{
bufferLen: bufferLen,
input: input,
aggregated: ch,
aggregator: *aggregator,
client: client,
BatchPointsConfig: batchPointsConfig,
}
return sink
}
func (sink *InfluxDBSink) run() {
log.Printf("Sink run")
go sink.aggregator.run()
for {
log.Printf("Sink wait for aggregated")
batchRequest := <-sink.aggregated
log.Printf("Sink got aggregated")
bp, err := client.NewBatchPoints(sink.BatchPointsConfig)
if err != nil {
log.Fatal(err)
}
for _, req := range batchRequest {
fields := make(map[string]interface{})
log.Printf("%v", req)
fields["request_time"] = req.GetRequestTime()
fields["ru_utime"] = req.GetRuUtime()
fields["ru_stime"] = req.GetRuStime()
tags := make(map[string]string)
tags["server_name"] = req.GetServerName()
tags["hostname"] = req.GetHostname()
tags["script_name"] = req.GetScriptName()
point := client.NewPoint("request", tags, fields, time.Now())
bp.AddPoint(point)
}
err = sink.client.Write(bp)
if err != nil {
log.Fatal(err)
}
}
}
func WriteRequest(request Pinba.Request) error {
return fmt.Errorf("not implemented")
}
type Aggregator struct {
n int
buf []Pinba.Request
input chan Pinba.Request
output chan []Pinba.Request
}
func NewAggregator(n int, input chan Pinba.Request, output chan []Pinba.Request) *Aggregator {
return &Aggregator{
n: n,
input: input,
output: output,
}
}
func (aggregator *Aggregator) run() {
timerChannel := make(<-chan time.Time)
realOutput := aggregator.output
var output chan []Pinba.Request
for {
select {
case output <- aggregator.buf:
log.Println("case output <- aggregator.buf")
timerChannel = nil
aggregator.buf = nil
output = nil
case request := <-aggregator.input:
log.Println("case request := <-aggregator.input")
if len(aggregator.buf) == 0 {
timerChannel = time.After(time.Second)
}
aggregator.buf = append(aggregator.buf, request)
if len(aggregator.buf) >= aggregator.n {
log.Printf("len(aggregator.buf) = %v, aggregator.n = %v", len(aggregator.buf), aggregator.n)
output = realOutput
}
case <-timerChannel:
log.Println("case <-timerChannel")
timerChannel = nil
output = realOutput
}
}
}