MCPcopy
hub / github.com/grpc/grpc-go / TestRLSTargetPickMetric

Method TestRLSTargetPickMetric

balancer/rls/metrics_test.go:57–164  ·  view source on GitHub ↗

TestRLSTargetPickMetric tests RLS Metrics in the case an RLS Balancer picks a target from an RLS Response for a RPC. This should emit a "grpc.lb.rls.target_picks" with certain labels and cache metrics with certain labels.

(t *testing.T)

Source from the content-addressed store, hash-verified

55// "grpc.lb.rls.target_picks" with certain labels and cache metrics with certain
56// labels.
57func (s) TestRLSTargetPickMetric(t *testing.T) {
58 // Overwrite the uuid random number generator to be deterministic.
59 uuid.SetRand(rand.New(rand.NewSource(1)))
60 defer uuid.SetRand(nil)
61 rlsServer, _ := rlstest.SetupFakeRLSServer(t, nil)
62 rlsConfig := buildBasicRLSConfigWithChildPolicy(t, t.Name(), rlsServer.Address)
63 backend := &stubserver.StubServer{
64 EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
65 return &testpb.Empty{}, nil
66 },
67 }
68 if err := backend.StartServer(); err != nil {
69 t.Fatalf("Failed to start backend: %v", err)
70 }
71 t.Logf("Started TestService backend at: %q", backend.Address)
72 defer backend.Stop()
73
74 rlsServer.SetResponseCallback(func(context.Context, *rlspb.RouteLookupRequest) *rlstest.RouteLookupResponse {
75 return &rlstest.RouteLookupResponse{Resp: &rlspb.RouteLookupResponse{Targets: []string{backend.Address}}}
76 })
77 r := startManualResolverWithConfig(t, rlsConfig)
78 reader := metric.NewManualReader()
79 provider := metric.NewMeterProvider(metric.WithReader(reader))
80 mo := opentelemetry.MetricsOptions{
81 MeterProvider: provider,
82 Metrics: opentelemetry.DefaultMetrics().Add("grpc.lb.rls.cache_entries", "grpc.lb.rls.cache_size", "grpc.lb.rls.default_target_picks", "grpc.lb.rls.target_picks", "grpc.lb.rls.failed_picks"),
83 OptionalLabels: []string{"grpc.client.call.custom"},
84 }
85 grpcTarget := r.Scheme() + ":///"
86 cc, err := grpc.NewClient(grpcTarget, grpc.WithResolvers(r), grpc.WithTransportCredentials(insecure.NewCredentials()), opentelemetry.DialOption(opentelemetry.Options{MetricsOptions: mo}))
87 if err != nil {
88 t.Fatalf("Failed to dial local test server: %v", err)
89 }
90 defer cc.Close()
91
92 wantMetrics := []metricdata.Metrics{
93 {
94 Name: "grpc.lb.rls.target_picks",
95 Description: "EXPERIMENTAL. Number of LB picks sent to each RLS target. Note that if the default target is also returned by the RLS server, RPCs sent to that target from the cache will be counted in this metric, not in grpc.rls.default_target_picks.",
96 Unit: "{pick}",
97 Data: metricdata.Sum[int64]{
98 DataPoints: []metricdata.DataPoint[int64]{
99 {
100 Attributes: attribute.NewSet(attribute.String("grpc.target", grpcTarget), attribute.String("grpc.lb.rls.server_target", rlsServer.Address), attribute.String("grpc.lb.rls.data_plane_target", backend.Address), attribute.String("grpc.lb.pick_result", "complete"), attribute.String("grpc.client.call.custom", "target-pick-custom")),
101 Value: 1,
102 },
103 },
104 Temporality: metricdata.CumulativeTemporality,
105 IsMonotonic: true,
106 },
107 },
108
109 // Receives an empty RLS Response, so a single cache entry with no size.
110 {
111 Name: "grpc.lb.rls.cache_entries",
112 Description: "EXPERIMENTAL. Number of entries in the RLS cache.",
113 Unit: "{entry}",
114 Data: metricdata.Gauge[int64]{

Callers

nothing calls this directly

Calls 15

StartServerMethod · 0.95
StopMethod · 0.95
EmptyCallMethod · 0.95
DefaultMetricsFunction · 0.92
NewClientFunction · 0.92
WithResolversFunction · 0.92
WithTransportCredentialsFunction · 0.92
NewCredentialsFunction · 0.92
DialOptionFunction · 0.92
SetResponseCallbackMethod · 0.80

Tested by

no test coverage detected