-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
67 lines (50 loc) · 1.55 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
package main
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net/http"
"time"
)
func main() {
hostname, err := getNodeName()
if err != nil {
log.Fatalf("failed getting hostname: %s", err)
}
// Prometheus: Histogram to collect required metrics
histogram := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "node_latency",
Help: "Time take ping other nodes",
Buckets: []float64{1, 2, 5, 6, 10}, //defining small buckets as this app should not take more than 1 sec to respond
}, []string{"origin_node", "destination_node"}) // this will be partitioned by nodes
err = prometheus.Register(histogram)
if err != nil {
log.Fatalf("failed registering historgram: %s", err)
}
go func() {
for {
hosts, err := getNodeList()
if err != nil {
log.Fatalf("failed getting node list: %s", err)
}
if len(hosts) == 0 {
log.Fatal("getNodes returned 0 nodes")
}
for _, host := range hosts {
rtt, err := pingHost(host)
if err != nil {
log.Printf("failed pinging node '%s' : %s", host, err)
} else {
fmt.Printf("Time: %v\n", rtt.Seconds())
histogram.WithLabelValues(hostname, host).Observe(rtt.Seconds())
}
}
time.Sleep(10 * time.Second)
}
}()
http.Handle("/metrics", promhttp.Handler())
_ = http.ListenAndServe(":2112", nil)
}
// sum(node_latency_sum)/sum(node_latency_count)
// histogram_quantile(0.99, sum(rate(node_latency_bucket{destination_node="facebook.com", origin_node="odin"}[5m])) by (le))