updateRingMetrics updates ring metrics. Caller must be holding the Write lock!
(compareResult CompareResult)
| 660 | |
| 661 | // updateRingMetrics updates ring metrics. Caller must be holding the Write lock! |
| 662 | func (r *Ring) updateRingMetrics(compareResult CompareResult) { |
| 663 | if compareResult == Equal { |
| 664 | return |
| 665 | } |
| 666 | |
| 667 | numByStateByZone := map[string]map[string]int{} |
| 668 | oldestTimestampByState := map[string]int64{} |
| 669 | |
| 670 | // Initialized to zero so we emit zero-metrics (instead of not emitting anything) |
| 671 | for _, s := range []string{unhealthy, ACTIVE.String(), LEAVING.String(), PENDING.String(), JOINING.String(), READONLY.String()} { |
| 672 | numByStateByZone[s] = map[string]int{} |
| 673 | // make sure removed zones got zero value |
| 674 | for _, zone := range r.previousRingZones { |
| 675 | numByStateByZone[s][zone] = 0 |
| 676 | } |
| 677 | for _, zone := range r.ringZones { |
| 678 | numByStateByZone[s][zone] = 0 |
| 679 | } |
| 680 | oldestTimestampByState[s] = 0 |
| 681 | } |
| 682 | |
| 683 | for _, instance := range r.ringDesc.Ingesters { |
| 684 | s := instance.State.String() |
| 685 | if !r.IsHealthy(&instance, Reporting, r.KVClient.LastUpdateTime(r.key)) { |
| 686 | s = unhealthy |
| 687 | } |
| 688 | if _, ok := numByStateByZone[s]; !ok { |
| 689 | numByStateByZone[s] = map[string]int{} |
| 690 | } |
| 691 | numByStateByZone[s][instance.Zone]++ |
| 692 | if oldestTimestampByState[s] == 0 || instance.Timestamp < oldestTimestampByState[s] { |
| 693 | oldestTimestampByState[s] = instance.Timestamp |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | for state, zones := range numByStateByZone { |
| 698 | for zone, count := range zones { |
| 699 | r.numMembersGaugeVec.WithLabelValues(state, zone).Set(float64(count)) |
| 700 | } |
| 701 | } |
| 702 | for state, timestamp := range oldestTimestampByState { |
| 703 | r.oldestTimestampGaugeVec.WithLabelValues(state).Set(float64(timestamp)) |
| 704 | } |
| 705 | |
| 706 | if compareResult == EqualButStatesAndTimestamps { |
| 707 | return |
| 708 | } |
| 709 | |
| 710 | if r.cfg.DetailedMetricsEnabled { |
| 711 | prevOwners := r.reportedOwners |
| 712 | r.reportedOwners = make(map[string]struct{}) |
| 713 | numTokens, ownedRange := r.countTokens() |
| 714 | for id, totalOwned := range ownedRange { |
| 715 | r.memberOwnershipGaugeVec.WithLabelValues(id).Set(float64(totalOwned) / float64(math.MaxUint32+1)) |
| 716 | r.numTokensGaugeVec.WithLabelValues(id).Set(float64(numTokens[id])) |
| 717 | delete(prevOwners, id) |
| 718 | r.reportedOwners[id] = struct{}{} |
| 719 | } |
no test coverage detected