google.golang.org/grpc@v1.72.2/balancer/pickfirst/pickfirstleaf/metrics_test.go (about) 1 /* 2 * 3 * Copyright 2024 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package pickfirstleaf_test 20 21 import ( 22 "context" 23 "fmt" 24 "testing" 25 26 "google.golang.org/grpc" 27 "google.golang.org/grpc/balancer/pickfirst/pickfirstleaf" 28 "google.golang.org/grpc/connectivity" 29 "google.golang.org/grpc/credentials/insecure" 30 "google.golang.org/grpc/internal" 31 "google.golang.org/grpc/internal/stubserver" 32 "google.golang.org/grpc/internal/testutils" 33 "google.golang.org/grpc/internal/testutils/stats" 34 testgrpc "google.golang.org/grpc/interop/grpc_testing" 35 testpb "google.golang.org/grpc/interop/grpc_testing" 36 "google.golang.org/grpc/resolver" 37 "google.golang.org/grpc/resolver/manual" 38 "google.golang.org/grpc/serviceconfig" 39 "google.golang.org/grpc/stats/opentelemetry" 40 41 "go.opentelemetry.io/otel/attribute" 42 "go.opentelemetry.io/otel/sdk/metric" 43 "go.opentelemetry.io/otel/sdk/metric/metricdata" 44 "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" 45 ) 46 47 var pfConfig string 48 49 func init() { 50 pfConfig = fmt.Sprintf(`{ 51 "loadBalancingConfig": [ 52 { 53 %q: { 54 } 55 } 56 ] 57 }`, pickfirstleaf.Name) 58 } 59 60 // TestPickFirstMetrics tests pick first metrics. It configures a pick first 61 // balancer, causes it to connect and then disconnect, and expects the 62 // subsequent metrics to emit from that. 63 func (s) TestPickFirstMetrics(t *testing.T) { 64 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) 65 defer cancel() 66 67 ss := &stubserver.StubServer{ 68 EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) { 69 return &testpb.Empty{}, nil 70 }, 71 } 72 ss.StartServer() 73 defer ss.Stop() 74 75 sc := internal.ParseServiceConfig.(func(string) *serviceconfig.ParseResult)(pfConfig) 76 77 r := manual.NewBuilderWithScheme("whatever") 78 r.InitialState(resolver.State{ 79 ServiceConfig: sc, 80 Addresses: []resolver.Address{{Addr: ss.Address}}}, 81 ) 82 83 tmr := stats.NewTestMetricsRecorder() 84 cc, err := grpc.NewClient(r.Scheme()+":///", grpc.WithStatsHandler(tmr), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(r)) 85 if err != nil { 86 t.Fatalf("NewClient() failed with error: %v", err) 87 } 88 defer cc.Close() 89 90 tsc := testgrpc.NewTestServiceClient(cc) 91 if _, err := tsc.EmptyCall(ctx, &testpb.Empty{}); err != nil { 92 t.Fatalf("EmptyCall() failed: %v", err) 93 } 94 95 if got, _ := tmr.Metric("grpc.lb.pick_first.connection_attempts_succeeded"); got != 1 { 96 t.Errorf("Unexpected data for metric %v, got: %v, want: %v", "grpc.lb.pick_first.connection_attempts_succeeded", got, 1) 97 } 98 if got, _ := tmr.Metric("grpc.lb.pick_first.connection_attempts_failed"); got != 0 { 99 t.Errorf("Unexpected data for metric %v, got: %v, want: %v", "grpc.lb.pick_first.connection_attempts_failed", got, 0) 100 } 101 if got, _ := tmr.Metric("grpc.lb.pick_first.disconnections"); got != 0 { 102 t.Errorf("Unexpected data for metric %v, got: %v, want: %v", "grpc.lb.pick_first.disconnections", got, 0) 103 } 104 105 ss.Stop() 106 testutils.AwaitState(ctx, t, cc, connectivity.Idle) 107 if got, _ := tmr.Metric("grpc.lb.pick_first.disconnections"); got != 1 { 108 t.Errorf("Unexpected data for metric %v, got: %v, want: %v", "grpc.lb.pick_first.disconnections", got, 1) 109 } 110 } 111 112 // TestPickFirstMetricsFailure tests the connection attempts failed metric. It 113 // configures a channel and scenario that causes a pick first connection attempt 114 // to fail, and then expects that metric to emit. 115 func (s) TestPickFirstMetricsFailure(t *testing.T) { 116 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) 117 defer cancel() 118 119 sc := internal.ParseServiceConfig.(func(string) *serviceconfig.ParseResult)(pfConfig) 120 121 r := manual.NewBuilderWithScheme("whatever") 122 r.InitialState(resolver.State{ 123 ServiceConfig: sc, 124 Addresses: []resolver.Address{{Addr: "bad address"}}}, 125 ) 126 grpcTarget := r.Scheme() + ":///" 127 tmr := stats.NewTestMetricsRecorder() 128 cc, err := grpc.NewClient(grpcTarget, grpc.WithStatsHandler(tmr), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(r)) 129 if err != nil { 130 t.Fatalf("NewClient() failed with error: %v", err) 131 } 132 defer cc.Close() 133 134 tsc := testgrpc.NewTestServiceClient(cc) 135 if _, err := tsc.EmptyCall(ctx, &testpb.Empty{}); err == nil { 136 t.Fatalf("EmptyCall() passed when expected to fail") 137 } 138 139 if got, _ := tmr.Metric("grpc.lb.pick_first.connection_attempts_succeeded"); got != 0 { 140 t.Errorf("Unexpected data for metric %v, got: %v, want: %v", "grpc.lb.pick_first.connection_attempts_succeeded", got, 0) 141 } 142 if got, _ := tmr.Metric("grpc.lb.pick_first.connection_attempts_failed"); got != 1 { 143 t.Errorf("Unexpected data for metric %v, got: %v, want: %v", "grpc.lb.pick_first.connection_attempts_failed", got, 1) 144 } 145 if got, _ := tmr.Metric("grpc.lb.pick_first.disconnections"); got != 0 { 146 t.Errorf("Unexpected data for metric %v, got: %v, want: %v", "grpc.lb.pick_first.disconnections", got, 0) 147 } 148 } 149 150 // TestPickFirstMetricsE2E tests the pick first metrics end to end. It 151 // configures a channel with an OpenTelemetry plugin, induces all 3 pick first 152 // metrics to emit, and makes sure the correct OpenTelemetry metrics atoms emit. 153 func (s) TestPickFirstMetricsE2E(t *testing.T) { 154 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) 155 defer cancel() 156 157 ss := &stubserver.StubServer{ 158 EmptyCallF: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) { 159 return &testpb.Empty{}, nil 160 }, 161 } 162 ss.StartServer() 163 defer ss.Stop() 164 165 sc := internal.ParseServiceConfig.(func(string) *serviceconfig.ParseResult)(pfConfig) 166 r := manual.NewBuilderWithScheme("whatever") 167 r.InitialState(resolver.State{ 168 ServiceConfig: sc, 169 Addresses: []resolver.Address{{Addr: "bad address"}}}, 170 ) // Will trigger connection failed. 171 172 grpcTarget := r.Scheme() + ":///" 173 reader := metric.NewManualReader() 174 provider := metric.NewMeterProvider(metric.WithReader(reader)) 175 mo := opentelemetry.MetricsOptions{ 176 MeterProvider: provider, 177 Metrics: opentelemetry.DefaultMetrics().Add("grpc.lb.pick_first.disconnections", "grpc.lb.pick_first.connection_attempts_succeeded", "grpc.lb.pick_first.connection_attempts_failed"), 178 } 179 180 cc, err := grpc.NewClient(grpcTarget, opentelemetry.DialOption(opentelemetry.Options{MetricsOptions: mo}), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(r)) 181 if err != nil { 182 t.Fatalf("NewClient() failed with error: %v", err) 183 } 184 defer cc.Close() 185 186 tsc := testgrpc.NewTestServiceClient(cc) 187 if _, err := tsc.EmptyCall(ctx, &testpb.Empty{}); err == nil { 188 t.Fatalf("EmptyCall() passed when expected to fail") 189 } 190 191 r.UpdateState(resolver.State{ 192 ServiceConfig: sc, 193 Addresses: []resolver.Address{{Addr: ss.Address}}, 194 }) // Will trigger successful connection metric. 195 if _, err := tsc.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true)); err != nil { 196 t.Fatalf("EmptyCall() failed: %v", err) 197 } 198 199 // Stop the server, that should send signal to disconnect, which will 200 // eventually emit disconnection metric before ClientConn goes IDLE. 201 ss.Stop() 202 testutils.AwaitState(ctx, t, cc, connectivity.Idle) 203 wantMetrics := []metricdata.Metrics{ 204 { 205 Name: "grpc.lb.pick_first.connection_attempts_succeeded", 206 Description: "EXPERIMENTAL. Number of successful connection attempts.", 207 Unit: "attempt", 208 Data: metricdata.Sum[int64]{ 209 DataPoints: []metricdata.DataPoint[int64]{ 210 { 211 Attributes: attribute.NewSet(attribute.String("grpc.target", grpcTarget)), 212 Value: 1, 213 }, 214 }, 215 Temporality: metricdata.CumulativeTemporality, 216 IsMonotonic: true, 217 }, 218 }, 219 { 220 Name: "grpc.lb.pick_first.connection_attempts_failed", 221 Description: "EXPERIMENTAL. Number of failed connection attempts.", 222 Unit: "attempt", 223 Data: metricdata.Sum[int64]{ 224 DataPoints: []metricdata.DataPoint[int64]{ 225 { 226 Attributes: attribute.NewSet(attribute.String("grpc.target", grpcTarget)), 227 Value: 1, 228 }, 229 }, 230 Temporality: metricdata.CumulativeTemporality, 231 IsMonotonic: true, 232 }, 233 }, 234 { 235 Name: "grpc.lb.pick_first.disconnections", 236 Description: "EXPERIMENTAL. Number of times the selected subchannel becomes disconnected.", 237 Unit: "disconnection", 238 Data: metricdata.Sum[int64]{ 239 DataPoints: []metricdata.DataPoint[int64]{ 240 { 241 Attributes: attribute.NewSet(attribute.String("grpc.target", grpcTarget)), 242 Value: 1, 243 }, 244 }, 245 Temporality: metricdata.CumulativeTemporality, 246 IsMonotonic: true, 247 }, 248 }, 249 } 250 251 gotMetrics := metricsDataFromReader(ctx, reader) 252 for _, metric := range wantMetrics { 253 val, ok := gotMetrics[metric.Name] 254 if !ok { 255 t.Fatalf("Metric %v not present in recorded metrics", metric.Name) 256 } 257 if !metricdatatest.AssertEqual(t, metric, val, metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreExemplars()) { 258 t.Fatalf("Metrics data type not equal for metric: %v", metric.Name) 259 } 260 } 261 } 262 263 func metricsDataFromReader(ctx context.Context, reader *metric.ManualReader) map[string]metricdata.Metrics { 264 rm := &metricdata.ResourceMetrics{} 265 reader.Collect(ctx, rm) 266 gotMetrics := map[string]metricdata.Metrics{} 267 for _, sm := range rm.ScopeMetrics { 268 for _, m := range sm.Metrics { 269 gotMetrics[m.Name] = m 270 } 271 } 272 return gotMetrics 273 }