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