-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (105 loc) · 3.17 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
package main
import (
"flag"
"os"
"bufio"
"strings"
"net/http"
"log"
"strconv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type hindsight struct {
filename string
metrics map[string]hindsightMetric
}
type hindsightMetric struct {
desc *prometheus.Desc
valType prometheus.ValueType
index int
}
var (
listenAddress = flag.String("web.listen-address", ":9121", "Address to listen on for web interface and telemetry.")
metricPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
hindsightStatsPath = flag.String("hindsight", "/hindsight.tsv", "hindsight.tsv file location.")
)
func newHindsightExporter(file string) *hindsight {
e := hindsight{
filename: file,
metrics: map[string]hindsightMetric{
"hindsight_injected_message_count": {
desc: prometheus.NewDesc("hindsight_injected_message_count", "Number of injected messages", []string{"plugin"}, nil),
valType: prometheus.GaugeValue,
index: 1,
},
"hindsight_injected_message_bytes": {
desc: prometheus.NewDesc("hindsight_injected_message_bytes", "Number of injected bytes", []string{"plugin"}, nil),
valType: prometheus.GaugeValue,
index: 2,
},
"hindsight_process_message_count": {
desc: prometheus.NewDesc("hindsight_process_message_count", "Number of processed messages", []string{"plugin"}, nil),
valType: prometheus.GaugeValue,
index:3,
},
"hindsight_process_message_failures": {
desc: prometheus.NewDesc("hindsight_process_message_failures", "Number of processed failure messages", []string{"plugin"}, nil),
valType: prometheus.GaugeValue,
index: 4,
},
},
}
return &e
}
func (h *hindsight) Describe(ch chan<- *prometheus.Desc) {
for _, i := range h.metrics {
ch <- i.desc
}
}
func (h *hindsight) Collect(ch chan<- prometheus.Metric) {
h.fetchHindsightStatistcs(ch)
}
func parseFloatOrZero(s string) float64 {
res, err := strconv.ParseFloat(s, 64)
if err != nil {
log.Printf("Cannot parse %s\n", s)
return 0.0
}
return res
}
func main() {
flag.Parse()
e := newHindsightExporter(*hindsightStatsPath)
prometheus.MustRegister(e)
http.Handle(*metricPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Warp10 exporter</title></head>
<body>
<h1>Hindsight exporter</h1>
<p><a href='` + *metricPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Printf("providing metrics at %s%s", *listenAddress, *metricPath)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}
func (h *hindsight) fetchHindsightStatistcs(ch chan<- prometheus.Metric) {
file, err := os.Open(h.filename)
defer file.Close()
if err == nil {
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
scanner.Scan()
scanner.Text()
for scanner.Scan() {
values := strings.Fields(scanner.Text())
for _, e := range h.metrics {
ch <- prometheus.MustNewConstMetric(e.desc, e.valType, parseFloatOrZero(values[e.index]), values[0])
}
}
} else {
log.Printf("Cannot output %s hindsight statistics", h.filename)
}
}