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

Replace command of fetching replica status for MySQL 8.4 #18

Merged
merged 8 commits into from
Oct 8, 2024
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ command = "/path/to/mackerel-plugin-mysql"

## Supported MySQL version

- `v1.1.0 >=` mysql 5.7, 8.0 and above
- `v1.3.0 >=` mysql 5.7, 8.0, 8.4 and above
- `v1.1.0 >=` mysql 5.7, 8.0
- `v1.0.0` mysql 5.0, 5.1, 5.5, 5.6, 5.7, 8.0
22 changes: 15 additions & 7 deletions lib/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,24 @@ func fetchShowVariablesBackwardCompatibile(stat map[string]float64) error {
}

// This code does not work with multi-source replication.
func (m *MySQLPlugin) fetchShowSlaveStatus(db *sql.DB, stat map[string]float64) error {
rows, err := db.Query("show slave status")
func (m *MySQLPlugin) fetchShowReplicaStatus(db *sql.DB, stat map[string]float64, version [3]int) error {
command := "show replica status"
secondsBehindSourceColumn := "Seconds_Behind_Source"
if version[0] < 8 || (version[0] == 8 && version[1] == 0 && version[2] < 22) {
// From MySQL 8.0.22, SHOW RELICA STATUS command is created and SHOW SLAVE STATUS command is deprecated.
// cf.) https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-22.html
command = "show slave status"
secondsBehindSourceColumn = "Seconds_Behind_Master"
}
rows, err := db.Query(command)
if err != nil {
return fmt.Errorf("FetchMetrics (Slave Status): %w", err)
return fmt.Errorf("FetchMetrics (Replica Status): %w", err)
}
defer rows.Close()
for rows.Next() {
columns, err := rows.ColumnTypes()
if err != nil {
return fmt.Errorf("FetchMetrics (Slave Status): %w", err)
return fmt.Errorf("FetchMetrics (Replica Status): %w", err)
}

valuePtrs := make([]interface{}, len(columns))
Expand All @@ -315,12 +323,12 @@ func (m *MySQLPlugin) fetchShowSlaveStatus(db *sql.DB, stat map[string]float64)
valuePtrs[i] = &values[i]
}
if err = rows.Scan(valuePtrs...); err != nil {
return fmt.Errorf("FetchMetrics (Slave Status): %w", err)
return fmt.Errorf("FetchMetrics (Replica Status): %w", err)
}
for i, column := range columns {
variableName := column.Name()
value := values[i]
if variableName == "Seconds_Behind_Master" {
if variableName == secondsBehindSourceColumn {
if value != nil {
f, err := atof(string(value))
if err != nil {
Expand Down Expand Up @@ -441,7 +449,7 @@ func (m *MySQLPlugin) FetchMetrics() (map[string]float64, error) {
}
}

err = m.fetchShowSlaveStatus(db, stat)
err = m.fetchShowReplicaStatus(db, stat, v)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion tests/extend-mysql8/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ docker run -d \
--name "test-$plugin" \
-p $port:3306 \
-e MYSQL_ROOT_PASSWORD=$password \
"$image" --default-authentication-plugin=mysql_native_password
"$image"
trap 'docker stop test-$plugin; docker rm test-$plugin; exit' 1 2 3 15 EXIT
sleep 10

Expand Down
Loading