@@ -616,14 +616,17 @@ func prepareFrontendConn(ctrl *gomock.Controller) *agentmock.MockAgentService_Co
616616 return frontendConn
617617}
618618
619- func prepareAgentConnMD (t testing.TB , ctrl * gomock.Controller , proxyServer * ProxyServer ) (* agentmock.MockAgentService_ConnectServer , * Backend ) {
619+ func prepareAgentConnMD (t testing.TB , ctrl * gomock.Controller , proxyServer * ProxyServer , agentidentifiers [] string ) (* agentmock.MockAgentService_ConnectServer , * Backend ) {
620620 t .Helper ()
621+ if agentidentifiers == nil {
622+ agentidentifiers = []string {}
623+ }
621624 // prepare the the connection to agent of proxy-server
622625 agentConn := agentmock .NewMockAgentService_ConnectServer (ctrl )
623626 agentConnMD := metadata.MD {
624627 ":authority" : []string {"127.0.0.1:8091" },
625628 "agentid" : []string {uuid .New ().String ()},
626- "agentidentifiers" : [] string {} ,
629+ "agentidentifiers" : agentidentifiers ,
627630 "content-type" : []string {"application/grpc" },
628631 "user-agent" : []string {"grpc-go/1.42.0" },
629632 }
@@ -658,7 +661,7 @@ func baseServerProxyTestWithBackend(t *testing.T, validate func(*agentmock.MockA
658661 // prepare proxy server
659662 proxyServer := NewProxyServer (uuid .New ().String (), []proxystrategies.ProxyStrategy {proxystrategies .ProxyStrategyDefault }, 1 , & AgentTokenAuthenticationOptions {}, xfrChannelSize )
660663
661- agentConn , _ := prepareAgentConnMD (t , ctrl , proxyServer )
664+ agentConn , _ := prepareAgentConnMD (t , ctrl , proxyServer , nil )
662665
663666 validate (frontendConn , agentConn )
664667
@@ -865,13 +868,34 @@ func TestReadyBackendsMetric(t *testing.T) {
865868 p := NewProxyServer (uuid .New ().String (), []proxystrategies.ProxyStrategy {proxystrategies .ProxyStrategyDefault }, 1 , & AgentTokenAuthenticationOptions {}, xfrChannelSize )
866869 assertReadyBackendsMetric (t , 0 )
867870
868- _ , backend := prepareAgentConnMD (t , ctrl , p )
871+ _ , backend := prepareAgentConnMD (t , ctrl , p , nil )
869872 assertReadyBackendsMetric (t , 1 )
870873
871874 p .removeBackend (backend )
872875 assertReadyBackendsMetric (t , 0 )
873876}
874877
878+ func TestTotalReadyBackendsMetric (t * testing.T ) {
879+ ctrl := gomock .NewController (t )
880+ defer ctrl .Finish ()
881+
882+ metrics .Metrics .Reset ()
883+
884+ p := NewProxyServer (uuid .New ().String (), []proxystrategies.ProxyStrategy {proxystrategies .ProxyStrategyDefault , proxystrategies .ProxyStrategyDestHost }, 1 , & AgentTokenAuthenticationOptions {}, xfrChannelSize )
885+ assertTotalReadyBackendsMetric (t , map [string ]int {proxystrategies .ProxyStrategyDefault .String (): 0 , proxystrategies .ProxyStrategyDestHost .String (): 0 })
886+
887+ _ , backend1 := prepareAgentConnMD (t , ctrl , p , nil )
888+ assertTotalReadyBackendsMetric (t , map [string ]int {proxystrategies .ProxyStrategyDefault .String (): 1 , proxystrategies .ProxyStrategyDestHost .String (): 0 })
889+
890+ // Add a backend with IPv4 agent identifier.
891+ _ , backend2 := prepareAgentConnMD (t , ctrl , p , []string {"host=localhost" })
892+ assertTotalReadyBackendsMetric (t , map [string ]int {proxystrategies .ProxyStrategyDefault .String (): 2 , proxystrategies .ProxyStrategyDestHost .String (): 1 })
893+
894+ p .removeBackend (backend1 )
895+ p .removeBackend (backend2 )
896+ assertTotalReadyBackendsMetric (t , map [string ]int {proxystrategies .ProxyStrategyDefault .String (): 0 , proxystrategies .ProxyStrategyDestHost .String (): 0 })
897+ }
898+
875899func dialReqPkt (dialID int64 ) * client.Packet {
876900 return & client.Packet {
877901 Type : client .PacketType_DIAL_REQ ,
@@ -933,6 +957,13 @@ func assertReadyBackendsMetric(t testing.TB, expect int) {
933957 }
934958}
935959
960+ func assertTotalReadyBackendsMetric (t testing.TB , expect map [string ]int ) {
961+ t .Helper ()
962+ if err := metricstest .DefaultTester .ExpectServerTotalReadyBackends (expect ); err != nil {
963+ t .Errorf ("Expected %s metric for each proxy strategy %+v, but got error: %v" , "ready_backend_connections_total" , expect , err )
964+ }
965+ }
966+
936967func dialClosePkt (dialID int64 ) * client.Packet {
937968 return & client.Packet {
938969 Type : client .PacketType_DIAL_CLS ,
0 commit comments