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)
| 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. |
| 1371 | func (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 | |
| 1410 | const delayedResolutionEventName = "Delayed name resolution complete" |
| 1411 |
nothing calls this directly
no test coverage detected