-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathprom.go
106 lines (92 loc) · 2.68 KB
/
prom.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
package main
import (
"flag"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
listenAddress = flag.String("web.listen-address", "[::]:9523", "Address on which to expose metrics and web interface")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
)
func handlePrometheus() {
prometheus.MustRegister(Collector{})
handler := promhttp.HandlerFor(prometheus.DefaultGatherer,
promhttp.HandlerOpts{})
http.Handle(*metricsPath, handler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if *metricsPath == "/metrics" {
w.Write([]byte(`<html>
<head><title>split-ping</title></head>
<body>
<h1>split-ping</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
} else {
// Let's not expose a URL that has been custom set, just in case people are
// trying security by obscurity
w.Write([]byte(`<html>
<head><title>split-ping</title></head>
<body>
<h1>split-ping</h1>
</body>
</html>`))
}
})
log.Print("Listening on", *listenAddress)
if err := http.ListenAndServe(*listenAddress, nil); err != nil {
log.Fatal(err)
}
}
//Collector implements the prometheus.Collector interface.
type Collector struct{}
//Describe implements the prometheus.Collector interface.
func (c Collector) Describe(ch chan<- *prometheus.Desc) {
promLatency.Describe(ch)
promLoss.Describe(ch)
}
//Collect implements the prometheus.Collector interface.
func (c Collector) Collect(ch chan<- prometheus.Metric) {
err := c.measure()
//only report data when measurement was successful
if err == nil {
promLatency.Collect(ch)
promLoss.Collect(ch)
} else {
log.Println("ERROR:", err)
return
}
}
var (
promLatency = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "splitping_latency",
Help: "The latency (in s) in each direction",
},
[]string{"direction", "host"},
)
promLoss = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "splitping_loss",
Help: "The loss in (in persent) each direction",
},
[]string{"direction", "host"},
)
)
func (c Collector) measure() error {
sessionLock.Lock()
for _, v := range sessionMap {
RXL, TXL, RXLoss, TXLoss, exchanges := getStats(v.LastRX, v.LastRXPing, v)
PeerAddr := v.PeerAddress.String()
promLatency.WithLabelValues("rx", PeerAddr).Set(float64(RXL.Seconds()))
promLatency.WithLabelValues("tx", PeerAddr).Set(float64(TXL.Seconds()))
if exchanges == 32 {
promLoss.WithLabelValues("rx", PeerAddr).Set(float64(RXLoss) / 32)
promLoss.WithLabelValues("tx", PeerAddr).Set(float64(TXLoss) / 32)
}
}
sessionLock.Unlock()
return nil
}