Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support MySQL 8.4 replicas syntax #837

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions collector/slave_hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
mtanda marked this conversation as resolved.
Show resolved Hide resolved
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,
Expand All @@ -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 {
mtanda marked this conversation as resolved.
Show resolved Hide resolved
slaveHostsRows, err = db.QueryContext(ctx, query)
if err == nil {
break
}
}
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion collector/slave_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down