Skip to content

Commit

Permalink
feat(inputs.mysql): Add support for replica status (influxdata#15749)
Browse files Browse the repository at this point in the history
  • Loading branch information
hungnt1 authored and asaharn committed Oct 16, 2024
1 parent 9988e27 commit f82d06c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
7 changes: 6 additions & 1 deletion plugins/inputs/mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
## gather metrics from SHOW SLAVE STATUS command output
# gather_slave_status = false

## gather metrics from SHOW REPLICA STATUS command output
# gather_replica_status = false

## use SHOW ALL SLAVES STATUS command output for MariaDB
## use SHOW ALL REPLICAS STATUS command if enable gather replica status
# mariadb_dialect = false

## gather metrics from SHOW BINARY LOGS command output
Expand Down Expand Up @@ -260,7 +264,8 @@ the single-source replication is on. If the multi-source replication is set,
then everything works differently, this metric does not work with multi-source
replication, unless you set `gather_all_slave_channels = true`. For MariaDB,
`mariadb_dialect = true` should be set to address the field names and commands
differences.
differences. If enable `gather_replica_status` metrics gather from command
`SHOW REPLICA STATUS`, for MariaDB will be `SHOW ALL REPLICAS STATUS`
* slave_[column name]
* Binary logs - all metrics including size and count of all binary files.
Requires to be turned on in configuration.
Expand Down
15 changes: 11 additions & 4 deletions plugins/inputs/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Mysql struct {
GatherInfoSchemaAutoInc bool `toml:"gather_info_schema_auto_inc"`
GatherInnoDBMetrics bool `toml:"gather_innodb_metrics"`
GatherSlaveStatus bool `toml:"gather_slave_status"`
GatherReplicaStatus bool `toml:"gather_replica_status"`
GatherAllSlaveChannels bool `toml:"gather_all_slave_channels"`
MariadbDialect bool `toml:"mariadb_dialect"`
GatherBinaryLogs bool `toml:"gather_binary_logs"`
Expand Down Expand Up @@ -77,12 +78,16 @@ func (*Mysql) SampleConfig() string {
}

func (m *Mysql) Init() error {
if m.MariadbDialect {
switch {
case m.MariadbDialect && m.GatherReplicaStatus:
m.getStatusQuery = replicaStatusQueryMariadb
case m.MariadbDialect:
m.getStatusQuery = slaveStatusQueryMariadb
} else {
case m.GatherReplicaStatus:
m.getStatusQuery = replicaStatusQuery
default:
m.getStatusQuery = slaveStatusQuery
}

// Default to localhost if nothing specified.
if len(m.Servers) == 0 {
s := config.NewSecret([]byte(localhost))
Expand Down Expand Up @@ -250,7 +255,9 @@ const (
globalStatusQuery = `SHOW GLOBAL STATUS`
globalVariablesQuery = `SHOW GLOBAL VARIABLES`
slaveStatusQuery = `SHOW SLAVE STATUS`
replicaStatusQuery = `SHOW REPLICA STATUS`
slaveStatusQueryMariadb = `SHOW ALL SLAVES STATUS`
replicaStatusQueryMariadb = `SHOW ALL REPLICAS STATUS`
binaryLogsQuery = `SHOW BINARY LOGS`
infoSchemaProcessListQuery = `
SELECT COALESCE(command,''),COALESCE(state,''),count(*)
Expand Down Expand Up @@ -475,7 +482,7 @@ func (m *Mysql) gatherServer(server *config.Secret, acc telegraf.Accumulator) er
}
}

if m.GatherSlaveStatus {
if m.GatherSlaveStatus || m.GatherReplicaStatus {
err = m.gatherSlaveStatuses(db, servtag, acc)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions plugins/inputs/mysql/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
## gather metrics from SHOW SLAVE STATUS command output
# gather_slave_status = false

## gather metrics from SHOW REPLICA STATUS command output
# gather_replica_status = false

## use SHOW ALL SLAVES STATUS command output for MariaDB
## use SHOW ALL REPLICAS STATUS command if enable gather replica status
# mariadb_dialect = false

## gather metrics from SHOW BINARY LOGS command output
Expand Down

0 comments on commit f82d06c

Please sign in to comment.