Skip to content

Commit 5228eee

Browse files
committed
♻️ Refactor
1 parent d49dc8f commit 5228eee

File tree

2 files changed

+99
-124
lines changed

2 files changed

+99
-124
lines changed

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ go 1.12
44

55
require (
66
github.com/koron/go-dproxy v1.2.1
7-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
8-
github.com/modern-go/reflect2 v1.0.1 // indirect
97
github.com/prometheus/client_golang v1.1.0
108
github.com/rluisr/mysqlrouter-go v1.0.0
119
)

main.go

Lines changed: 99 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"flag"
5+
"fmt"
56
"log"
67
"net/http"
78
"os"
@@ -13,136 +14,108 @@ import (
1314
"github.com/rluisr/mysqlrouter-go"
1415
)
1516

16-
var nameSpace = "mysqlrouter"
17-
1817
var (
19-
routerStatusGauge = prometheus.NewGaugeVec(
20-
prometheus.GaugeOpts{
18+
nameSpace = "mysqlrouter"
19+
metricMap map[string]*prometheus.GaugeVec
20+
port = os.Getenv("MYSQLROUTER_EXPORTER_PORT")
21+
url = os.Getenv("MYSQLROUTER_EXPORTER_URL")
22+
user = os.Getenv("MYSQLROUTER_EXPORTER_USER")
23+
pass = os.Getenv("MYSQLROUTER_EXPORTER_PASS")
24+
)
25+
26+
func init() {
27+
metricMap = map[string]*prometheus.GaugeVec{
28+
"router status": prometheus.NewGaugeVec(prometheus.GaugeOpts{
2129
Namespace: nameSpace,
2230
Name: "router_status",
2331
Help: "MySQL Router information",
24-
}, []string{"process_id", "product_edition", "time_started", "version", "hostname"})
25-
metadataGauge = prometheus.NewGaugeVec(
26-
prometheus.GaugeOpts{
32+
}, []string{"process_id", "product_edition", "time_started", "version", "hostname"}),
33+
"metadata": prometheus.NewGaugeVec(prometheus.GaugeOpts{
2734
Namespace: nameSpace,
2835
Name: "metadata",
2936
Help: "metadata list",
30-
}, []string{"name"})
31-
metadataConfigGauge = prometheus.NewGaugeVec(
32-
prometheus.GaugeOpts{
37+
}, []string{"name"}),
38+
"metadata config": prometheus.NewGaugeVec(prometheus.GaugeOpts{
3339
Namespace: nameSpace,
3440
Name: "metadata_config",
3541
Help: "metadata config",
36-
}, []string{"name", "cluster_name", "time_refresh_in_ms", "group_replication_id"})
37-
metadataConfigNodeGauge = prometheus.NewGaugeVec(
38-
prometheus.GaugeOpts{
42+
}, []string{"name", "cluster_name", "time_refresh_in_ms", "group_replication_id"}),
43+
"metadata config node": prometheus.NewGaugeVec(prometheus.GaugeOpts{
3944
Namespace: nameSpace,
4045
Name: "metadata_config_node",
4146
Help: "metadata config node",
42-
}, []string{"name", "router_host", "cluster_name", "hostname", "port"})
43-
metadataStatusGauge = prometheus.NewGaugeVec(
44-
prometheus.GaugeOpts{
47+
}, []string{"name", "router_host", "cluster_name", "hostname", "port"}),
48+
"metadata status": prometheus.NewGaugeVec(prometheus.GaugeOpts{
4549
Namespace: nameSpace,
4650
Name: "metadata_status",
4751
Help: "metadata status",
48-
}, []string{"name", "refresh_failed", "time_last_refresh_succeeded", "last_refresh_hostname", "last_refresh_port"})
49-
routeGauge = prometheus.NewGaugeVec(
50-
prometheus.GaugeOpts{
52+
}, []string{"name", "refresh_failed", "time_last_refresh_succeeded", "last_refresh_hostname", "last_refresh_port"}),
53+
"route": prometheus.NewGaugeVec(prometheus.GaugeOpts{
5154
Namespace: nameSpace,
5255
Name: "route",
5356
Help: "route name",
54-
}, []string{"name"})
55-
routeActiveConnectionsGauge = prometheus.NewGaugeVec(
56-
prometheus.GaugeOpts{
57+
}, []string{"name"}),
58+
"route active connections": prometheus.NewGaugeVec(prometheus.GaugeOpts{
5759
Namespace: nameSpace,
5860
Name: "route_active_connections",
5961
Help: "route active connections",
60-
}, []string{"name", "router_hostname"})
61-
routeTotalConnectionsGauge = prometheus.NewGaugeVec(
62-
prometheus.GaugeOpts{
62+
}, []string{"name", "router_hostname"}),
63+
"route total connections": prometheus.NewGaugeVec(prometheus.GaugeOpts{
6364
Namespace: nameSpace,
6465
Name: "route_total_connections",
6566
Help: "route total connections",
66-
}, []string{"name", "router_hostname"})
67-
routeBlockedHostsGauge = prometheus.NewGaugeVec(
68-
prometheus.GaugeOpts{
67+
}, []string{"name", "router_hostname"}),
68+
"route blocked hosts": prometheus.NewGaugeVec(prometheus.GaugeOpts{
6969
Namespace: nameSpace,
7070
Name: "route_blocked_hosts",
7171
Help: "route blocked_hosts",
72-
}, []string{"name", "router_hostname"})
73-
routeHealthGauge = prometheus.NewGaugeVec(
74-
prometheus.GaugeOpts{
72+
}, []string{"name", "router_hostname"}),
73+
"route health": prometheus.NewGaugeVec(prometheus.GaugeOpts{
7574
Namespace: nameSpace,
7675
Name: "route_health",
7776
Help: "0: not active, 1: active",
78-
}, []string{"name", "router_hostname"})
79-
routeDestinationsGauge = prometheus.NewGaugeVec(
80-
prometheus.GaugeOpts{
77+
}, []string{"name", "router_hostname"}),
78+
"route destinations": prometheus.NewGaugeVec(prometheus.GaugeOpts{
8179
Namespace: nameSpace,
8280
Name: "route_destinations",
8381
Help: "",
84-
}, []string{"name", "address", "port"})
85-
routeConnectionsByteFromServerGauge = prometheus.NewGaugeVec(
86-
prometheus.GaugeOpts{
87-
Name: "route_connections_byte_from_server",
88-
Help: "Route connections byte from server",
89-
}, []string{"name", "router_hostname", "source_address", "destination_address"})
90-
routeConnectionsByteToServerGauge = prometheus.NewGaugeVec(
91-
prometheus.GaugeOpts{
92-
Name: "route_connections_byte_to_server",
93-
Help: "Route connections byte to server",
94-
}, []string{"name", "router_hostname", "source_address", "destination_address"})
95-
routeConnectionsTimeStartedGauge = prometheus.NewGaugeVec(
96-
prometheus.GaugeOpts{
97-
Name: "route_connections_time_started",
98-
Help: "Route connections time started",
99-
}, []string{"name", "router_hostname", "source_address", "destination_address"})
100-
routeConnectionsTimeConnectedToServerGauge = prometheus.NewGaugeVec(
101-
prometheus.GaugeOpts{
102-
Name: "route_connections_time_connected_to_server",
103-
Help: "Route connections time connected to server",
104-
}, []string{"name", "router_hostname", "source_address", "destination_address"})
105-
routeConnectionsTimeLastSentToServerGauge = prometheus.NewGaugeVec(
106-
prometheus.GaugeOpts{
107-
Name: "route_connections_time_last_sent_to_server",
108-
Help: "Route connections time last sent to server",
109-
}, []string{"name", "router_hostname", "source_address", "destination_address"})
110-
routeConnectionsTimeLastReceivedFromServerGauge = prometheus.NewGaugeVec(
111-
prometheus.GaugeOpts{
112-
Name: "route_connections_time_last_received_from_server",
113-
Help: "Route connections time last received from server",
114-
}, []string{"name", "router_hostname", "source_address", "destination_address"})
115-
)
116-
117-
func init() {
118-
prometheus.MustRegister(
119-
routerStatusGauge,
120-
metadataGauge,
121-
metadataConfigGauge,
122-
metadataConfigNodeGauge,
123-
metadataStatusGauge,
124-
routeGauge,
125-
routeActiveConnectionsGauge,
126-
routeTotalConnectionsGauge,
127-
routeBlockedHostsGauge,
128-
routeHealthGauge,
129-
routeDestinationsGauge,
130-
routeConnectionsByteFromServerGauge,
131-
routeConnectionsByteToServerGauge,
132-
routeConnectionsTimeStartedGauge,
133-
routeConnectionsTimeConnectedToServerGauge,
134-
routeConnectionsTimeLastSentToServerGauge,
135-
routeConnectionsTimeLastReceivedFromServerGauge,
136-
)
82+
}, []string{"name", "address", "port"}),
83+
"route connections bytes from server": prometheus.NewGaugeVec(prometheus.GaugeOpts{
84+
Namespace: nameSpace,
85+
Name: "route_connections_byte_from_server",
86+
Help: "Route connections byte from server",
87+
}, []string{"name", "router_hostname", "source_address", "destination_address"}),
88+
"route connections byte to server": prometheus.NewGaugeVec(prometheus.GaugeOpts{
89+
Namespace: nameSpace,
90+
Name: "route_connections_byte_to_server",
91+
Help: "Route connections byte to server",
92+
}, []string{"name", "router_hostname", "source_address", "destination_address"}),
93+
"route connections time started": prometheus.NewGaugeVec(prometheus.GaugeOpts{
94+
Namespace: nameSpace,
95+
Name: "route_connections_time_started",
96+
Help: "Route connections time started",
97+
}, []string{"name", "router_hostname", "source_address", "destination_address"}),
98+
"route connections time connected to server": prometheus.NewGaugeVec(prometheus.GaugeOpts{
99+
Namespace: nameSpace,
100+
Name: "route_connections_time_connected_to_server",
101+
Help: "Route connections time connected to server",
102+
}, []string{"name", "router_hostname", "source_address", "destination_address"}),
103+
"route connections time last sent to server": prometheus.NewGaugeVec(prometheus.GaugeOpts{
104+
Namespace: nameSpace,
105+
Name: "route_connections_time_last_sent_to_server",
106+
Help: "Route connections time last sent to server",
107+
}, []string{"name", "router_hostname", "source_address", "destination_address"}),
108+
"route connections time last received from server": prometheus.NewGaugeVec(prometheus.GaugeOpts{
109+
Namespace: nameSpace,
110+
Name: "route_connections_time_last_received_from_server",
111+
Help: "Route connections time last received from server",
112+
}, []string{"name", "router_hostname", "source_address", "destination_address"}),
113+
}
114+
for _, metric := range metricMap {
115+
prometheus.MustRegister(metric)
116+
}
137117
}
138118

139-
var (
140-
port = os.Getenv("MYSQLROUTER_EXPORTER_PORT")
141-
url = os.Getenv("MYSQLROUTER_EXPORTER_URL")
142-
user = os.Getenv("MYSQLROUTER_EXPORTER_USER")
143-
pass = os.Getenv("MYSQLROUTER_EXPORTER_PASS")
144-
)
145-
146119
func main() {
147120
flag.Parse()
148121

@@ -153,91 +126,91 @@ func main() {
153126

154127
mr, err := mysqlrouter.New(url, user, pass)
155128
if err != nil {
156-
panic(err)
129+
writeError(err)
157130
}
158131

159132
go func() {
160133
for {
161134
// Router
162135
router, err := mr.GetRouterStatus()
163136
if err != nil {
164-
panic(err)
137+
writeError(err)
165138
}
166-
routerStatusGauge.WithLabelValues(strconv.Itoa(router.ProcessID), router.ProductEdition, router.TimeStarted.String(), router.Version, router.Hostname)
139+
metricMap["router status"].WithLabelValues(strconv.Itoa(router.ProcessID), router.ProductEdition, router.TimeStarted.String(), router.Version, router.Hostname)
167140

168141
// Metadata
169142
metadata, err := mr.GetAllMetadata()
170143
if err != nil {
171-
panic(err)
144+
writeError(err)
172145
}
173146
for _, m := range metadata {
174-
metadataGauge.WithLabelValues(m.Name)
147+
metricMap["metadata"].WithLabelValues(m.Name)
175148

176149
// config
177150
mc, err := mr.GetMetadataConfig(m.Name)
178151
if err != nil {
179-
panic(err)
152+
writeError(err)
180153
}
181-
metadataConfigGauge.WithLabelValues(m.Name, mc.ClusterName, strconv.Itoa(mc.TimeRefreshInMs), mc.GroupReplicationID)
154+
metricMap["metadata config"].WithLabelValues(m.Name, mc.ClusterName, strconv.Itoa(mc.TimeRefreshInMs), mc.GroupReplicationID)
182155

183156
// config node
184157
for _, node := range mc.Nodes {
185-
metadataConfigNodeGauge.WithLabelValues(m.Name, router.Hostname, mc.ClusterName, node.Hostname, strconv.Itoa(node.Port))
158+
metricMap["metadata config node"].WithLabelValues(m.Name, router.Hostname, mc.ClusterName, node.Hostname, strconv.Itoa(node.Port))
186159
}
187160

188161
// status
189162
ms, err := mr.GetMetadataStatus(m.Name)
190163
if err != nil {
191-
panic(err)
164+
writeError(err)
192165
}
193-
metadataStatusGauge.WithLabelValues(m.Name, strconv.Itoa(ms.RefreshFailed), ms.TimeLastRefreshSucceeded.String(), ms.LastRefreshHostname, strconv.Itoa(ms.LastRefreshPort))
166+
metricMap["metadata status"].WithLabelValues(m.Name, strconv.Itoa(ms.RefreshFailed), ms.TimeLastRefreshSucceeded.String(), ms.LastRefreshHostname, strconv.Itoa(ms.LastRefreshPort))
194167
}
195168

196169
// Routes
197170
routes, err := mr.GetAllRoutes()
198171
if err != nil {
199-
panic(err)
172+
writeError(err)
200173
}
201174
for _, route := range routes {
202-
routeGauge.WithLabelValues(route.Name)
175+
metricMap["route"].WithLabelValues(route.Name)
203176

204177
rs, err := mr.GetRouteStatus(route.Name)
205178
if err != nil {
206-
panic(err)
179+
writeError(err)
207180
}
208-
routeActiveConnectionsGauge.WithLabelValues(route.Name, router.Hostname).Set(float64(rs.ActiveConnections))
209-
routeTotalConnectionsGauge.WithLabelValues(route.Name, router.Hostname).Set(float64(rs.TotalConnections))
210-
routeBlockedHostsGauge.WithLabelValues(route.Name, router.Hostname).Set(float64(rs.BlockedHosts))
181+
metricMap["route active connections"].WithLabelValues(route.Name, router.Hostname).Set(float64(rs.ActiveConnections))
182+
metricMap["route total connections"].WithLabelValues(route.Name, router.Hostname).Set(float64(rs.TotalConnections))
183+
metricMap["route blocked hosts"].WithLabelValues(route.Name, router.Hostname).Set(float64(rs.BlockedHosts))
211184

212185
rh, err := mr.GetRouteHealth(route.Name)
213186
if err != nil {
214-
panic(err)
187+
writeError(err)
215188
}
216189
if rh.IsAlive {
217-
routeHealthGauge.WithLabelValues(route.Name, router.Hostname).Set(float64(1))
190+
metricMap["route health"].WithLabelValues(route.Name, router.Hostname).Set(float64(1))
218191
} else {
219-
routeHealthGauge.WithLabelValues(route.Name).Set(float64(0))
192+
metricMap["route health"].WithLabelValues(route.Name).Set(float64(0))
220193
}
221194

222195
rd, err := mr.GetRouteDestinations(route.Name)
223196
if err != nil {
224-
panic(err)
197+
writeError(err)
225198
}
226199
for _, d := range rd {
227-
routeDestinationsGauge.WithLabelValues(route.Name, d.Address, strconv.Itoa(d.Port))
200+
metricMap["route destinations"].WithLabelValues(route.Name, d.Address, strconv.Itoa(d.Port))
228201
}
229202

230203
rc, err := mr.GetRouteConnections(route.Name)
231204
if err != nil {
232-
panic(err)
205+
writeError(err)
233206
}
234207
for _, c := range rc {
235-
routeConnectionsByteFromServerGauge.WithLabelValues(route.Name, router.Hostname, c.SourceAddress, c.DestinationAddress).Set(float64(c.BytesFromServer))
236-
routeConnectionsByteToServerGauge.WithLabelValues(route.Name, router.Hostname, c.SourceAddress, c.DestinationAddress).Set(float64(c.BytesToServer))
237-
routeConnectionsTimeStartedGauge.WithLabelValues(route.Name, router.Hostname, c.SourceAddress, c.DestinationAddress).Set(float64(c.TimeStarted.Unix() * 1000))
238-
routeConnectionsTimeConnectedToServerGauge.WithLabelValues(route.Name, router.Hostname, c.SourceAddress, c.DestinationAddress).Set(float64(c.TimeConnectedToServer.Unix() * 1000))
239-
routeConnectionsTimeLastSentToServerGauge.WithLabelValues(route.Name, router.Hostname, c.SourceAddress, c.DestinationAddress).Set(float64(c.TimeLastSentToServer.Unix() * 1000))
240-
routeConnectionsTimeLastReceivedFromServerGauge.WithLabelValues(route.Name, router.Hostname, c.SourceAddress, c.DestinationAddress).Set(float64(c.TimeLastReceivedFromServer.Unix() * 1000))
208+
metricMap["route connections bytes from server"].WithLabelValues(route.Name, router.Hostname, c.SourceAddress, c.DestinationAddress).Set(float64(c.BytesFromServer))
209+
metricMap["route connections byte to server"].WithLabelValues(route.Name, router.Hostname, c.SourceAddress, c.DestinationAddress).Set(float64(c.BytesToServer))
210+
metricMap["route connections time started"].WithLabelValues(route.Name, router.Hostname, c.SourceAddress, c.DestinationAddress).Set(float64(c.TimeStarted.Unix() * 1000))
211+
metricMap["route connections time connected to server"].WithLabelValues(route.Name, router.Hostname, c.SourceAddress, c.DestinationAddress).Set(float64(c.TimeConnectedToServer.Unix() * 1000))
212+
metricMap["route connections time last sent to server"].WithLabelValues(route.Name, router.Hostname, c.SourceAddress, c.DestinationAddress).Set(float64(c.TimeLastSentToServer.Unix() * 1000))
213+
metricMap["route connections time last received from server"].WithLabelValues(route.Name, router.Hostname, c.SourceAddress, c.DestinationAddress).Set(float64(c.TimeLastReceivedFromServer.Unix() * 1000))
241214
}
242215
}
243216
time.Sleep(60 * time.Second)
@@ -252,3 +225,7 @@ func main() {
252225
http.Handle("/metrics", promhttp.Handler())
253226
log.Fatal(http.ListenAndServe("0.0.0.0:"+port, nil))
254227
}
228+
229+
func writeError(err error) {
230+
_, _ = fmt.Fprintf(os.Stderr, "[mysqlrouter_exporter ERROR] %v\n", err)
231+
}

0 commit comments

Comments
 (0)