From 812dc66065b633adb222b359eca17b2dd0b92191 Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Tue, 14 May 2024 19:02:21 +0900 Subject: [PATCH 1/2] support MySQL 8.4 Signed-off-by: Mitsuhiro Tanda --- collector/slave_hosts.go | 16 +++++++++++++--- collector/slave_status.go | 8 +++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/collector/slave_hosts.go b/collector/slave_hosts.go index d473c3cd3..8129ff7d5 100644 --- a/collector/slave_hosts.go +++ b/collector/slave_hosts.go @@ -31,12 +31,12 @@ const ( // timestamps. %s will be replaced by the database and table name. // The second column allows gets the server timestamp at the exact same // time the query is run. - slaveHostsQuery = "SHOW SLAVE HOSTS" ) // Metric descriptors. var ( - SlaveHostsInfo = prometheus.NewDesc( + slaveHostsQueries = [2]string{"SHOW SLAVE HOSTS", "SHOW REPLICAS"} + SlaveHostsInfo = prometheus.NewDesc( prometheus.BuildFQName(namespace, heartbeat, "mysql_slave_hosts_info"), "Information about running slaves", []string{"server_id", "slave_host", "port", "master_id", "slave_uuid"}, nil, @@ -63,7 +63,17 @@ func (ScrapeSlaveHosts) Version() float64 { // Scrape collects data from database connection and sends it over channel as prometheus metric. func (ScrapeSlaveHosts) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric, logger log.Logger) error { - slaveHostsRows, err := db.QueryContext(ctx, slaveHostsQuery) + var ( + slaveHostsRows *sql.Rows + err error + ) + // Try the both syntax for MySQL 8.0 and MySQL 8.4 + for _, query := range slaveHostsQueries { + slaveHostsRows, err = db.QueryContext(ctx, query) + if err == nil { + break + } + } if err != nil { return err } diff --git a/collector/slave_status.go b/collector/slave_status.go index 36dda335c..b7984657f 100644 --- a/collector/slave_status.go +++ b/collector/slave_status.go @@ -30,7 +30,7 @@ const ( slaveStatus = "slave_status" ) -var slaveStatusQueries = [2]string{"SHOW ALL SLAVES STATUS", "SHOW SLAVE STATUS"} +var slaveStatusQueries = [3]string{"SHOW ALL SLAVES STATUS", "SHOW SLAVE STATUS", "SHOW REPLICA STATUS"} var slaveStatusQuerySuffixes = [3]string{" NONBLOCKING", " NOLOCK", ""} func columnIndex(slaveCols []string, colName string) int { @@ -113,7 +113,13 @@ func (ScrapeSlaveStatus) Scrape(ctx context.Context, db *sql.DB, ch chan<- prome } masterUUID := columnValue(scanArgs, slaveCols, "Master_UUID") + if masterUUID == "" { + masterUUID = columnValue(scanArgs, slaveCols, "Source_UUID") + } masterHost := columnValue(scanArgs, slaveCols, "Master_Host") + if masterHost == "" { + masterHost = columnValue(scanArgs, slaveCols, "Source_Host") + } channelName := columnValue(scanArgs, slaveCols, "Channel_Name") // MySQL & Percona connectionName := columnValue(scanArgs, slaveCols, "Connection_name") // MariaDB From e0626d145495ca222f32ebcbd28736edb2e44c80 Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Wed, 15 May 2024 16:17:40 +0900 Subject: [PATCH 2/2] Fix: Addressed PR review comments Signed-off-by: Mitsuhiro Tanda --- collector/slave_hosts.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/collector/slave_hosts.go b/collector/slave_hosts.go index 8129ff7d5..b95110e13 100644 --- a/collector/slave_hosts.go +++ b/collector/slave_hosts.go @@ -31,12 +31,13 @@ const ( // timestamps. %s will be replaced by the database and table name. // The second column allows gets the server timestamp at the exact same // time the query is run. + slaveHostsQuery = "SHOW SLAVE HOSTS" + showReplicasQuery = "SHOW REPLICAS" ) // Metric descriptors. var ( - slaveHostsQueries = [2]string{"SHOW SLAVE HOSTS", "SHOW REPLICAS"} - SlaveHostsInfo = prometheus.NewDesc( + SlaveHostsInfo = prometheus.NewDesc( prometheus.BuildFQName(namespace, heartbeat, "mysql_slave_hosts_info"), "Information about running slaves", []string{"server_id", "slave_host", "port", "master_id", "slave_uuid"}, nil, @@ -68,15 +69,11 @@ func (ScrapeSlaveHosts) Scrape(ctx context.Context, db *sql.DB, ch chan<- promet err error ) // Try the both syntax for MySQL 8.0 and MySQL 8.4 - for _, query := range slaveHostsQueries { - slaveHostsRows, err = db.QueryContext(ctx, query) - if err == nil { - break + if slaveHostsRows, err = db.QueryContext(ctx, slaveHostsQuery); err != nil { + if slaveHostsRows, err = db.QueryContext(ctx, showReplicasQuery); err != nil { + return err } } - if err != nil { - return err - } defer slaveHostsRows.Close() // fields of row