/
metrics.go
140 lines (118 loc) · 3.18 KB
/
metrics.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
"github.com/koding/multiconfig"
"github.com/prometheus/client_golang/prometheus"
)
type Config struct {
Host string `default:"127.0.0.1"`
Port int `default:"12801"`
River string `default:"http://127.0.0.1:12800/stat"`
}
var (
masterBin = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "master_bin",
Help: "Master bin log file number",
})
masterPos = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "master_pos",
Help: "Master bin log pos in file",
})
riverBin = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "river_bin",
Help: "bin log file number that the river is reading",
})
riverPos = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "river_pos",
Help: "bin log pos in file that the river is reading",
})
inserts = prometheus.NewCounter(prometheus.CounterOpts{
Name: "insert_count",
Help: "insert count",
})
updates = prometheus.NewCounter(prometheus.CounterOpts{
Name: "update_count",
Help: "update count",
})
deletes = prometheus.NewCounter(prometheus.CounterOpts{
Name: "delete_count",
Help: "delete count",
})
)
func handleBodyError(err error) {
fmt.Println("error reading from native river stats output: ", err)
masterBin.Set(1.0)
riverBin.Set(0.0)
}
func readCounts(riverAddr string) {
resp, err := http.Get(riverAddr)
if err != nil {
handleBodyError(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
handleBodyError(err)
return
}
status := strings.Split(string(body), "\n")
fmt.Println(status)
// parse master bin
re := regexp.MustCompile(`server.+\(.+\.(\d+)[^\d]+(\d+)\)`)
strs := re.FindStringSubmatch(status[0])
f, _ := strconv.ParseFloat(strs[1], 64)
masterBin.Set(f)
f, _ = strconv.ParseFloat(strs[2], 64)
masterPos.Set(f)
// parse river bin
re = regexp.MustCompile(`read.+\(.+\.(\d+)[^\d]+(\d+)\)`)
strs = re.FindStringSubmatch(status[1])
f, _ = strconv.ParseFloat(strs[1], 64)
riverBin.Set(f)
f, _ = strconv.ParseFloat(strs[2], 64)
riverPos.Set(f)
re = regexp.MustCompile(`insert_num:(\d+)`)
strs = re.FindStringSubmatch(status[2])
f, _ = strconv.ParseFloat(strs[1], 64)
inserts.Set(f)
re = regexp.MustCompile(`update_num:(\d+)`)
strs = re.FindStringSubmatch(status[3])
f, _ = strconv.ParseFloat(strs[1], 64)
updates.Set(f)
re = regexp.MustCompile(`delete_num:(\d+)`)
strs = re.FindStringSubmatch(status[4])
f, _ = strconv.ParseFloat(strs[1], 64)
deletes.Set(f)
}
func proxyHandler(h http.Handler, f func()) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f()
h.ServeHTTP(w, r)
})
}
func main() {
mc := multiconfig.New()
conf := new(Config)
mc.MustLoad(conf)
prometheus.MustRegister(masterBin)
prometheus.MustRegister(masterPos)
prometheus.MustRegister(riverBin)
prometheus.MustRegister(riverPos)
prometheus.MustRegister(inserts)
prometheus.MustRegister(updates)
prometheus.MustRegister(deletes)
handler := proxyHandler(prometheus.Handler(), func() {
readCounts(conf.River)
})
http.Handle("/metrics", handler)
err := http.ListenAndServe(fmt.Sprintf("%s:%d", conf.Host, conf.Port), nil)
if err != nil {
panic(err)
}
}