-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
125 lines (108 loc) · 3.06 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
package main
import (
"flag"
"log"
"strings"
"time"
influxdb "github.com/influxdata/influxdb/client/v2"
pinba "github.com/olegfedoseev/pinba-server/client"
)
var version = "master"
func newInfluxdbClient(cfg *config, userAgent string) (influxdb.Client, error) {
if strings.HasPrefix(cfg.Influxdb.Addr, "http") {
return influxdb.NewHTTPClient(influxdb.HTTPConfig{
Addr: cfg.Influxdb.Addr,
Username: cfg.Influxdb.User,
Password: cfg.Influxdb.Password,
UserAgent: userAgent,
})
}
return influxdb.NewUDPClient(influxdb.UDPConfig{
Addr: cfg.Influxdb.Addr,
})
}
func main() {
log.Println(version)
var configFile = flag.String("config", "config.yml", "config name, default - config.yml")
flag.Parse()
config, err := getConfig(*configFile)
if err != nil {
log.Fatalf("Failed to load config from %v: %v", *configFile, err)
}
influxdbClient, err := newInfluxdbClient(config, "pinba-influxer")
if err != nil {
log.Fatal(err)
}
pinbaClient, err := pinba.New(
config.Pinba.Addr,
config.Pinba.ConnectTimeout,
config.Pinba.ReadTimeout,
)
if err != nil {
log.Fatalf("Failed to create pinba client: %v", err)
}
go pinbaClient.Listen(1)
for {
select {
case requests := <-pinbaClient.Requests:
// Create a new point batch, error can be only in Precision parsing
batch, _ := influxdb.NewBatchPoints(influxdb.BatchPointsConfig{
Database: config.Influxdb.Database,
})
var cnt int64
for _, request := range requests.Requests {
server, _ := request.Tags.Get("server")
if len(config.Whitelist.ServerNames) > 0 && !in(server, config.Whitelist.ServerNames) {
continue
}
// Create a point and add to batch
datapoint, err := influxdb.NewPoint(
"requests", // table name
request.Tags.Filter(config.Whitelist.Tags).GetMap(),
map[string]interface{}{
"request_time": request.RequestTime,
},
time.Unix(requests.Timestamp, cnt), // cnt as nsec - so that all of our metrics have unique timestamp
)
if err != nil {
log.Printf("[ERROR] Failed to create datapoint: %v", err)
continue
}
cnt++
batch.AddPoint(datapoint)
// Request's Timers
for _, timer := range request.Timers {
datapoint, err := influxdb.NewPoint(
"timers", // table name
timer.Tags.Filter(config.Whitelist.Tags).GetMap(),
map[string]interface{}{
"value": timer.Value,
"hits": int64(timer.HitCount),
},
time.Unix(requests.Timestamp, cnt), // cnt as nsec - so that all of our metrics have unique timestamp
)
if err != nil {
log.Printf("[ERROR] Failed to create datapoint: %v", err)
continue
}
batch.AddPoint(datapoint)
cnt++
}
}
// Write the batch
t := time.Now()
if err := influxdbClient.Write(batch); err != nil {
log.Printf("Failed to write %v points for %v: %v", cnt, requests.Timestamp, err)
}
log.Printf("Writen %d datapoints in %v\n", cnt, time.Since(t))
}
}
}
func in(val string, slice []string) bool {
for _, v := range slice {
if v == val {
return true
}
}
return false
}