Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
feat(redis): collect master slave link metrics (#851)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyam8 authored Sep 23, 2022
1 parent 770bc95 commit 2fb5298
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
2 changes: 2 additions & 0 deletions modules/redis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ It collects information and statistics about the server executing the following
### Replication

- Connected replicas in `replicas`
- Time elapsed since the last interaction with master in `seconds`
- Time elapsed since the link between master and slave is down in `seconds`

### Persistence

Expand Down
24 changes: 24 additions & 0 deletions modules/redis/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const (
prioNet

prioConnectedReplicas
prioMasterLastIOSinceTime
prioMasterLinkDownSinceTime

prioPersistenceRDBChanges
prioPersistenceRDBBgSaveNow
Expand Down Expand Up @@ -329,6 +331,28 @@ var (
{ID: "connected_slaves", Name: "connected"},
},
}
masterLastIOSinceTimeChart = module.Chart{
ID: "master_last_io_since_time",
Title: "Time elapsed since the last interaction with master",
Units: "seconds",
Fam: "replication",
Ctx: "redis.master_last_io_since_time",
Priority: prioMasterLastIOSinceTime,
Dims: module.Dims{
{ID: "master_last_io_seconds_ago", Name: "time"},
},
}
masterLinkDownSinceTimeChart = module.Chart{
ID: "master_link_down_since_stime",
Title: "Time elapsed since the link between master and slave is down",
Units: "seconds",
Fam: "replication",
Ctx: "redis.master_link_down_since_time",
Priority: prioMasterLinkDownSinceTime,
Dims: module.Dims{
{ID: "master_link_down_since_seconds", Name: "time"},
},
}
)

var (
Expand Down
15 changes: 15 additions & 0 deletions modules/redis/collect_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ func (r *Redis) collectInfo(mx map[string]int64, info string) {
if has(mx, "keyspace_hits", "keyspace_misses") {
mx["keyspace_hit_rate"] = int64(calcKeyspaceHitRate(mx) * precision)
}
if has(mx, "master_last_io_seconds_ago") {
r.addReplSlaveChartsOnce.Do(r.addReplSlaveCharts)
if !has(mx, "master_link_down_since_seconds") {
mx["master_link_down_since_seconds"] = 0
}
}
}

var reKeyspaceValue = regexp.MustCompile(`^keys=(\d+),expires=(\d+)`)
Expand Down Expand Up @@ -220,6 +226,15 @@ func (r *Redis) addAOFCharts() {
}
}

func (r *Redis) addReplSlaveCharts() {
if err := r.Charts().Add(masterLastIOSinceTimeChart.Copy()); err != nil {
r.Warningf("error on adding '%s' chart", masterLastIOSinceTimeChart.ID)
}
if err := r.Charts().Add(masterLinkDownSinceTimeChart.Copy()); err != nil {
r.Warningf("error on adding '%s' chart", masterLinkDownSinceTimeChart.ID)
}
}

func has(m map[string]int64, key string, keys ...string) bool {
switch _, ok := m[key]; len(keys) {
case 0:
Expand Down
12 changes: 7 additions & 5 deletions modules/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ func New() *Redis {
PingSamples: 5,
},

addAOFChartsOnce: &sync.Once{},
pingSummary: metrics.NewSummary(),
collectedCommands: make(map[string]bool),
collectedDbs: make(map[string]bool),
addAOFChartsOnce: &sync.Once{},
addReplSlaveChartsOnce: &sync.Once{},
pingSummary: metrics.NewSummary(),
collectedCommands: make(map[string]bool),
collectedDbs: make(map[string]bool),
}
}

Expand All @@ -56,7 +57,8 @@ type (
server string
version *semver.Version

addAOFChartsOnce *sync.Once
addAOFChartsOnce *sync.Once
addReplSlaveChartsOnce *sync.Once

pingSummary metrics.Summary

Expand Down

0 comments on commit 2fb5298

Please sign in to comment.