Skip to content

Commit

Permalink
Merge pull request #125 from prometheus/superq/slave_connecting
Browse files Browse the repository at this point in the history
Fix SLAVE STATUS "Connecting"
  • Loading branch information
SuperQ committed May 5, 2016
2 parents c67791b + 5bd3537 commit 8cd6e88
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func parseStatus(data sql.RawBytes) (float64, bool) {
if bytes.Compare(data, []byte("No")) == 0 || bytes.Compare(data, []byte("OFF")) == 0 {
return 0, true
}
// SHOW SLAVE STATUS Slave_IO_Running can return "Connecting" which is a non-running state.
if bytes.Compare(data, []byte("Connecting")) == 0 {
return 0, true
}
if logNum := logRE.Find(data); logNum != nil {
value, err := strconv.ParseFloat(string(logNum), 64)
return value, err == nil
Expand Down
49 changes: 49 additions & 0 deletions collector/slave_status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package collector

import (
"testing"

"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/smartystreets/goconvey/convey"
"gopkg.in/DATA-DOG/go-sqlmock.v1"
)

func TestScrapeSlaveStatus(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("error opening a stub database connection: %s", err)
}
defer db.Close()

columns := []string{"Master_Host", "Read_Master_Log_Pos", "Slave_IO_Running", "Slave_SQL_Running", "Seconds_Behind_Master"}
rows := sqlmock.NewRows(columns).
AddRow("127.0.0.1", "1", "Connecting", "Yes", "2")
mock.ExpectQuery(sanitizeQuery(slaveStatusQuery)).WillReturnRows(rows)

ch := make(chan prometheus.Metric)
go func() {
if err = ScrapeSlaveStatus(db, ch); err != nil {
t.Errorf("error calling function on test: %s", err)
}
close(ch)
}()

counterExpected := []MetricResult{
{labels: labelMap{}, value: 1, metricType: dto.MetricType_UNTYPED},
{labels: labelMap{}, value: 0, metricType: dto.MetricType_UNTYPED},
{labels: labelMap{}, value: 1, metricType: dto.MetricType_UNTYPED},
{labels: labelMap{}, value: 2, metricType: dto.MetricType_UNTYPED},
}
convey.Convey("Metrics comparison", t, func() {
for _, expect := range counterExpected {
got := readMetric(<-ch)
convey.So(got, convey.ShouldResemble, expect)
}
})

// Ensure all SQL queries were executed
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expections: %s", err)
}
}

0 comments on commit 8cd6e88

Please sign in to comment.