Debian Codegraph @

instrumentation/google.golang.org/grpc/otelgrpc/stats_handlertest_test.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otelgrpc_test
5
6 import (
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11 "go.opentelemetry.io/otel/attribute"
12 otelcode "go.opentelemetry.io/otel/codes"
13 "go.opentelemetry.io/otel/sdk/metric"
14 "go.opentelemetry.io/otel/sdk/metric/metricdata"
15 "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
16 "go.opentelemetry.io/otel/sdk/trace"
17 "go.opentelemetry.io/otel/sdk/trace/tracetest"
18 semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
19 "go.opentelemetry.io/otel/semconv/v1.40.0/rpcconv"
20 grpc_codes "google.golang.org/grpc/codes"
21 "google.golang.org/grpc/stats"
22 "google.golang.org/grpc/status"
23
24 "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
25 )
26
27 func getSpanFromRecorder(sr *tracetest.SpanRecorder, name string) (trace.ReadOnlySpan, bool) {
28 for _, s := range sr.Ended() {
29 if s.Name() == name {
30 return s, true
31 }
32 }
33 return nil, false
34 }
35
36 var serverChecks = []struct {
37 grpcCode grpc_codes.Code
38 wantSpanCode otelcode.Code
39 wantSpanStatusDescription string
40 wantRPCResponseStatusCode string
41 }{
42 {
43 grpcCode: grpc_codes.OK,
44 wantSpanCode: otelcode.Unset,
45 wantSpanStatusDescription: "",
46 wantRPCResponseStatusCode: "OK",
47 },
48 {
49 grpcCode: grpc_codes.Canceled,
50 wantSpanCode: otelcode.Unset,
51 wantSpanStatusDescription: "",
52 wantRPCResponseStatusCode: "CANCELLED",
53 },
54 {
55 grpcCode: grpc_codes.Unknown,
56 wantSpanCode: otelcode.Error,
57 wantSpanStatusDescription: grpc_codes.Unknown.String(),
58 wantRPCResponseStatusCode: "UNKNOWN",
59 },
60 {
61 grpcCode: grpc_codes.InvalidArgument,
62 wantSpanCode: otelcode.Unset,
63 wantSpanStatusDescription: "",
64 wantRPCResponseStatusCode: "INVALID_ARGUMENT",
65 },
66 {
67 grpcCode: grpc_codes.DeadlineExceeded,
68 wantSpanCode: otelcode.Error,
69 wantSpanStatusDescription: grpc_codes.DeadlineExceeded.String(),
70 wantRPCResponseStatusCode: "DEADLINE_EXCEEDED",
71 },
72 {
73 grpcCode: grpc_codes.NotFound,
74 wantSpanCode: otelcode.Unset,
75 wantSpanStatusDescription: "",
76 wantRPCResponseStatusCode: "NOT_FOUND",
77 },
78 {
79 grpcCode: grpc_codes.AlreadyExists,
80 wantSpanCode: otelcode.Unset,
81 wantSpanStatusDescription: "",
82 wantRPCResponseStatusCode: "ALREADY_EXISTS",
83 },
84 {
85 grpcCode: grpc_codes.PermissionDenied,
86 wantSpanCode: otelcode.Unset,
87 wantSpanStatusDescription: "",
88 wantRPCResponseStatusCode: "PERMISSION_DENIED",
89 },
90 {
91 grpcCode: grpc_codes.ResourceExhausted,
92 wantSpanCode: otelcode.Unset,
93 wantSpanStatusDescription: "",
94 wantRPCResponseStatusCode: "RESOURCE_EXHAUSTED",
95 },
96 {
97 grpcCode: grpc_codes.FailedPrecondition,
98 wantSpanCode: otelcode.Unset,
99 wantSpanStatusDescription: "",
100 wantRPCResponseStatusCode: "FAILED_PRECONDITION",
101 },
102 {
103 grpcCode: grpc_codes.Aborted,
104 wantSpanCode: otelcode.Unset,
105 wantSpanStatusDescription: "",
106 wantRPCResponseStatusCode: "ABORTED",
107 },
108 {
109 grpcCode: grpc_codes.OutOfRange,
110 wantSpanCode: otelcode.Unset,
111 wantSpanStatusDescription: "",
112 wantRPCResponseStatusCode: "OUT_OF_RANGE",
113 },
114 {
115 grpcCode: grpc_codes.Unimplemented,
116 wantSpanCode: otelcode.Error,
117 wantSpanStatusDescription: grpc_codes.Unimplemented.String(),
118 wantRPCResponseStatusCode: "UNIMPLEMENTED",
119 },
120 {
121 grpcCode: grpc_codes.Internal,
122 wantSpanCode: otelcode.Error,
123 wantSpanStatusDescription: grpc_codes.Internal.String(),
124 wantRPCResponseStatusCode: "INTERNAL",
125 },
126 {
127 grpcCode: grpc_codes.Unavailable,
128 wantSpanCode: otelcode.Error,
129 wantSpanStatusDescription: grpc_codes.Unavailable.String(),
130 wantRPCResponseStatusCode: "UNAVAILABLE",
131 },
132 {
133 grpcCode: grpc_codes.DataLoss,
134 wantSpanCode: otelcode.Error,
135 wantSpanStatusDescription: grpc_codes.DataLoss.String(),
136 wantRPCResponseStatusCode: "DATA_LOSS",
137 },
138 {
139 grpcCode: grpc_codes.Unauthenticated,
140 wantSpanCode: otelcode.Unset,
141 wantSpanStatusDescription: "",
142 wantRPCResponseStatusCode: "UNAUTHENTICATED",
143 },
144 {
145 grpcCode: grpc_codes.Code(9999),
146 wantSpanCode: otelcode.Unset,
147 wantSpanStatusDescription: "",
148 wantRPCResponseStatusCode: "CODE(9999)",
149 },
150 }
151
152 func TestStatsHandlerHandleRPCServerErrors(t *testing.T) {
153 for _, check := range serverChecks {
154 name := check.grpcCode.String()
155 t.Run(name, func(t *testing.T) {
156 t.Setenv("OTEL_METRICS_EXEMPLAR_FILTER", "always_off")
157 sr := tracetest.NewSpanRecorder()
158 tp := trace.NewTracerProvider(trace.WithSpanProcessor(sr))
159
160 mr := metric.NewManualReader()
161 mp := metric.NewMeterProvider(metric.WithReader(mr))
162
163 serverHandler := otelgrpc.NewServerHandler(
164 otelgrpc.WithTracerProvider(tp),
165 otelgrpc.WithMeterProvider(mp),
166 otelgrpc.WithMetricAttributes(testMetricAttr),
167 )
168
169 serviceName := "TestGrpcService"
170 methodName := serviceName + "/" + name
171 fullMethodName := "/" + methodName
172 // call the server handler
173 ctx := serverHandler.TagRPC(t.Context(), &stats.RPCTagInfo{
174 FullMethodName: fullMethodName,
175 })
176
177 grpcErr := status.Error(check.grpcCode, check.grpcCode.String())
178 serverHandler.HandleRPC(ctx, &stats.End{
179 Error: grpcErr,
180 })
181
182 // validate span
183 span, ok := getSpanFromRecorder(sr, methodName)
184 require.True(t, ok, "missing span %s", methodName)
185 assertServerSpan(t, check.wantSpanCode, check.wantSpanStatusDescription, check.wantRPCResponseStatusCode, span)
186
187 // validate metric
188 assertStatsHandlerServerMetrics(t, mr, serviceName, name, check.wantRPCResponseStatusCode)
189 })
190 }
191 }
192
193 func assertServerSpan(t *testing.T, wantSpanCode otelcode.Code, wantSpanStatusDescription, wantGrpcCode string, span trace.ReadOnlySpan) {
194 // validate span status
195 assert.Equal(t, wantSpanCode, span.Status().Code)
196 assert.Equal(t, wantSpanStatusDescription, span.Status().Description)
197
198 // validate grpc code span attribute
199 var codeAttr attribute.KeyValue
200 for _, a := range span.Attributes() {
201 if a.Key == semconv.RPCResponseStatusCodeKey {
202 codeAttr = a
203 break
204 }
205 }
206
207 require.True(t, codeAttr.Valid(), "attributes contain gRPC status code")
208 assert.Equal(t, attribute.StringValue(wantGrpcCode), codeAttr.Value)
209 }
210
211 func assertStatsHandlerServerMetrics(t *testing.T, reader metric.Reader, serviceName, name, code string) {
212 want := metricdata.ScopeMetrics{
213 Scope: wantInstrumentationScope,
214 Metrics: []metricdata.Metrics{
215 {
216 Name: rpcconv.ServerCallDuration{}.Name(),
217 Description: rpcconv.ServerCallDuration{}.Description(),
218 Unit: rpcconv.ServerCallDuration{}.Unit(),
219 Data: metricdata.Histogram[float64]{
220 Temporality: metricdata.CumulativeTemporality,
221 DataPoints: []metricdata.HistogramDataPoint[float64]{
222 {
223 Attributes: attribute.NewSet(
224 semconv.RPCMethod(serviceName+"/"+name),
225 semconv.RPCSystemNameGRPC,
226 semconv.RPCResponseStatusCode(code),
227 testMetricAttr,
228 ),
229 },
230 },
231 },
232 },
233 },
234 }
235 rm := metricdata.ResourceMetrics{}
236 err := reader.Collect(t.Context(), &rm)
237 assert.NoError(t, err)
238 require.Len(t, rm.ScopeMetrics, 1)
239 metricdatatest.AssertEqual(t, want, rm.ScopeMetrics[0], metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue())
240 }