TestE2ECallMetricsUnary tests the injection of custom backend metrics from the server application for a unary RPC, and verifies that expected load reports are received at the client.
(t *testing.T)
| 43 | // the server application for a unary RPC, and verifies that expected load |
| 44 | // reports are received at the client. |
| 45 | func (s) TestE2ECallMetricsUnary(t *testing.T) { |
| 46 | tests := []struct { |
| 47 | desc string |
| 48 | injectMetrics bool |
| 49 | wantProto *v3orcapb.OrcaLoadReport |
| 50 | }{ |
| 51 | { |
| 52 | desc: "with custom backend metrics", |
| 53 | injectMetrics: true, |
| 54 | wantProto: &v3orcapb.OrcaLoadReport{ |
| 55 | CpuUtilization: 1.0, |
| 56 | MemUtilization: 0.9, |
| 57 | RequestCost: map[string]float64{"queryCost": 25.0}, |
| 58 | Utilization: map[string]float64{"queueSize": 0.75}, |
| 59 | }, |
| 60 | }, |
| 61 | { |
| 62 | desc: "with no custom backend metrics", |
| 63 | injectMetrics: false, |
| 64 | }, |
| 65 | } |
| 66 | |
| 67 | for _, test := range tests { |
| 68 | t.Run(test.desc, func(t *testing.T) { |
| 69 | // A server option to enable reporting of per-call backend metrics. |
| 70 | smr := orca.NewServerMetricsRecorder() |
| 71 | callMetricsServerOption := orca.CallMetricsServerOption(smr) |
| 72 | smr.SetCPUUtilization(1.0) |
| 73 | |
| 74 | // An interceptor to injects custom backend metrics, added only when |
| 75 | // the injectMetrics field in the test is set. |
| 76 | injectingInterceptor := func(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) { |
| 77 | recorder := orca.CallMetricsRecorderFromContext(ctx) |
| 78 | if recorder == nil { |
| 79 | err := errors.New("Failed to retrieve per-RPC custom metrics recorder from the RPC context") |
| 80 | t.Error(err) |
| 81 | return nil, err |
| 82 | } |
| 83 | recorder.SetMemoryUtilization(0.9) |
| 84 | // This value will be overwritten by a write to the same metric |
| 85 | // from the server handler. |
| 86 | recorder.SetNamedUtilization("queueSize", 1.0) |
| 87 | return handler(ctx, req) |
| 88 | } |
| 89 | |
| 90 | // A stub server whose unary handler injects custom metrics, if the |
| 91 | // injectMetrics field in the test is set. It overwrites one of the |
| 92 | // values injected above, by the interceptor. |
| 93 | srv := stubserver.StubServer{ |
| 94 | EmptyCallF: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) { |
| 95 | if !test.injectMetrics { |
| 96 | return &testpb.Empty{}, nil |
| 97 | } |
| 98 | recorder := orca.CallMetricsRecorderFromContext(ctx) |
| 99 | if recorder == nil { |
| 100 | err := errors.New("Failed to retrieve per-RPC custom metrics recorder from the RPC context") |
| 101 | t.Error(err) |
| 102 | return nil, err |
nothing calls this directly
no test coverage detected