Skip to content

Commit

Permalink
Support MySQL 8.4 replicas syntax (prometheus#837)
Browse files Browse the repository at this point in the history
* support MySQL 8.4

Signed-off-by: Mitsuhiro Tanda <[email protected]>
---------

Signed-off-by: Mitsuhiro Tanda <[email protected]>
  • Loading branch information
mtanda authored and oblitorum committed Oct 15, 2024
1 parent 8a4c154 commit 4b04685
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
15 changes: 11 additions & 4 deletions collector/slave_hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ 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"
slaveHostsQuery = "SHOW SLAVE HOSTS"
showReplicasQuery = "SHOW REPLICAS"
)

// Metric descriptors.
Expand Down Expand Up @@ -63,9 +64,15 @@ 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)
if err != nil {
return err
var (
slaveHostsRows *sql.Rows
err error
)
// Try the both syntax for MySQL 8.0 and MySQL 8.4
if slaveHostsRows, err = db.QueryContext(ctx, slaveHostsQuery); err != nil {
if slaveHostsRows, err = db.QueryContext(ctx, showReplicasQuery); err != nil {
return err
}
}
defer slaveHostsRows.Close()

Expand Down
8 changes: 7 additions & 1 deletion collector/slave_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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 {
Expand Down Expand Up @@ -114,7 +114,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

Expand Down

0 comments on commit 4b04685

Please sign in to comment.