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

Method TestE2ECallMetricsUnary

orca/call_metrics_test.go:45–146  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

43// the server application for a unary RPC, and verifies that expected load
44// reports are received at the client.
45func (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

Callers

nothing calls this directly

Calls 15

SetCPUUtilizationMethod · 0.95
StartServerMethod · 0.95
StopMethod · 0.95
EmptyCallMethod · 0.95
NewServerMetricsRecorderFunction · 0.92
CallMetricsServerOptionFunction · 0.92
ChainUnaryInterceptorFunction · 0.92
NewClientFunction · 0.92
WithTransportCredentialsFunction · 0.92
NewCredentialsFunction · 0.92
TrailerFunction · 0.92

Tested by

no test coverage detected