-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.go
68 lines (60 loc) · 1.39 KB
/
collector.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
package main
import (
"encoding/json"
"fmt"
"github.com/go-redis/redis"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
func writetoRedis(rediskey string, redisVal string) {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
fmt.Println(redisVal)
llength, _ := client.LLen(rediskey).Result()
fmt.Println(llength)
if llength == 100 {
client.LPop(rediskey)
}
client.RPush(rediskey, redisVal)
}
func main() {
user := os.Args[1]
host := os.Args[2]
netService := os.Args[3]
app := os.Args[4]
time.Sleep(time.Second * 5)
for {
collect(user, host, netService, app)
time.Sleep(time.Second * 1)
}
}
type CollectorData struct {
Timestamp int64 `json:"timestamp"`
Data json.RawMessage `json:"data"`
}
func collect(user, host, service, app string) {
url := "http://167.99.213.33:9000/v1/data"
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
responsedata, errori := ioutil.ReadAll(resp.Body)
if errori != nil {
log.Fatal(errori)
}
rediskey := fmt.Sprintf("heatmaps:%s:%s:%s:%s:latency", user, host, service, app)
//b, erro := json.Marshal(resp)
fmt.Println(string(responsedata))
m := CollectorData{Data: responsedata, Timestamp: time.Now().Unix()}
data, errorios := json.Marshal(m)
if errorios != nil {
log.Fatal(errorios)
}
writetoRedis(rediskey, string(data))
}