UpdateMetrics cleans up dangling user and label set from the tracker as well as metrics. It takes a function for user to customize the metrics cleanup logic when either a user or a specific label set is removed. If a user is removed then removeUser is set to true.
(userSet map[string]map[uint64]struct{}, deleteMetricFunc func(user, labelSetStr string, removeUser bool))
| 53 | // It takes a function for user to customize the metrics cleanup logic when either a user or |
| 54 | // a specific label set is removed. If a user is removed then removeUser is set to true. |
| 55 | func (m *LabelSetTracker) UpdateMetrics(userSet map[string]map[uint64]struct{}, deleteMetricFunc func(user, labelSetStr string, removeUser bool)) { |
| 56 | for i := range numMetricShards { |
| 57 | shard := m.shards[i] |
| 58 | shard.Lock() |
| 59 | |
| 60 | for user, userEntry := range shard.userLabelSets { |
| 61 | limits, ok := userSet[user] |
| 62 | // Remove user if it doesn't exist or has no limits anymore. |
| 63 | if !ok || len(limits) == 0 { |
| 64 | deleteMetricFunc(user, "", true) |
| 65 | delete(shard.userLabelSets, user) |
| 66 | continue |
| 67 | } |
| 68 | for h, lbls := range userEntry { |
| 69 | // This limit no longer exists. |
| 70 | if _, ok := limits[h]; !ok { |
| 71 | delete(userEntry, h) |
| 72 | labelSetStr := lbls.String() |
| 73 | deleteMetricFunc(user, labelSetStr, false) |
| 74 | continue |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | shard.Unlock() |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // labelSetExists is used for testing only to check the existence of a label set. |
| 84 | func (m *LabelSetTracker) labelSetExists(userId string, hash uint64, labelSet labels.Labels) bool { |