-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (64 loc) · 1.52 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
package main
import (
"bufio"
"encoding/json"
"log"
"net/http"
"runtime"
"strings"
"time"
"github.com/ContaAzul/hystrix-to-librato/internal/config"
"github.com/ContaAzul/hystrix-to-librato/internal/models"
"github.com/ContaAzul/hystrix-to-librato/internal/report"
)
var waitTime = 5 * time.Second
func main() {
config := config.Get()
report := report.Librato(
config.User,
config.Token,
config.ReportLatencies,
time.Duration(config.ReportInterval)*time.Second,
)
for _, cluster := range config.Clusters {
go read(config.URL, cluster, report)
}
// sleep forever
for {
time.Sleep(waitTime)
log.Println(runtime.NumGoroutine(), "goroutines running...")
}
}
func read(url, cluster string, report report.Report) {
time.Sleep(waitTime)
log.Println("Starting", cluster)
resp, err := http.Get(url + "?cluster=" + cluster)
if err != nil {
log.Println(err)
read(url, cluster, report)
return
}
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Text()
if !isData(line) || !isCircuitReport(line) {
continue
}
doReport(report, cluster, line)
}
read(url, cluster, report)
}
func doReport(report report.Report, cluster, line string) {
line = strings.TrimPrefix(line, "data:")
var data models.Data
if err := json.Unmarshal([]byte(line), &data); err != nil {
return
}
go report.Report(data, cluster)
}
func isData(line string) bool {
return strings.HasPrefix(line, "data:")
}
func isCircuitReport(line string) bool {
return strings.Contains(line, "isCircuitBreakerOpen")
}