Debian Codegraph @

exporters/otlp/otlptrace/otlptracegrpc/client_unit_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otlptracegrpc
5
6 import (
7 "context"
8 "testing"
9 "time"
10
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 "google.golang.org/genproto/googleapis/rpc/errdetails"
14 "google.golang.org/grpc/codes"
15 "google.golang.org/grpc/status"
16 "google.golang.org/protobuf/types/known/durationpb"
17 )
18
19 func TestThrottleDelay(t *testing.T) {
20 c := codes.ResourceExhausted
21 testcases := []struct {
22 status *status.Status
23 wantOK bool
24 wantDuration time.Duration
25 }{
26 {
27 status: status.New(c, "NoRetryInfo"),
28 wantOK: false,
29 wantDuration: 0,
30 },
31 {
32 status: func() *status.Status {
33 s, err := status.New(c, "SingleRetryInfo").WithDetails(
34 &errdetails.RetryInfo{
35 RetryDelay: durationpb.New(15 * time.Millisecond),
36 },
37 )
38 require.NoError(t, err)
39 return s
40 }(),
41 wantOK: true,
42 wantDuration: 15 * time.Millisecond,
43 },
44 {
45 status: func() *status.Status {
46 s, err := status.New(c, "ErrorInfo").WithDetails(
47 &errdetails.ErrorInfo{Reason: "no throttle detail"},
48 )
49 require.NoError(t, err)
50 return s
51 }(),
52 wantOK: false,
53 wantDuration: 0,
54 },
55 {
56 status: func() *status.Status {
57 s, err := status.New(c, "ErrorAndRetryInfo").WithDetails(
58 &errdetails.ErrorInfo{Reason: "with throttle detail"},
59 &errdetails.RetryInfo{
60 RetryDelay: durationpb.New(13 * time.Minute),
61 },
62 )
63 require.NoError(t, err)
64 return s
65 }(),
66 wantOK: true,
67 wantDuration: 13 * time.Minute,
68 },
69 {
70 status: func() *status.Status {
71 s, err := status.New(c, "DoubleRetryInfo").WithDetails(
72 &errdetails.RetryInfo{
73 RetryDelay: durationpb.New(13 * time.Minute),
74 },
75 &errdetails.RetryInfo{
76 RetryDelay: durationpb.New(15 * time.Minute),
77 },
78 )
79 require.NoError(t, err)
80 return s
81 }(),
82 wantOK: true,
83 wantDuration: 13 * time.Minute,
84 },
85 }
86
87 for _, tc := range testcases {
88 t.Run(tc.status.Message(), func(t *testing.T) {
89 ok, d := throttleDelay(tc.status)
90 assert.Equal(t, tc.wantOK, ok)
91 assert.Equal(t, tc.wantDuration, d)
92 })
93 }
94 }
95
96 func TestRetryable(t *testing.T) {
97 retryableCodes := map[codes.Code]bool{
98 codes.OK: false,
99 codes.Canceled: true,
100 codes.Unknown: false,
101 codes.InvalidArgument: false,
102 codes.DeadlineExceeded: true,
103 codes.NotFound: false,
104 codes.AlreadyExists: false,
105 codes.PermissionDenied: false,
106 codes.ResourceExhausted: false,
107 codes.FailedPrecondition: false,
108 codes.Aborted: true,
109 codes.OutOfRange: true,
110 codes.Unimplemented: false,
111 codes.Internal: false,
112 codes.Unavailable: true,
113 codes.DataLoss: true,
114 codes.Unauthenticated: false,
115 }
116
117 for c, want := range retryableCodes {
118 got, _ := retryable(status.Error(c, ""))
119 assert.Equalf(t, want, got, "evaluate(%s)", c)
120 }
121 }
122
123 func TestRetryableGRPCStatusResourceExhaustedWithRetryInfo(t *testing.T) {
124 delay := 15 * time.Millisecond
125 s, err := status.New(codes.ResourceExhausted, "WithRetryInfo").WithDetails(
126 &errdetails.RetryInfo{
127 RetryDelay: durationpb.New(delay),
128 },
129 )
130 require.NoError(t, err)
131
132 ok, d := retryableGRPCStatus(s)
133 assert.True(t, ok)
134 assert.Equal(t, delay, d)
135 }
136
137 func TestUnstartedStop(t *testing.T) {
138 client := NewClient()
139 assert.ErrorIs(t, client.Stop(t.Context()), errAlreadyStopped)
140 }
141
142 func TestUnstartedUploadTrace(t *testing.T) {
143 client := NewClient()
144 assert.ErrorIs(t, client.UploadTraces(t.Context(), nil), errShutdown)
145 }
146
147 func TestExportContextHonorsParentDeadline(t *testing.T) {
148 now := time.Now()
149 ctx, cancel := context.WithDeadline(t.Context(), now)
150 t.Cleanup(cancel)
151
152 // Without a client timeout, the parent deadline should be used.
153 client := newClient(WithTimeout(0))
154 eCtx, eCancel := client.exportContext(ctx)
155 t.Cleanup(eCancel)
156
157 deadline, ok := eCtx.Deadline()
158 assert.True(t, ok, "deadline not propagated to child context")
159 assert.Equal(t, now, deadline)
160 }
161
162 func TestExportContextHonorsClientTimeout(t *testing.T) {
163 // Setting a timeout should ensure a deadline is set on the context.
164 client := newClient(WithTimeout(1 * time.Second))
165 ctx, cancel := client.exportContext(t.Context())
166 t.Cleanup(cancel)
167
168 _, ok := ctx.Deadline()
169 assert.True(t, ok, "timeout not set as deadline for child context")
170 }
171
172 func TestExportContextLinksStopSignal(t *testing.T) {
173 rootCtx := context.Background() //nolint:usetesting // used to assert Stop
174
175 client := newClient(WithInsecure())
176 t.Cleanup(func() { require.NoError(t, client.Stop(rootCtx)) })
177 require.NoError(t, client.Start(rootCtx))
178
179 ctx, cancel := client.exportContext(rootCtx)
180 t.Cleanup(cancel)
181
182 require.False(t, func() bool {
183 select {
184 case <-ctx.Done():
185 return true
186 default:
187 }
188 return false
189 }(), "context should not be done prior to canceling it")
190
191 // The client.stopFunc cancels the client.stopCtx. This should have been
192 // setup as a parent of ctx. Therefore, it should cancel ctx as well.
193 client.stopFunc()
194
195 // Assert this with Eventually to account for goroutine scheduler timing.
196 assert.Eventually(t, func() bool {
197 select {
198 case <-ctx.Done():
199 return true
200 default:
201 }
202 return false
203 }, 10*time.Second, time.Microsecond)
204 }
205
206 func TestWithEndpointWithEnv(t *testing.T) {
207 testCases := []struct {
208 name string
209 options []Option
210 envs map[string]string
211 want string
212 }{
213 {
214 name: "WithEndpointURL last",
215 options: []Option{
216 WithEndpoint("foo"),
217 WithEndpointURL("http://bar:8080/path"),
218 },
219 want: "bar:8080",
220 },
221 {
222 name: "WithEndpoint last",
223 options: []Option{
224 WithEndpointURL("http://bar:8080/path"),
225 WithEndpoint("foo"),
226 },
227 want: "foo",
228 },
229 {
230 name: "OTEL_EXPORTER_OTLP_ENDPOINT only",
231 envs: map[string]string{
232 "OTEL_EXPORTER_OTLP_ENDPOINT": "foo2",
233 },
234 want: "foo2",
235 },
236 {
237 name: "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT only",
238 envs: map[string]string{
239 "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT": "bar2",
240 },
241 want: "bar2",
242 },
243 {
244 name: "both OTEL_EXPORTER_OTLP_ENDPOINT and OTEL_EXPORTER_OTLP_TRACES_ENDPOINT",
245 envs: map[string]string{
246 "OTEL_EXPORTER_OTLP_ENDPOINT": "foo2",
247 "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT": "bar2",
248 },
249 want: "bar2",
250 },
251 {
252 name: "both options and envs",
253 envs: map[string]string{
254 "OTEL_EXPORTER_OTLP_ENDPOINT": "foo2",
255 "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT": "bar2",
256 },
257 options: []Option{
258 WithEndpointURL("http://bar:8080/path"),
259 WithEndpoint("foo"),
260 },
261 want: "foo",
262 },
263 {
264 name: "both options and envs",
265 envs: map[string]string{
266 "OTEL_EXPORTER_OTLP_ENDPOINT": "foo2",
267 "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT": "bar2",
268 },
269 options: []Option{
270 WithEndpoint("foo"),
271 WithEndpointURL("http://bar:8080/path"),
272 },
273 want: "bar:8080",
274 },
275 }
276
277 for _, tc := range testCases {
278 t.Run(tc.name, func(t *testing.T) {
279 for key, value := range tc.envs {
280 t.Setenv(key, value)
281 }
282
283 client := newClient(tc.options...)
284
285 assert.Equal(t, tc.want, client.endpoint)
286 })
287 }
288 }