Skip to content

Commit 3bf5656

Browse files
authored
Merge pull request #43 from rluisr/fix/issue#40
set 0 on route connection, health if MySQL Router is down
2 parents 5cd8d7c + a6f3c28 commit 3bf5656

File tree

6 files changed

+115
-26
lines changed

6 files changed

+115
-26
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
name: lint
1010
runs-on: ubuntu-latest
1111
steps:
12-
- uses: actions/checkout@v3
12+
- uses: actions/checkout@v4
1313
- uses: actions/setup-go@v4
1414
with:
1515
go-version-file: 'go.mod'

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
name: Binary Release
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v3
13+
- uses: actions/checkout@v4
1414
- uses: actions/setup-go@v4
1515
with:
1616
go-version-file: 'go.mod'
@@ -27,7 +27,7 @@ jobs:
2727
runs-on: ubuntu-latest
2828
steps:
2929
- name: Checkout
30-
uses: actions/checkout@v3
30+
uses: actions/checkout@v4
3131
- name: Docker meta
3232
id: meta
3333
uses: docker/metadata-action@v4

go.mod

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ go 1.20
44

55
require (
66
github.com/jessevdk/go-flags v1.5.0
7-
github.com/prometheus/client_golang v1.15.1
8-
github.com/rluisr/mysqlrouter-go v1.1.1
7+
github.com/prometheus/client_golang v1.17.0
8+
github.com/rluisr/mysqlrouter-go v1.2.0
99
)
1010

1111
require (
1212
github.com/beorn7/perks v1.0.1 // indirect
1313
github.com/cespare/xxhash/v2 v2.2.0 // indirect
1414
github.com/golang/protobuf v1.5.3 // indirect
1515
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
16-
github.com/prometheus/client_model v0.3.0 // indirect
17-
github.com/prometheus/common v0.42.0 // indirect
18-
github.com/prometheus/procfs v0.9.0 // indirect
19-
golang.org/x/sys v0.6.0 // indirect
20-
google.golang.org/protobuf v1.30.0 // indirect
16+
github.com/prometheus/client_model v0.5.0 // indirect
17+
github.com/prometheus/common v0.44.0 // indirect
18+
github.com/prometheus/procfs v0.12.0 // indirect
19+
golang.org/x/sys v0.13.0 // indirect
20+
google.golang.org/protobuf v1.31.0 // indirect
2121
)

go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,36 @@ github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfr
1717
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1818
github.com/prometheus/client_golang v1.15.1 h1:8tXpTmJbyH5lydzFPoxSIJ0J46jdh3tylbvM1xCv0LI=
1919
github.com/prometheus/client_golang v1.15.1/go.mod h1:e9yaBhRPU2pPNsZwE+JdQl0KEt1N9XgF6zxWmaC0xOk=
20+
github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q=
21+
github.com/prometheus/client_golang v1.17.0/go.mod h1:VeL+gMmOAxkS2IqfCq0ZmHSL+LjWfWDUmp1mBz9JgUY=
2022
github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4=
2123
github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w=
24+
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
25+
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
2226
github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI1YM=
2327
github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc=
28+
github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdOOfY=
29+
github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY=
2430
github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI=
2531
github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY=
32+
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
33+
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
2634
github.com/rluisr/mysqlrouter-go v1.1.1 h1:8Rm5op3GDUNhJtVXaO7nWosoK3nr0YIIVmLSXsY/cbo=
2735
github.com/rluisr/mysqlrouter-go v1.1.1/go.mod h1:TakU6i+cdcQhI4uRiAIpMa51lV/IJiAMHGyDxeFSVuc=
36+
github.com/rluisr/mysqlrouter-go v1.2.0 h1:2E65PtqEiCgQRE14SSsi8+I6IPPrqKveFcO2JG+pHck=
37+
github.com/rluisr/mysqlrouter-go v1.2.0/go.mod h1:TakU6i+cdcQhI4uRiAIpMa51lV/IJiAMHGyDxeFSVuc=
2838
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
2939
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
3040
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3141
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
3242
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
43+
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
44+
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3345
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
3446
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
3547
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
3648
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
3749
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
50+
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
51+
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
3852
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

hack/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
If you want to run InnoDB Cluster with http and https REST API, run this command.
2+
3+
```shell
4+
$ git clone [email protected]:rluisr/mysqlrouter-go.git
5+
$ cd mysqlrouter-go/test
6+
$ docker compose up --build --force-recreate --always-recreate-deps --renew-anon-volumes -d;
7+
```
8+
9+
### Basic
10+
11+
root / mysql

main.go

Lines changed: 80 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ var args struct {
4747
Version bool `short:"v" long:"version" description:"Show version"`
4848
}
4949

50+
var (
51+
lastRouter *mysqlrouter.Router
52+
lastRoutes []*mysqlrouter.Routes
53+
lastRouteConnections []*mysqlrouter.RouteConnections
54+
)
55+
5056
const (
5157
nameSpace = "mysqlrouter"
5258
)
@@ -127,55 +133,111 @@ func recordMetrics() {
127133
}
128134

129135
func collectMetrics() {
130-
// router
131-
router, err := mysqlRouterClient.GetRouterStatus()
136+
router, err := collectRouterMetrics()
132137
if err != nil {
133138
writeError(err)
134-
routerUpGauge.Set(float64(0))
135-
return
139+
router = nil
140+
}
141+
142+
collectMetadataMetrics(router)
143+
collectRouteMetrics(router)
144+
}
145+
146+
func collectRouterMetrics() (*mysqlrouter.Router, error) {
147+
router, err := mysqlRouterClient.GetRouterStatus()
148+
if err != nil {
149+
routerUpGauge.Set(0)
150+
return nil, err
136151
}
137-
routerUpGauge.Set(float64(1))
152+
153+
routerUpGauge.Set(1)
138154
routerStatusGauge.WithLabelValues(strconv.Itoa(router.ProcessID), router.ProductEdition, router.TimeStarted.String(), router.Version, router.Hostname)
139155

140-
// metadata
141-
metadatas, err := mysqlRouterClient.GetAllMetadata()
156+
lastRouter = router
157+
158+
return router, nil
159+
}
160+
161+
func collectMetadataMetrics(router *mysqlrouter.Router) {
162+
// nil means router is down
163+
// so we don't need to collect metadata metrics
164+
if router == nil {
165+
return
166+
}
167+
168+
metadata, err := mysqlRouterClient.GetAllMetadata()
142169
if err != nil {
143170
writeError(err)
144171
return
145172
}
146-
for _, metadata := range metadatas {
147-
metadataGauge.WithLabelValues(metadata.Name)
173+
174+
for _, m := range metadata {
175+
metadataGauge.WithLabelValues(m.Name)
148176

149177
// config
150-
metadataConfig, gmcErr := mysqlRouterClient.GetMetadataConfig(metadata.Name)
178+
metadataConfig, gmcErr := mysqlRouterClient.GetMetadataConfig(m.Name)
151179
if gmcErr != nil {
152180
writeError(gmcErr)
153181
return
154182
}
155-
metadataConfigGauge.WithLabelValues(metadata.Name, metadataConfig.ClusterName, strconv.Itoa(metadataConfig.TimeRefreshInMs), metadataConfig.GroupReplicationID)
183+
metadataConfigGauge.WithLabelValues(m.Name, metadataConfig.ClusterName, strconv.Itoa(metadataConfig.TimeRefreshInMs), metadataConfig.GroupReplicationID)
156184

157185
// config nodes count
158-
metadataConfigNodesGauge.WithLabelValues(metadata.Name, router.Hostname, metadataConfig.ClusterName).Set(float64(len(metadataConfig.Nodes)))
186+
metadataConfigNodesGauge.WithLabelValues(m.Name, router.Hostname, metadataConfig.ClusterName).Set(float64(len(metadataConfig.Nodes)))
159187

160188
// status
161189
if args.CollectMetadataStatus {
162-
metadataStatus, gmsErr := mysqlRouterClient.GetMetadataStatus(metadata.Name)
190+
metadataStatus, gmsErr := mysqlRouterClient.GetMetadataStatus(m.Name)
163191
if gmsErr != nil {
164192
writeError(gmsErr)
165193
return
166194
}
167195
metadataStatusGauge.Reset()
168-
metadataStatusGauge.WithLabelValues(metadata.Name, strconv.Itoa(metadataStatus.RefreshFailed), metadataStatus.TimeLastRefreshSucceeded.String(), metadataStatus.LastRefreshHostname, strconv.Itoa(metadataStatus.LastRefreshPort))
196+
metadataStatusGauge.WithLabelValues(m.Name, strconv.Itoa(metadataStatus.RefreshFailed), metadataStatus.TimeLastRefreshSucceeded.String(), metadataStatus.LastRefreshHostname, strconv.Itoa(metadataStatus.LastRefreshPort))
169197
}
170198
}
199+
}
200+
201+
func collectRouteMetrics(router *mysqlrouter.Router) {
202+
// nil means router is down
203+
// so route metrics will be 0
204+
if router == nil {
205+
for _, route := range lastRoutes {
206+
routeActiveConnectionsGauge.WithLabelValues(route.Name, lastRouter.Hostname).Set(0)
207+
routeTotalConnectionsGauge.WithLabelValues(route.Name, lastRouter.Hostname).Set(0)
208+
routeBlockedHostsGauge.WithLabelValues(route.Name, lastRouter.Hostname).Set(0)
209+
routeHealthGauge.WithLabelValues(route.Name, lastRouter.Hostname).Set(0)
210+
211+
for _, routeConnection := range lastRouteConnections {
212+
if args.CollectRouteConnectionsByteFromServer {
213+
routeConnectionsByteFromServerGauge.WithLabelValues(route.Name, router.Hostname, routeConnection.SourceAddress, routeConnection.DestinationAddress).Set(0)
214+
}
215+
if args.CollectRouteConnectionsByteToServer {
216+
routeConnectionsByteToServerGauge.WithLabelValues(route.Name, router.Hostname, routeConnection.SourceAddress, routeConnection.DestinationAddress).Set(0)
217+
}
218+
if args.CollectRouteConnectionsTimeStarted {
219+
routeConnectionsTimeStartedGauge.WithLabelValues(route.Name, router.Hostname, routeConnection.SourceAddress, routeConnection.DestinationAddress).Set(0)
220+
}
221+
if args.CollectRouteConnectionsTimeConnectedToServer {
222+
routeConnectionsTimeConnectedToServerGauge.WithLabelValues(route.Name, router.Hostname, routeConnection.SourceAddress, routeConnection.DestinationAddress).Set(0)
223+
}
224+
if args.CollectRouteConnectionsTimeLastSentToServer {
225+
routeConnectionsTimeLastSentToServerGauge.WithLabelValues(route.Name, router.Hostname, routeConnection.SourceAddress, routeConnection.DestinationAddress).Set(0)
226+
}
227+
if args.CollectRouteConnectionsTimeReceivedFromServer {
228+
routeConnectionsTimeLastReceivedFromServerGauge.WithLabelValues(route.Name, router.Hostname, routeConnection.SourceAddress, routeConnection.DestinationAddress).Set(0)
229+
}
230+
}
231+
}
232+
return
233+
}
171234

172-
// routes
173235
routes, err := mysqlRouterClient.GetAllRoutes()
174236
if err != nil {
175237
writeError(err)
176238
return
177239
}
178-
240+
179241
if args.CollectRouteConnectionsByteFromServer {
180242
routeConnectionsByteFromServerGauge.Reset()
181243
}
@@ -253,7 +315,9 @@ func collectMetrics() {
253315
routeConnectionsTimeLastReceivedFromServerGauge.WithLabelValues(route.Name, router.Hostname, routeConnection.SourceAddress, routeConnection.DestinationAddress).Set(float64(routeConnection.TimeLastReceivedFromServer.Unix() * 1000)) // nolint
254316
}
255317
}
318+
lastRouteConnections = routeConnections
256319
}
320+
lastRoutes = routes
257321
}
258322

259323
func writeError(err error) {

0 commit comments

Comments
 (0)