Skip to content

Commit ef585c7

Browse files
committed
Add support for MariaDB GTID in slave status
1 parent 1d4c493 commit ef585c7

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

collector/slave_status.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"database/sql"
2121
"fmt"
2222
"log/slog"
23+
"strconv"
2324
"strings"
2425

2526
"github.com/prometheus/client_golang/prometheus"
@@ -139,9 +140,46 @@ func (ScrapeSlaveStatus) Scrape(ctx context.Context, instance *instance, ch chan
139140
)
140141
}
141142
}
143+
144+
ScrapeMariaDBGtid(scanArgs, slaveCols, "Gtid_IO_Pos", ch, masterHost, masterUUID, channelName, connectionName)
145+
ScrapeMariaDBGtid(scanArgs, slaveCols, "Gtid_Slave_Pos", ch, masterHost, masterUUID, channelName, connectionName)
142146
}
143147
return nil
144148
}
145149

150+
func ScrapeMariaDBGtid(scanArgs []interface{}, slaveCols []string, name string, ch chan<- prometheus.Metric, masterHost string, masterUUID string, channelName string, connectionName string) {
151+
value := columnValue(scanArgs, slaveCols, name)
152+
if value == "" {
153+
return
154+
}
155+
156+
for _, gtid := range strings.Split(value, ",") {
157+
parts := strings.Split(gtid, "-")
158+
if len(parts) != 3 {
159+
continue
160+
}
161+
162+
domainID := parts[0]
163+
serverID := parts[1]
164+
165+
sequence_num, err := strconv.ParseUint(parts[2], 10, 64)
166+
if err != nil {
167+
continue
168+
}
169+
170+
ch <- prometheus.MustNewConstMetric(
171+
prometheus.NewDesc(
172+
prometheus.BuildFQName(namespace, slaveStatus, strings.ToLower(name)),
173+
fmt.Sprintf("%s metric from SHOW SLAVE STATUS.", name),
174+
[]string{"master_host", "master_uuid", "channel_name", "connection_name", "domain_id", "server_id"},
175+
nil,
176+
),
177+
prometheus.GaugeValue,
178+
float64(sequence_num),
179+
masterHost, masterUUID, channelName, connectionName, domainID, serverID,
180+
)
181+
}
182+
}
183+
146184
// check interface
147185
var _ Scraper = ScrapeSlaveStatus{}

collector/slave_status_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ func TestScrapeSlaveStatus(t *testing.T) {
3232
defer db.Close()
3333
inst := &instance{db: db}
3434

35-
columns := []string{"Master_Host", "Read_Master_Log_Pos", "Slave_IO_Running", "Slave_SQL_Running", "Seconds_Behind_Master"}
35+
columns := []string{"Master_Host", "Read_Master_Log_Pos", "Slave_IO_Running", "Slave_SQL_Running", "Seconds_Behind_Master", "Gtid_IO_Pos"}
3636
rows := sqlmock.NewRows(columns).
37-
AddRow("127.0.0.1", "1", "Connecting", "Yes", "2")
37+
AddRow("127.0.0.1", "1", "Connecting", "Yes", "2", "0-1-2,3-4-5")
3838
mock.ExpectQuery(sanitizeQuery("SHOW SLAVE STATUS")).WillReturnRows(rows)
3939

4040
ch := make(chan prometheus.Metric)
@@ -50,6 +50,8 @@ func TestScrapeSlaveStatus(t *testing.T) {
5050
{labels: labelMap{"channel_name": "", "connection_name": "", "master_host": "127.0.0.1", "master_uuid": ""}, value: 0, metricType: dto.MetricType_UNTYPED},
5151
{labels: labelMap{"channel_name": "", "connection_name": "", "master_host": "127.0.0.1", "master_uuid": ""}, value: 1, metricType: dto.MetricType_UNTYPED},
5252
{labels: labelMap{"channel_name": "", "connection_name": "", "master_host": "127.0.0.1", "master_uuid": ""}, value: 2, metricType: dto.MetricType_UNTYPED},
53+
{labels: labelMap{"channel_name": "", "connection_name": "", "master_host": "127.0.0.1", "master_uuid": "", "domain_id": "0", "server_id": "1"}, value: 2, metricType: dto.MetricType_GAUGE},
54+
{labels: labelMap{"channel_name": "", "connection_name": "", "master_host": "127.0.0.1", "master_uuid": "", "domain_id": "3", "server_id": "4"}, value: 5, metricType: dto.MetricType_GAUGE},
5355
}
5456
convey.Convey("Metrics comparison", t, func() {
5557
for _, expect := range counterExpected {

0 commit comments

Comments
 (0)