Skip to content

Commit

Permalink
feat: add repl_connect_status to pika_exporter (#2961)
Browse files Browse the repository at this point in the history
* add repl_connect_status to pika_exporter
  • Loading branch information
cheniujh authored Dec 11, 2024
1 parent bc46bad commit 7fcb916
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tools/pika_exporter/exporter/metrics/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,52 @@ const (
defaultValue = 0
)

type statusToGaugeParser struct {
statusMapping map[string]int
}

func (p *statusToGaugeParser) Parse(m MetricMeta, c Collector, opt ParseOption) {
m.Lookup(func(m MetaData) {
metric := Metric{
MetaData: m,
LabelValues: make([]string, len(m.Labels)),
Value: defaultValue,
}

for i, labelName := range m.Labels {
labelValue, ok := findInMap(labelName, opt.Extracts)
if !ok {
log.Debugf("statusToGaugeParser::Parse not found label value. metricName:%s labelName:%s",
m.Name, labelName)
}

metric.LabelValues[i] = labelValue
}

if m.ValueName != "" {
if v, ok := findInMap(m.ValueName, opt.Extracts); !ok {
log.Warnf("statusToGaugeParser::Parse not found value. metricName:%s valueName:%s", m.Name, m.ValueName)
return
} else {
mappedValue, exists := p.statusMapping[v]
if !exists {
log.Warnf("statusToGaugeParser::Parse unknown status value. metricName:%s valueName:%s rawValue:%s",
m.Name, m.ValueName, v)
mappedValue = defaultValue
}
metric.Value = float64(mappedValue)
}
}

if err := c.Collect(metric); err != nil {
log.Errorf("statusToGaugeParser::Parse metric collect failed. metric:%#v err:%s",
m, m.ValueName)
}
})
}



type ParseOption struct {
Version *semver.Version
Extracts map[string]string
Expand Down
31 changes: 31 additions & 0 deletions tools/pika_exporter/exporter/metrics/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,37 @@ var collectReplicationMetrics = map[string]MetricConfig{
},
},

"slave_info>=3.5.5_or_4.0.0": {
Parser: &keyMatchParser{
matchers: map[string]Matcher{
"role": &equalMatcher{v: "slave"},
},
Parser: &regexParser{
name: "repl_connect_status",
reg: regexp.MustCompile(`(?m)^\s*(?P<db_name>db\d+)\s*:\s*(?P<status>\w+)\s*$`),
Parser: &statusToGaugeParser{
statusMapping: map[string]int{
"no_connect": 0,
"try_to_incr_sync": 1,
"try_to_full_sync": 2,
"syncing_full": 3,
"connecting": 4,
"connected": 5,
"error": -1,
},
},
},
},
MetricMeta: &MetaData{
Name: "repl_connect_status",
Help: "Replication connection status for each database on the slave node",
Type: metricTypeGauge,
Labels: []string{LabelNameAddr, LabelNameAlias, "db_name"},
ValueName: "status",
},
},


"slave_info<3.2.0": {
Parser: &keyMatchParser{
matchers: map[string]Matcher{
Expand Down

0 comments on commit 7fcb916

Please sign in to comment.