Skip to content

Commit

Permalink
keydb multimaster support (#710)
Browse files Browse the repository at this point in the history
Co-authored-by: Mihai Craiu <mihai.craiu@1and1.ro>
  • Loading branch information
mihaiush and Mihai Craiu authored Oct 28, 2022
1 parent bfaebc4 commit ec7ec0f
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions exporter/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ import (
log "github.com/sirupsen/logrus"
)

// precompiled regexps

// keydb multimaster
/*
master_host:kdb0.server.local
master_port:6377
master_1_host:kdb1.server.local
master_1_port:6377
*/
var reMasterHost = regexp.MustCompile(`^master(_[0-9]+)?_host`)
var reMasterPort = regexp.MustCompile(`^master(_[0-9]+)?_port`)
var reMasterLinkStatus = regexp.MustCompile(`^master(_[0-9]+)?_link_status`)

// info fieldKey:fieldValue -> metric redis_fieldKey{master_host, master_port} fieldValue
var reMasterDirect = regexp.MustCompile(`^(master(_[0-9]+)?_(last_io_seconds_ago|sync_in_progress)|slave_repl_offset)`)

// numbered slaves
/*
slave0:ip=10.254.11.1,port=6379,state=online,offset=1751844676,lag=0
slave1:ip=10.254.11.2,port=6379,state=online,offset=1751844222,lag=0
*/
var reSlave = regexp.MustCompile(`^slave\d+`)

func extractVal(s string) (val float64, err error) {
split := strings.Split(s, "=")
if len(split) != 2 {
Expand Down Expand Up @@ -67,11 +90,11 @@ func (e *Exporter) extractInfoMetrics(ch chan<- prometheus.Metric, info string,

keyValues[fieldKey] = fieldValue

if fieldKey == "master_host" {
if reMasterHost.MatchString(fieldKey) {
masterHost = fieldValue
}

if fieldKey == "master_port" {
if reMasterPort.MatchString(fieldKey) {
masterPort = fieldValue
}

Expand Down Expand Up @@ -240,7 +263,7 @@ func parseDBKeyspaceString(inputKey string, inputVal string) (keysTotal float64,
*/
func parseConnectedSlaveString(slaveName string, keyValues string) (offset float64, ip string, port string, state string, lag float64, ok bool) {
ok = false
if matched, _ := regexp.MatchString(`^slave\d+`, slaveName); !matched {
if !reSlave.MatchString(slaveName) {
return
}
connectedkeyValues := make(map[string]string)
Expand Down Expand Up @@ -279,17 +302,21 @@ func parseConnectedSlaveString(slaveName string, keyValues string) (offset float

func (e *Exporter) handleMetricsReplication(ch chan<- prometheus.Metric, masterHost string, masterPort string, fieldKey string, fieldValue string) bool {
// only slaves have this field
if fieldKey == "master_link_status" {
if reMasterLinkStatus.MatchString(fieldKey) {
if fieldValue == "up" {
e.registerConstMetricGauge(ch, "master_link_up", 1, masterHost, masterPort)
} else {
e.registerConstMetricGauge(ch, "master_link_up", 0, masterHost, masterPort)
}
return true
}
switch fieldKey {

case "master_last_io_seconds_ago", "slave_repl_offset", "master_sync_in_progress":
if reMasterDirect.MatchString(fieldKey) {
if strings.HasSuffix(fieldKey, "last_io_seconds_ago") {
fieldKey = "master_last_io_seconds_ago"
} else if strings.HasSuffix(fieldKey, "sync_in_progress") {
fieldKey = "master_sync_in_progress"
}
val, _ := strconv.Atoi(fieldValue)
e.registerConstMetricGauge(ch, fieldKey, float64(val), masterHost, masterPort)
return true
Expand Down

0 comments on commit ec7ec0f

Please sign in to comment.