Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ring_members_by_zone metric if ring is zone aware #5718

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions pkg/ring/ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,13 @@ type Ring struct {
// If set to nil, no caching is done (used by tests, and subrings).
shuffledSubringCache map[subringCacheKey]*Ring

memberOwnershipGaugeVec *prometheus.GaugeVec
numMembersGaugeVec *prometheus.GaugeVec
totalTokensGauge prometheus.Gauge
numTokensGaugeVec *prometheus.GaugeVec
oldestTimestampGaugeVec *prometheus.GaugeVec
reportedOwners map[string]struct{}
memberOwnershipGaugeVec *prometheus.GaugeVec
numMembersGaugeVec *prometheus.GaugeVec
numMembersByZoneGaugeVec *prometheus.GaugeVec
totalTokensGauge prometheus.Gauge
numTokensGaugeVec *prometheus.GaugeVec
oldestTimestampGaugeVec *prometheus.GaugeVec
reportedOwners map[string]struct{}

logger log.Logger
}
Expand Down Expand Up @@ -249,6 +250,11 @@ func NewWithStoreClientAndStrategy(cfg Config, name, key string, store kv.Client
Help: "Number of members in the ring",
ConstLabels: map[string]string{"name": name}},
[]string{"state"}),
numMembersByZoneGaugeVec: promauto.With(reg).NewGaugeVec(prometheus.GaugeOpts{
Name: "ring_members_by_zone",
Help: "Number of members in the ring by zone",
ConstLabels: map[string]string{"name": name}},
[]string{"zone", "state"}),
totalTokensGauge: promauto.With(reg).NewGauge(prometheus.GaugeOpts{
Name: "ring_tokens_total",
Help: "Number of tokens in the ring",
Expand Down Expand Up @@ -338,6 +344,7 @@ func (r *Ring) updateRingState(ringDesc *Desc) {

r.mtx.Lock()
defer r.mtx.Unlock()
prevRingZones := r.ringZones
r.ringDesc = ringDesc
r.ringTokens = ringTokens
r.ringTokensByZone = ringTokensByZone
Expand All @@ -348,6 +355,24 @@ func (r *Ring) updateRingState(ringDesc *Desc) {
// Invalidate all cached subrings.
r.shuffledSubringCache = make(map[subringCacheKey]*Ring)
}
//cleaning up non-existent zone metrics
for _, prevZone := range prevRingZones {
zoneStillExists := false
for _, currZone := range r.ringZones {
if prevZone == currZone {
zoneStillExists = true
break
}
}
if !zoneStillExists {
//need to remove non existing zone
for _, s := range []string{unhealthy, ACTIVE.String(), LEAVING.String(), PENDING.String(), JOINING.String()} {
if ok := r.numMembersByZoneGaugeVec.DeleteLabelValues(prevZone, s); !ok {
level.Warn(r.logger).Log("msg", "failed to remove ring_members_by_zone metric for non existing zone", "zone", prevZone, "state", s)
}
}
}
}
r.updateRingMetrics(rc)
}

Expand Down Expand Up @@ -604,12 +629,20 @@ func (r *Ring) updateRingMetrics(compareResult CompareResult) {
}

numByState := map[string]int{}
numByZoneAndState := map[string]map[string]int{}
oldestTimestampByState := map[string]int64{}

// Initialized to zero so we emit zero-metrics (instead of not emitting anything)
for _, s := range []string{unhealthy, ACTIVE.String(), LEAVING.String(), PENDING.String(), JOINING.String()} {
numByState[s] = 0
oldestTimestampByState[s] = 0
zones := r.ringZones
for _, zone := range zones {
if _, ok := numByZoneAndState[zone]; !ok {
numByZoneAndState[zone] = make(map[string]int)
}
for _, s := range []string{unhealthy, ACTIVE.String(), LEAVING.String(), PENDING.String(), JOINING.String()} {
numByState[s] = 0
oldestTimestampByState[s] = 0
numByZoneAndState[zone][s] = 0
}
}

for _, instance := range r.ringDesc.Ingesters {
Expand All @@ -618,6 +651,7 @@ func (r *Ring) updateRingMetrics(compareResult CompareResult) {
s = unhealthy
}
numByState[s]++
numByZoneAndState[instance.Zone][s]++
if oldestTimestampByState[s] == 0 || instance.Timestamp < oldestTimestampByState[s] {
oldestTimestampByState[s] = instance.Timestamp
}
Expand All @@ -626,6 +660,11 @@ func (r *Ring) updateRingMetrics(compareResult CompareResult) {
for state, count := range numByState {
r.numMembersGaugeVec.WithLabelValues(state).Set(float64(count))
}
for zone, numByStateCount := range numByZoneAndState {
for state, count := range numByStateCount {
r.numMembersByZoneGaugeVec.WithLabelValues(zone, state).Set(float64(count))
}
}
for state, timestamp := range oldestTimestampByState {
r.oldestTimestampGaugeVec.WithLabelValues(state).Set(float64(timestamp))
}
Expand Down
41 changes: 36 additions & 5 deletions pkg/ring/ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2962,8 +2962,8 @@ func TestUpdateMetrics(t *testing.T) {

ringDesc := Desc{
Ingesters: map[string]InstanceDesc{
"A": {Addr: "127.0.0.1", Timestamp: 22, Tokens: []uint32{math.MaxUint32 / 4, (math.MaxUint32 / 4) * 3}},
"B": {Addr: "127.0.0.2", Timestamp: 11, Tokens: []uint32{(math.MaxUint32 / 4) * 2, math.MaxUint32}},
"A": {Addr: "127.0.0.1", Zone: "us-west-2a", Timestamp: 22, Tokens: []uint32{math.MaxUint32 / 4, (math.MaxUint32 / 4) * 3}},
"B": {Addr: "127.0.0.2", Zone: "us-west-2b", Timestamp: 11, Tokens: []uint32{(math.MaxUint32 / 4) * 2, math.MaxUint32}},
},
}
ring.updateRingState(&ringDesc)
Expand All @@ -2980,6 +2980,18 @@ func TestUpdateMetrics(t *testing.T) {
ring_members{name="test",state="LEAVING"} 0
ring_members{name="test",state="PENDING"} 0
ring_members{name="test",state="Unhealthy"} 0
# HELP ring_members_by_zone Number of members in the ring by zone
# TYPE ring_members_by_zone gauge
ring_members_by_zone{name="test",state="ACTIVE",zone="us-west-2a"} 1
ring_members_by_zone{name="test",state="ACTIVE",zone="us-west-2b"} 1
ring_members_by_zone{name="test",state="JOINING",zone="us-west-2a"} 0
ring_members_by_zone{name="test",state="JOINING",zone="us-west-2b"} 0
ring_members_by_zone{name="test",state="LEAVING",zone="us-west-2a"} 0
ring_members_by_zone{name="test",state="LEAVING",zone="us-west-2b"} 0
ring_members_by_zone{name="test",state="PENDING",zone="us-west-2a"} 0
ring_members_by_zone{name="test",state="PENDING",zone="us-west-2b"} 0
ring_members_by_zone{name="test",state="Unhealthy",zone="us-west-2a"} 0
ring_members_by_zone{name="test",state="Unhealthy",zone="us-west-2b"} 0
# HELP ring_oldest_member_timestamp Timestamp of the oldest member in the ring.
# TYPE ring_oldest_member_timestamp gauge
ring_oldest_member_timestamp{name="test",state="ACTIVE"} 11
Expand Down Expand Up @@ -3014,8 +3026,8 @@ func TestUpdateMetricsWithRemoval(t *testing.T) {

ringDesc := Desc{
Ingesters: map[string]InstanceDesc{
"A": {Addr: "127.0.0.1", Timestamp: 22, Tokens: []uint32{math.MaxUint32 / 4, (math.MaxUint32 / 4) * 3}},
"B": {Addr: "127.0.0.2", Timestamp: 11, Tokens: []uint32{(math.MaxUint32 / 4) * 2, math.MaxUint32}},
"A": {Addr: "127.0.0.1", Zone: "us-west-2a", Timestamp: 22, Tokens: []uint32{math.MaxUint32 / 4, (math.MaxUint32 / 4) * 3}},
"B": {Addr: "127.0.0.2", Zone: "us-west-2b", Timestamp: 11, Tokens: []uint32{(math.MaxUint32 / 4) * 2, math.MaxUint32}},
},
}
ring.updateRingState(&ringDesc)
Expand All @@ -3032,6 +3044,18 @@ func TestUpdateMetricsWithRemoval(t *testing.T) {
ring_members{name="test",state="LEAVING"} 0
ring_members{name="test",state="PENDING"} 0
ring_members{name="test",state="Unhealthy"} 0
# HELP ring_members_by_zone Number of members in the ring by zone
# TYPE ring_members_by_zone gauge
ring_members_by_zone{name="test",state="ACTIVE",zone="us-west-2a"} 1
ring_members_by_zone{name="test",state="ACTIVE",zone="us-west-2b"} 1
ring_members_by_zone{name="test",state="JOINING",zone="us-west-2a"} 0
ring_members_by_zone{name="test",state="JOINING",zone="us-west-2b"} 0
ring_members_by_zone{name="test",state="LEAVING",zone="us-west-2a"} 0
ring_members_by_zone{name="test",state="LEAVING",zone="us-west-2b"} 0
ring_members_by_zone{name="test",state="PENDING",zone="us-west-2a"} 0
ring_members_by_zone{name="test",state="PENDING",zone="us-west-2b"} 0
ring_members_by_zone{name="test",state="Unhealthy",zone="us-west-2a"} 0
ring_members_by_zone{name="test",state="Unhealthy",zone="us-west-2b"} 0
# HELP ring_oldest_member_timestamp Timestamp of the oldest member in the ring.
# TYPE ring_oldest_member_timestamp gauge
ring_oldest_member_timestamp{name="test",state="ACTIVE"} 11
Expand All @@ -3051,7 +3075,7 @@ func TestUpdateMetricsWithRemoval(t *testing.T) {

ringDescNew := Desc{
Ingesters: map[string]InstanceDesc{
"A": {Addr: "127.0.0.1", Timestamp: 22, Tokens: []uint32{math.MaxUint32 / 4, (math.MaxUint32 / 4) * 3}},
"A": {Addr: "127.0.0.1", Zone: "us-west-2a", Timestamp: 22, Tokens: []uint32{math.MaxUint32 / 4, (math.MaxUint32 / 4) * 3}},
},
}
ring.updateRingState(&ringDescNew)
Expand All @@ -3067,6 +3091,13 @@ func TestUpdateMetricsWithRemoval(t *testing.T) {
ring_members{name="test",state="LEAVING"} 0
ring_members{name="test",state="PENDING"} 0
ring_members{name="test",state="Unhealthy"} 0
# HELP ring_members_by_zone Number of members in the ring by zone
# TYPE ring_members_by_zone gauge
ring_members_by_zone{name="test",state="ACTIVE",zone="us-west-2a"} 1
ring_members_by_zone{name="test",state="JOINING",zone="us-west-2a"} 0
ring_members_by_zone{name="test",state="LEAVING",zone="us-west-2a"} 0
ring_members_by_zone{name="test",state="PENDING",zone="us-west-2a"} 0
ring_members_by_zone{name="test",state="Unhealthy",zone="us-west-2a"} 0
# HELP ring_oldest_member_timestamp Timestamp of the oldest member in the ring.
# TYPE ring_oldest_member_timestamp gauge
ring_oldest_member_timestamp{name="test",state="ACTIVE"} 22
Expand Down
Loading