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

Added UserReplicaGroupMetrics #6463

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
39 changes: 38 additions & 1 deletion pkg/ha/ha_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import (
"github.com/cortexproject/cortex/pkg/util/services"
)

const (
userReplicaGroupUpdateInterval = 30 * time.Second
)

var (
errNegativeUpdateTimeoutJitterMax = errors.New("HA tracker max update timeout jitter shouldn't be negative")
errInvalidFailoverTimeout = "HA Tracker failover timeout (%v) must be at least 1s greater than update timeout - max jitter (%v)"
Expand Down Expand Up @@ -137,6 +141,7 @@ type HATracker struct {
electedReplicaTimestamp *prometheus.GaugeVec
electedReplicaPropagationTime prometheus.Histogram
kvCASCalls *prometheus.CounterVec
userReplicaGroupCount *prometheus.GaugeVec

cleanupRuns prometheus.Counter
replicasMarkedForDeletion prometheus.Counter
Expand Down Expand Up @@ -182,6 +187,11 @@ func NewHATracker(cfg HATrackerConfig, limits HATrackerLimits, trackerStatusConf
Help: "The total number of CAS calls to the KV store for a user ID/cluster.",
}, []string{"user", "cluster"}),

userReplicaGroupCount: promauto.With(reg).NewGaugeVec(prometheus.GaugeOpts{
Name: "ha_tracker_user_replica_group_count",
Help: "Number of HA replica groups tracked for each user.",
}, []string{"user"}),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to clean up the metric in CleanupHATrackerMetricsForUser so that when a user is not active anymore the corresponding user dimension is removed from the metric


cleanupRuns: promauto.With(reg).NewCounter(prometheus.CounterOpts{
Name: "ha_tracker_replicas_cleanup_started_total",
Help: "Number of elected replicas cleanup loops started.",
Expand Down Expand Up @@ -227,11 +237,26 @@ func (c *HATracker) loop(ctx context.Context) error {

// Start cleanup loop. It will stop when context is done.
wg := sync.WaitGroup{}
wg.Add(1)
wg.Add(2)
go func() {
defer wg.Done()
c.cleanupOldReplicasLoop(ctx)
}()
// Start periodic update of user replica group count.
go func() {
defer wg.Done()
ticker := time.NewTicker(userReplicaGroupUpdateInterval)
defer ticker.Stop()

for {
select {
case <-ticker.C:
c.updateUserReplicaGroupCount()
case <-ctx.Done():
return
}
}
}()

// The KVStore config we gave when creating c should have contained a prefix,
// which would have given us a prefixed KVStore client. So, we can pass empty string here.
Expand Down Expand Up @@ -504,6 +529,9 @@ func (c *HATracker) CleanupHATrackerMetricsForUser(userID string) {
if err := util.DeleteMatchingLabels(c.kvCASCalls, filter); err != nil {
level.Warn(c.logger).Log("msg", "failed to remove cortex_ha_tracker_kv_store_cas_total metric for user", "user", userID, "err", err)
}
if err := util.DeleteMatchingLabels(c.userReplicaGroupCount, filter); err != nil {
level.Warn(c.logger).Log("msg", "failed to remove cortex_ha_tracker_user_replica_group_count metric for user", "user", userID, "err", err)
}
}

// Returns a snapshot of the currently elected replicas. Useful for status display
Expand All @@ -521,3 +549,12 @@ func (c *HATracker) SnapshotElectedReplicas() map[string]ReplicaDesc {
}
return electedCopy
}

func (t *HATracker) updateUserReplicaGroupCount() {
t.electedLock.RLock()
defer t.electedLock.RUnlock()

for user, groups := range t.replicaGroups {
t.userReplicaGroupCount.WithLabelValues(user).Set(float64(len(groups)))
}
}
17 changes: 17 additions & 0 deletions pkg/ha/ha_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -795,3 +795,20 @@ func checkReplicaDeletionState(t *testing.T, duration time.Duration, c *HATracke
require.Equal(t, expectedMarkedForDeletion, markedForDeletion, "KV entry marked for deletion")
}
}
func TestHATracker_UserReplicaGroupMetrics(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this test and reuse the same test of TestHATracker_MetricsCleanup where we check metrics clean up functionality. You can follow what I did yeya24@9a35c7d#diff-59f8014af0fba125c4aa53685006c000bde94ce36bc718ce8613a465263a9ad7R630

t.Parallel()
reg := prometheus.NewPedanticRegistry()
tr, err := NewHATracker(HATrackerConfig{EnableHATracker: false}, nil, haTrackerStatusConfig, prometheus.WrapRegistererWithPrefix("cortex_", reg), "test-ha-tracker", log.NewNopLogger())
require.NoError(t, err)
metrics := []string{
"cortex_ha_tracker_user_replica_group_count",
}
tr.userReplicaGroupCount.WithLabelValues("userA").Add(2)
require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
# HELP cortex_ha_tracker_user_replica_group_count Number of HA replica groups tracked for each user.
# TYPE cortex_ha_tracker_user_replica_group_count gauge
cortex_ha_tracker_user_replica_group_count{user="userA"} 2
`), metrics...))

tr.CleanupHATrackerMetricsForUser("userA")
}
Loading