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

Method TestRPCSpanErrorStatus

stats/opentelemetry/e2e_test.go:1371–1408  ·  view source on GitHub ↗

TestRPCSpanErrorStatus verifies that errors during RPC calls are correctly reflected in the span status. It simulates a unary RPC that returns an error and checks that the span's status is set to error with the appropriate message.

(t *testing.T)

Source from the content-addressed store, hash-verified

1369// reflected in the span status. It simulates a unary RPC that returns an error
1370// and checks that the span's status is set to error with the appropriate message.
1371func (s) TestRPCSpanErrorStatus(t *testing.T) {
1372 mo, _ := defaultMetricsOptions(t, nil)
1373 // Using defaultTraceOptions to set up OpenTelemetry with an in-memory exporter
1374 to, exporter := defaultTraceOptions(t)
1375 const rpcErrorMsg = "unary call: internal server error"
1376 ss := &stubserver.StubServer{
1377 UnaryCallF: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
1378 return nil, fmt.Errorf("%v", rpcErrorMsg)
1379 },
1380 }
1381
1382 otelOptions := opentelemetry.Options{
1383 MetricsOptions: *mo,
1384 TraceOptions: *to,
1385 }
1386
1387 if err := ss.Start([]grpc.ServerOption{opentelemetry.ServerOption(otelOptions)},
1388 opentelemetry.DialOption(otelOptions)); err != nil {
1389 t.Fatalf("Error starting endpoint server: %v", err)
1390 }
1391 defer ss.Stop()
1392
1393 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
1394 defer cancel()
1395
1396 ss.Client.UnaryCall(ctx, &testpb.SimpleRequest{Payload: &testpb.Payload{
1397 Body: make([]byte, 10000),
1398 }})
1399
1400 // Verify spans has error status with rpcErrorMsg as error message.
1401 for ; len(exporter.GetSpans()) == 0 && ctx.Err() == nil; <-time.After(time.Millisecond) {
1402 // wait until trace spans are collected
1403 }
1404 spans := exporter.GetSpans()
1405 if got, want := spans[0].Status.Description, rpcErrorMsg; got != want {
1406 t.Fatalf("got rpc error %s, want %s", spans[0].Status.Description, rpcErrorMsg)
1407 }
1408}
1409
1410const delayedResolutionEventName = "Delayed name resolution complete"
1411

Callers

nothing calls this directly

Calls 10

StartMethod · 0.95
StopMethod · 0.95
ServerOptionFunction · 0.92
DialOptionFunction · 0.92
defaultMetricsOptionsFunction · 0.85
defaultTraceOptionsFunction · 0.85
ErrMethod · 0.80
ErrorfMethod · 0.65
FatalfMethod · 0.65
UnaryCallMethod · 0.65

Tested by

no test coverage detected