@@ -616,14 +616,17 @@ func prepareFrontendConn(ctrl *gomock.Controller) *agentmock.MockAgentService_Co
616
616
return frontendConn
617
617
}
618
618
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 ) {
620
620
t .Helper ()
621
+ if agentidentifiers == nil {
622
+ agentidentifiers = []string {}
623
+ }
621
624
// prepare the the connection to agent of proxy-server
622
625
agentConn := agentmock .NewMockAgentService_ConnectServer (ctrl )
623
626
agentConnMD := metadata.MD {
624
627
":authority" : []string {"127.0.0.1:8091" },
625
628
"agentid" : []string {uuid .New ().String ()},
626
- "agentidentifiers" : [] string {} ,
629
+ "agentidentifiers" : agentidentifiers ,
627
630
"content-type" : []string {"application/grpc" },
628
631
"user-agent" : []string {"grpc-go/1.42.0" },
629
632
}
@@ -658,7 +661,7 @@ func baseServerProxyTestWithBackend(t *testing.T, validate func(*agentmock.MockA
658
661
// prepare proxy server
659
662
proxyServer := NewProxyServer (uuid .New ().String (), []proxystrategies.ProxyStrategy {proxystrategies .ProxyStrategyDefault }, 1 , & AgentTokenAuthenticationOptions {}, xfrChannelSize )
660
663
661
- agentConn , _ := prepareAgentConnMD (t , ctrl , proxyServer )
664
+ agentConn , _ := prepareAgentConnMD (t , ctrl , proxyServer , nil )
662
665
663
666
validate (frontendConn , agentConn )
664
667
@@ -865,13 +868,34 @@ func TestReadyBackendsMetric(t *testing.T) {
865
868
p := NewProxyServer (uuid .New ().String (), []proxystrategies.ProxyStrategy {proxystrategies .ProxyStrategyDefault }, 1 , & AgentTokenAuthenticationOptions {}, xfrChannelSize )
866
869
assertReadyBackendsMetric (t , 0 )
867
870
868
- _ , backend := prepareAgentConnMD (t , ctrl , p )
871
+ _ , backend := prepareAgentConnMD (t , ctrl , p , nil )
869
872
assertReadyBackendsMetric (t , 1 )
870
873
871
874
p .removeBackend (backend )
872
875
assertReadyBackendsMetric (t , 0 )
873
876
}
874
877
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
+
875
899
func dialReqPkt (dialID int64 ) * client.Packet {
876
900
return & client.Packet {
877
901
Type : client .PacketType_DIAL_REQ ,
@@ -933,6 +957,13 @@ func assertReadyBackendsMetric(t testing.TB, expect int) {
933
957
}
934
958
}
935
959
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
+
936
967
func dialClosePkt (dialID int64 ) * client.Packet {
937
968
return & client.Packet {
938
969
Type : client .PacketType_DIAL_CLS ,
0 commit comments