@@ -47,6 +47,12 @@ var args struct {
47
47
Version bool `short:"v" long:"version" description:"Show version"`
48
48
}
49
49
50
+ var (
51
+ lastRouter * mysqlrouter.Router
52
+ lastRoutes []* mysqlrouter.Routes
53
+ lastRouteConnections []* mysqlrouter.RouteConnections
54
+ )
55
+
50
56
const (
51
57
nameSpace = "mysqlrouter"
52
58
)
@@ -127,55 +133,111 @@ func recordMetrics() {
127
133
}
128
134
129
135
func collectMetrics () {
130
- // router
131
- router , err := mysqlRouterClient .GetRouterStatus ()
136
+ router , err := collectRouterMetrics ()
132
137
if err != nil {
133
138
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
136
151
}
137
- routerUpGauge .Set (float64 (1 ))
152
+
153
+ routerUpGauge .Set (1 )
138
154
routerStatusGauge .WithLabelValues (strconv .Itoa (router .ProcessID ), router .ProductEdition , router .TimeStarted .String (), router .Version , router .Hostname )
139
155
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 ()
142
169
if err != nil {
143
170
writeError (err )
144
171
return
145
172
}
146
- for _ , metadata := range metadatas {
147
- metadataGauge .WithLabelValues (metadata .Name )
173
+
174
+ for _ , m := range metadata {
175
+ metadataGauge .WithLabelValues (m .Name )
148
176
149
177
// config
150
- metadataConfig , gmcErr := mysqlRouterClient .GetMetadataConfig (metadata .Name )
178
+ metadataConfig , gmcErr := mysqlRouterClient .GetMetadataConfig (m .Name )
151
179
if gmcErr != nil {
152
180
writeError (gmcErr )
153
181
return
154
182
}
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 )
156
184
157
185
// 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 )))
159
187
160
188
// status
161
189
if args .CollectMetadataStatus {
162
- metadataStatus , gmsErr := mysqlRouterClient .GetMetadataStatus (metadata .Name )
190
+ metadataStatus , gmsErr := mysqlRouterClient .GetMetadataStatus (m .Name )
163
191
if gmsErr != nil {
164
192
writeError (gmsErr )
165
193
return
166
194
}
167
195
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 ))
169
197
}
170
198
}
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
+ }
171
234
172
- // routes
173
235
routes , err := mysqlRouterClient .GetAllRoutes ()
174
236
if err != nil {
175
237
writeError (err )
176
238
return
177
239
}
178
-
240
+
179
241
if args .CollectRouteConnectionsByteFromServer {
180
242
routeConnectionsByteFromServerGauge .Reset ()
181
243
}
@@ -253,7 +315,9 @@ func collectMetrics() {
253
315
routeConnectionsTimeLastReceivedFromServerGauge .WithLabelValues (route .Name , router .Hostname , routeConnection .SourceAddress , routeConnection .DestinationAddress ).Set (float64 (routeConnection .TimeLastReceivedFromServer .Unix () * 1000 )) // nolint
254
316
}
255
317
}
318
+ lastRouteConnections = routeConnections
256
319
}
320
+ lastRoutes = routes
257
321
}
258
322
259
323
func writeError (err error ) {
0 commit comments