Created
January 10, 2020 04:21
-
-
Save aloucks/070cd01fd4c3286a064f052abe6b8820 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/collector/slave_status.go b/collector/slave_status.go | |
index e343258..1fea965 100644 | |
--- a/collector/slave_status.go | |
+++ b/collector/slave_status.go | |
@@ -18,7 +18,6 @@ package collector | |
import ( | |
"context" | |
"database/sql" | |
- "fmt" | |
"strings" | |
"github.com/go-kit/kit/log" | |
@@ -30,8 +29,16 @@ const ( | |
slaveStatus = "slave_status" | |
) | |
-var slaveStatusQueries = [2]string{"SHOW ALL SLAVES STATUS", "SHOW SLAVE STATUS"} | |
-var slaveStatusQuerySuffixes = [3]string{" NONBLOCKING", " NOLOCK", ""} | |
+var slaveStatusQueries = [4]string{ | |
+ // Only valid for Percona | |
+ "SHOW SLAVE STATUS NONBLOCKING", | |
+ "SHOW SLAVE STATUS NOLOCK", | |
+ // Only valid for MariaDB | |
+ "SHOW ALL SLAVE STATUS", | |
+ // Valid for all | |
+ "SHOW SLAVE STATUS", | |
+} | |
+var slaveStatusQueryIndex = len(slaveStatusQueries) | |
func columnIndex(slaveCols []string, colName string) int { | |
for idx := range slaveCols { | |
@@ -74,19 +81,18 @@ func (ScrapeSlaveStatus) Scrape(ctx context.Context, db *sql.DB, ch chan<- prome | |
slaveStatusRows *sql.Rows | |
err error | |
) | |
- // Try the both syntax for MySQL/Percona and MariaDB | |
- for _, query := range slaveStatusQueries { | |
+ // Try the syntax for Percona, MariaDB, and MySQL | |
+ // and cache the index of the successful query. | |
+ if slaveStatusQueryIndex < len(slaveStatusQueries) { | |
+ query := slaveStatusQueries[slaveStatusQueryIndex] | |
slaveStatusRows, err = db.QueryContext(ctx, query) | |
- if err != nil { // MySQL/Percona | |
- // Leverage lock-free SHOW SLAVE STATUS by guessing the right suffix | |
- for _, suffix := range slaveStatusQuerySuffixes { | |
- slaveStatusRows, err = db.QueryContext(ctx, fmt.Sprint(query, suffix)) | |
- if err == nil { | |
- break | |
- } | |
+ } else { | |
+ for i, query := range slaveStatusQueries { | |
+ slaveStatusRows, err = db.QueryContext(ctx, query) | |
+ if err == nil { | |
+ slaveStatusQueryIndex = i | |
+ break | |
} | |
- } else { // MariaDB | |
- break | |
} | |
} | |
if err != nil { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment