TestMetricsAndTracesOptionEnabled verifies the integration of metrics and traces emitted by the OpenTelemetry instrumentation in a gRPC environment. It sets up a stub server with both metrics and traces enabled, and tests the correct emission of metrics and traces during a Unary RPC and a Streaming
(t *testing.T)
| 804 | // that the emitted metrics reflect the operations performed, including the size of |
| 805 | // the compressed message, and verifies that tracing information is correctly recorded. |
| 806 | func (s) TestMetricsAndTracesOptionEnabled(t *testing.T) { |
| 807 | // Create default metrics options |
| 808 | mo, reader := defaultMetricsOptions(t, nil) |
| 809 | // Create default trace options |
| 810 | to, exporter := defaultTraceOptions(t) |
| 811 | |
| 812 | ss := setupStubServer(t, mo, to) |
| 813 | defer ss.Stop() |
| 814 | |
| 815 | ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout*2) |
| 816 | defer cancel() |
| 817 | |
| 818 | // Make two RPC's, a unary RPC and a streaming RPC. These should cause |
| 819 | // certain metrics and traces to be emitted which should be observed |
| 820 | // through metrics reader and span exporter respectively. |
| 821 | if _, err := ss.Client.UnaryCall(ctx, &testpb.SimpleRequest{Payload: &testpb.Payload{ |
| 822 | Body: make([]byte, 10000), |
| 823 | }}, grpc.UseCompressor(gzip.Name)); err != nil { // Deterministic compression. |
| 824 | t.Fatalf("Unexpected error from UnaryCall: %v", err) |
| 825 | } |
| 826 | stream, err := ss.Client.FullDuplexCall(ctx) |
| 827 | if err != nil { |
| 828 | t.Fatalf("ss.Client.FullDuplexCall failed: %f", err) |
| 829 | } |
| 830 | |
| 831 | stream.CloseSend() |
| 832 | if _, err = stream.Recv(); err != io.EOF { |
| 833 | t.Fatalf("stream.Recv received an unexpected error: %v, expected an EOF error", err) |
| 834 | } |
| 835 | |
| 836 | // Verify metrics |
| 837 | rm := &metricdata.ResourceMetrics{} |
| 838 | reader.Collect(ctx, rm) |
| 839 | |
| 840 | gotMetrics := map[string]metricdata.Metrics{} |
| 841 | for _, sm := range rm.ScopeMetrics { |
| 842 | for _, m := range sm.Metrics { |
| 843 | gotMetrics[m.Name] = m |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | wantMetrics := testutils.MetricData(testutils.MetricDataOptions{ |
| 848 | Target: ss.Target, |
| 849 | UnaryCompressedMessageSize: float64(57), |
| 850 | }) |
| 851 | gotMetrics = testutils.WaitForServerMetrics(ctx, t, reader, gotMetrics, wantMetrics) |
| 852 | testutils.CompareMetrics(t, gotMetrics, wantMetrics) |
| 853 | |
| 854 | wantSpanInfos := []traceSpanInfo{ |
| 855 | { |
| 856 | name: "Recv.grpc.testing.TestService.UnaryCall", |
| 857 | spanKind: oteltrace.SpanKindServer.String(), |
| 858 | status: otelcodes.Ok, |
| 859 | attributes: nil, |
| 860 | events: []trace.Event{ |
| 861 | { |
| 862 | Name: "Inbound message", |
| 863 | Attributes: []attribute.KeyValue{ |
nothing calls this directly
no test coverage detected