Skip to content

Commit

Permalink
Add separate ro and rw slave counters
Browse files Browse the repository at this point in the history
  • Loading branch information
roivaz committed Oct 4, 2023
1 parent 7ec9bcc commit 9e4509f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
34 changes: 32 additions & 2 deletions pkg/redis/metrics/cluster_status_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metrics
import (
"context"

"github.com/3scale/saas-operator/pkg/redis/client"
"github.com/3scale/saas-operator/pkg/redis/sharded"
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/metrics"
Expand All @@ -13,14 +14,30 @@ var (
prometheus.GaugeOpts{
Name: "server_info",
Namespace: "saas_redis_cluster_status",
Help: `"redis server info"`,
Help: "redis cluster member info",
},
[]string{"resource", "shard", "redis_server_host", "redis_server_alias", "role", "read_only"})
roSlaveCount = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "ro_slave_count",
Namespace: "saas_redis_cluster_status",
Help: "read-only slave count",
},
[]string{"resource", "shard"},
)
rwSlaveCount = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "rw_slave_count",
Namespace: "saas_redis_cluster_status",
Help: "read-write slave count",
},
[]string{"resource", "shard"},
)
)

func init() {
// Register custom metrics with the global prometheus registry
metrics.Registry.MustRegister(serverInfo)
metrics.Registry.MustRegister(serverInfo, roSlaveCount, rwSlaveCount)
}

func FromShardedCluster(ctx context.Context, cluster *sharded.Cluster, refresh bool, resource string) error {
Expand All @@ -33,6 +50,8 @@ func FromShardedCluster(ctx context.Context, cluster *sharded.Cluster, refresh b
}

for _, shard := range cluster.Shards {
roslave := 0
rwslave := 0

for _, server := range shard.Servers {
ro, ok := server.Config["slave-read-only"]
Expand All @@ -43,7 +62,18 @@ func FromShardedCluster(ctx context.Context, cluster *sharded.Cluster, refresh b
"redis_server_host": server.ID(), "redis_server_alias": server.GetAlias(),
"role": string(server.Role), "read_only": ro,
}).Set(float64(1))

if server.Role == client.Slave {
if ro == "yes" {
roslave++
} else {
rwslave++
}
}
}

roSlaveCount.With(prometheus.Labels{"resource": resource, "shard": shard.Name}).Set(float64(roslave))
rwSlaveCount.With(prometheus.Labels{"resource": resource, "shard": shard.Name}).Set(float64(rwslave))
}

return nil
Expand Down
16 changes: 1 addition & 15 deletions pkg/redis/metrics/sentinel_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ var (
},
[]string{"sentinel", "shard", "redis_server", "role"},
)
numSlaves = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "num_slaves",
Namespace: "saas_redis_sentinel",
Help: `"sentinel master <name> num-slaves"`,
},
[]string{"sentinel", "shard", "redis_server", "role"},
)
numOtherSentinels = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "num_other_sentinels",
Expand Down Expand Up @@ -79,7 +71,7 @@ var (
func init() {
// Register custom metrics with the global prometheus registry
metrics.Registry.MustRegister(
linkPendingCommands, lastOkPingReply, roleReportedTime, numSlaves,
linkPendingCommands, lastOkPingReply, roleReportedTime,
numOtherSentinels, masterLinkDownTime, slaveReplOffset,
)
}
Expand Down Expand Up @@ -172,7 +164,6 @@ func (smg *SentinelMetricsGatherer) Stop() {
linkPendingCommands.Reset()
lastOkPingReply.Reset()
roleReportedTime.Reset()
numSlaves.Reset()
numOtherSentinels.Reset()
masterLinkDownTime.Reset()
slaveReplOffset.Reset()
Expand All @@ -199,10 +190,6 @@ func (smg *SentinelMetricsGatherer) gatherMetrics(ctx context.Context) error {
"redis_server": fmt.Sprintf("%s:%d", master.IP, master.Port), "role": master.RoleReported,
}).Set(float64(master.RoleReportedTime))

numSlaves.With(prometheus.Labels{"sentinel": smg.sentinelURI, "shard": master.Name,
"redis_server": fmt.Sprintf("%s:%d", master.IP, master.Port), "role": master.RoleReported,
}).Set(float64(master.NumSlaves))

numOtherSentinels.With(prometheus.Labels{"sentinel": smg.sentinelURI, "shard": master.Name,
"redis_server": fmt.Sprintf("%s:%d", master.IP, master.Port), "role": master.RoleReported,
}).Set(float64(master.NumOtherSentinels))
Expand Down Expand Up @@ -259,7 +246,6 @@ func cleanupMetrics(labels prometheus.Labels) {
linkPendingCommands.Delete(labels)
lastOkPingReply.Delete(labels)
roleReportedTime.Delete(labels)
numSlaves.Delete(labels)
numOtherSentinels.Delete(labels)
masterLinkDownTime.Delete(labels)
slaveReplOffset.Delete(labels)
Expand Down

0 comments on commit 9e4509f

Please sign in to comment.