6262 Description : "EXPERIMENTAL. Number of scheduler updates in which there were not enough endpoints with valid weight, which caused the WRR policy to fall back to RR behavior." ,
6363 Unit : "{update}" ,
6464 Labels : []string {"grpc.target" },
65- OptionalLabels : []string {"grpc.lb.locality" },
65+ OptionalLabels : []string {"grpc.lb.locality" , "grpc.lb.backend_service" },
6666 Default : false ,
6767 })
6868
7171 Description : "EXPERIMENTAL. Number of endpoints from each scheduler update that don't yet have usable weight information (i.e., either the load report has not yet been received, or it is within the blackout period)." ,
7272 Unit : "{endpoint}" ,
7373 Labels : []string {"grpc.target" },
74- OptionalLabels : []string {"grpc.lb.locality" },
74+ OptionalLabels : []string {"grpc.lb.locality" , "grpc.lb.backend_service" },
7575 Default : false ,
7676 })
7777
@@ -80,15 +80,15 @@ var (
8080 Description : "EXPERIMENTAL. Number of endpoints from each scheduler update whose latest weight is older than the expiration period." ,
8181 Unit : "{endpoint}" ,
8282 Labels : []string {"grpc.target" },
83- OptionalLabels : []string {"grpc.lb.locality" },
83+ OptionalLabels : []string {"grpc.lb.locality" , "grpc.lb.backend_service" },
8484 Default : false ,
8585 })
8686 endpointWeightsMetric = estats .RegisterFloat64Histo (estats.MetricDescriptor {
8787 Name : "grpc.lb.wrr.endpoint_weights" ,
8888 Description : "EXPERIMENTAL. Weight of each endpoint, recorded on every scheduler update. Endpoints without usable weights will be recorded as weight 0." ,
8989 Unit : "{endpoint}" ,
9090 Labels : []string {"grpc.target" },
91- OptionalLabels : []string {"grpc.lb.locality" },
91+ OptionalLabels : []string {"grpc.lb.locality" , "grpc.lb.backend_service" },
9292 Default : false ,
9393 })
9494)
@@ -173,6 +173,7 @@ func (b *wrrBalancer) updateEndpointsLocked(endpoints []resolver.Endpoint) {
173173 metricsRecorder : b .metricsRecorder ,
174174 target : b .target ,
175175 locality : b .locality ,
176+ clusterName : b .clusterName ,
176177 }
177178 for _ , addr := range endpoint .Addresses {
178179 b .addressWeights .Set (addr , ew )
@@ -211,6 +212,7 @@ type wrrBalancer struct {
211212 mu sync.Mutex
212213 cfg * lbConfig // active config
213214 locality string
215+ clusterName string
214216 stopPicker * grpcsync.Event
215217 addressWeights * resolver.AddressMapV2 [* endpointWeight ]
216218 endpointToWeight * resolver.EndpointMap [* endpointWeight ]
@@ -231,6 +233,7 @@ func (b *wrrBalancer) UpdateClientConnState(ccs balancer.ClientConnState) error
231233 b .mu .Lock ()
232234 b .cfg = cfg
233235 b .locality = weightedtarget .LocalityFromResolverState (ccs .ResolverState )
236+ b .clusterName = backendServiceFromState (ccs .ResolverState )
234237 b .updateEndpointsLocked (ccs .ResolverState .Endpoints )
235238 b .mu .Unlock ()
236239
@@ -288,6 +291,7 @@ func (b *wrrBalancer) UpdateState(state balancer.State) {
288291 metricsRecorder : b .metricsRecorder ,
289292 locality : b .locality ,
290293 target : b .target ,
294+ clusterName : b .clusterName ,
291295 }
292296
293297 b .stopPicker = grpcsync .NewEvent ()
@@ -420,6 +424,7 @@ type picker struct {
420424 // The following fields are immutable.
421425 target string
422426 locality string
427+ clusterName string
423428 metricsRecorder estats.MetricsRecorder
424429}
425430
@@ -499,6 +504,7 @@ type endpointWeight struct {
499504 target string
500505 metricsRecorder estats.MetricsRecorder
501506 locality string
507+ clusterName string
502508
503509 // The following fields are only accessed on calls into the LB policy, and
504510 // do not need a mutex.
@@ -602,14 +608,14 @@ func (w *endpointWeight) weight(now time.Time, weightExpirationPeriod, blackoutP
602608
603609 if recordMetrics {
604610 defer func () {
605- endpointWeightsMetric .Record (w .metricsRecorder , weight , w .target , w .locality )
611+ endpointWeightsMetric .Record (w .metricsRecorder , weight , w .target , w .locality , w . clusterName )
606612 }()
607613 }
608614
609615 // The endpoint has not received a load report (i.e. just turned READY with
610616 // no load report).
611617 if w .lastUpdated .Equal (time.Time {}) {
612- endpointWeightNotYetUsableMetric .Record (w .metricsRecorder , 1 , w .target , w .locality )
618+ endpointWeightNotYetUsableMetric .Record (w .metricsRecorder , 1 , w .target , w .locality , w . clusterName )
613619 return 0
614620 }
615621
@@ -618,7 +624,7 @@ func (w *endpointWeight) weight(now time.Time, weightExpirationPeriod, blackoutP
618624 // start getting data again in the future, and return 0.
619625 if now .Sub (w .lastUpdated ) >= weightExpirationPeriod {
620626 if recordMetrics {
621- endpointWeightStaleMetric .Record (w .metricsRecorder , 1 , w .target , w .locality )
627+ endpointWeightStaleMetric .Record (w .metricsRecorder , 1 , w .target , w .locality , w . clusterName )
622628 }
623629 w .nonEmptySince = time.Time {}
624630 return 0
@@ -627,10 +633,27 @@ func (w *endpointWeight) weight(now time.Time, weightExpirationPeriod, blackoutP
627633 // If we don't have at least blackoutPeriod worth of data, return 0.
628634 if blackoutPeriod != 0 && (w .nonEmptySince .Equal (time.Time {}) || now .Sub (w .nonEmptySince ) < blackoutPeriod ) {
629635 if recordMetrics {
630- endpointWeightNotYetUsableMetric .Record (w .metricsRecorder , 1 , w .target , w .locality )
636+ endpointWeightNotYetUsableMetric .Record (w .metricsRecorder , 1 , w .target , w .locality , w . clusterName )
631637 }
632638 return 0
633639 }
634640
635641 return w .weightVal
636642}
643+
644+ type backendServiceKey struct {}
645+
646+ // SetBackendService stores the backendService on the resolver state so
647+ // that it can be used later as a label in wrr metrics.
648+ func SetBackendService (state resolver.State , backendService string ) resolver.State {
649+ state .Attributes = state .Attributes .WithValue (backendServiceKey {}, backendService )
650+ return state
651+ }
652+
653+ // getBackendServiceFromState retrieves the cluster name stored as an attribute
654+ // in the resolver state.
655+ func backendServiceFromState (state resolver.State ) string {
656+ v := state .Attributes .Value (backendServiceKey {})
657+ name , _ := v .(string )
658+ return name
659+ }
0 commit comments