-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (116 loc) · 3.36 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/fsnotify/fsnotify"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
uuid "github.com/satori/go.uuid"
"github.com/spf13/viper"
"github.com/streadway/amqp"
statsd "gopkg.in/alexcesaro/statsd.v2"
"github.com/vgno/ssehub-prober/sse"
)
func failOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
}
}
var probesSent = make(map[string]time.Time)
var probesReceived = make(chan *sse.Event)
var statsChannel = make(chan float64)
func amqpHandler() {
host := fmt.Sprintf("amqp://%s:%d/", viper.GetString("amqp.host"), viper.GetInt("amqp.port"))
log.Printf("AMQP: Connecting to %s", host)
conn, err := amqp.Dial(host)
failOnError(err, "AMQP: Failed to connect to RabbitMQ")
defer conn.Close()
log.Print("AMQP: Connected to amqp")
ch, err := conn.Channel()
failOnError(err, "AMQP: Failed to open a channel")
defer ch.Close()
for {
id, _ := uuid.NewV4()
body := fmt.Sprintf("{\"event\": \"probe\", \"id\": \"%s\", \"path\": \"%s\", \"data\": \"{}\"}", id.String(), viper.GetString("amqp.path"))
err = ch.Publish(
viper.GetString("amqp.exchange"),
"#",
false,
false,
amqp.Publishing{
ContentType: "application/json",
Body: []byte(body),
})
failOnError(err, "AMQP: Failed to publish a message")
log.Printf("AMQP: Sent %s", body)
probesSent[id.String()] = time.Now()
time.Sleep(time.Duration(viper.GetInt("ssehub.interval")) * time.Second)
}
}
func probeHandler() {
sse.Notify(viper.GetString("ssehub.url"), probesReceived, true)
for {
v := <-probesReceived
if v.Type == "probe" {
if probe, ok := probesSent[v.Id]; ok {
duration := time.Since(probe)
diff := float64(duration.Seconds() * 1e3)
delete(probesSent, v.Id)
statsChannel <- diff
}
}
}
}
func statsHandler() {
shouldTrackPrometheus := viper.GetBool("collectors.prometheus.enabled")
shouldTrackStatsd := viper.GetBool("collectors.statsd.enabled")
var statsdClient *statsd.Client
var statsdError error
if shouldTrackStatsd {
statsdClient, statsdError = statsd.New(
statsd.Address(viper.GetString("collectors.statsd.address")),
)
failOnError(statsdError, "StatsD: Failed to connect to statsd")
}
for {
v := <-statsChannel
log.Printf("Oberserving value %f", v)
if shouldTrackPrometheus {
prometheusStats.Observe(v)
}
if shouldTrackStatsd {
statsdClient.Histogram("ssehub.response_time", v)
}
}
}
func healthzHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "OK")
}
var (
prometheusStats = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "ssehub_response_time_miliseconds",
Help: "Response time of the ssehub.",
})
)
func init() {
// Register the summary and the histogram with Prometheus's default registry.
prometheus.MustRegister(prometheusStats)
}
func main() {
viper.AddConfigPath(".")
viper.SetConfigName("config")
err := viper.ReadInConfig() // Find and read the config file
failOnError(err, "Could not read config file")
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
log.Printf("CONFIG: file changed:", e.Name)
})
go probeHandler()
go amqpHandler()
go statsHandler()
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/healthz", healthzHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}