TestSpan_WithW3CContextPropagator sets up a stub server with OpenTelemetry tracing enabled, makes a unary and a streaming RPC, and then asserts that the correct number of spans are created with the expected spans. Verification: - Verifies that the correct number of spans are created for both unary
(t *testing.T)
| 1167 | // - Verifies that the trace ID and span ID are correctly assigned and accessible |
| 1168 | // in the OpenTelemetry backend. |
| 1169 | func (s) TestSpan_WithW3CContextPropagator(t *testing.T) { |
| 1170 | mo, _ := defaultMetricsOptions(t, nil) |
| 1171 | // Using defaultTraceOptions to set up OpenTelemetry with an in-memory exporter |
| 1172 | to, exporter := defaultTraceOptions(t) |
| 1173 | // Set the W3CContextPropagator as part of TracingOptions. |
| 1174 | to.TextMapPropagator = propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}) |
| 1175 | // Start the server with OpenTelemetry options |
| 1176 | ss := setupStubServer(t, mo, to) |
| 1177 | defer ss.Stop() |
| 1178 | |
| 1179 | ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) |
| 1180 | defer cancel() |
| 1181 | |
| 1182 | // Make two RPC's, a unary RPC and a streaming RPC. These should cause |
| 1183 | // certain traces to be emitted, which should be observed through the |
| 1184 | // span exporter. |
| 1185 | if _, err := ss.Client.UnaryCall(ctx, &testpb.SimpleRequest{Payload: &testpb.Payload{ |
| 1186 | Body: make([]byte, 10000), |
| 1187 | }}); err != nil { |
| 1188 | t.Fatalf("Unexpected error from UnaryCall: %v", err) |
| 1189 | } |
| 1190 | stream, err := ss.Client.FullDuplexCall(ctx) |
| 1191 | if err != nil { |
| 1192 | t.Fatalf("ss.Client.FullDuplexCall failed: %f", err) |
| 1193 | } |
| 1194 | |
| 1195 | stream.CloseSend() |
| 1196 | if _, err = stream.Recv(); err != io.EOF { |
| 1197 | t.Fatalf("stream.Recv received an unexpected error: %v, expected an EOF error", err) |
| 1198 | } |
| 1199 | |
| 1200 | wantSpanInfos := []traceSpanInfo{ |
| 1201 | { |
| 1202 | name: "Recv.grpc.testing.TestService.UnaryCall", |
| 1203 | spanKind: oteltrace.SpanKindServer.String(), |
| 1204 | status: otelcodes.Ok, |
| 1205 | attributes: nil, |
| 1206 | events: []trace.Event{ |
| 1207 | { |
| 1208 | Name: "Inbound message", |
| 1209 | Attributes: []attribute.KeyValue{ |
| 1210 | { |
| 1211 | Key: "sequence-number", |
| 1212 | Value: attribute.IntValue(0), |
| 1213 | }, |
| 1214 | { |
| 1215 | Key: "message-size", |
| 1216 | Value: attribute.IntValue(10006), |
| 1217 | }, |
| 1218 | }, |
| 1219 | }, |
| 1220 | { |
| 1221 | Name: "Outbound message", |
| 1222 | Attributes: []attribute.KeyValue{ |
| 1223 | { |
| 1224 | Key: "sequence-number", |
| 1225 | Value: attribute.IntValue(0), |
| 1226 | }, |
nothing calls this directly
no test coverage detected