Debian Codegraph @

sdk/log/exporter_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package log
5
6 import (
7 "context"
8 "io"
9 stdlog "log"
10 "slices"
11 "sync"
12 "sync/atomic"
13 "testing"
14 "time"
15
16 "github.com/stretchr/testify/assert"
17 "github.com/stretchr/testify/require"
18
19 "go.opentelemetry.io/otel"
20 "go.opentelemetry.io/otel/log"
21 )
22
23 type instruction struct {
24 Record *[]Record
25 Flush chan [][]Record
26 }
27
28 type testExporter struct {
29 // Err is the error returned by all methods of the testExporter.
30 Err error
31 // ExportTrigger is read from prior to returning from the Export method if
32 // non-nil.
33 ExportTrigger chan struct{}
34
35 // Counts of method calls.
36 exportN, shutdownN, forceFlushN atomic.Int32
37
38 stopped atomic.Bool
39 inputMu sync.Mutex
40 input chan instruction
41 done chan struct{}
42 }
43
44 func newTestExporter(err error) *testExporter {
45 e := &testExporter{
46 Err: err,
47 input: make(chan instruction),
48 }
49 e.done = run(e.input)
50
51 return e
52 }
53
54 func run(input chan instruction) chan struct{} {
55 done := make(chan struct{})
56 go func() {
57 defer close(done)
58
59 var records [][]Record
60 for in := range input {
61 if in.Record != nil {
62 records = append(records, *in.Record)
63 }
64 if in.Flush != nil {
65 cp := slices.Clone(records)
66 records = records[:0]
67 in.Flush <- cp
68 }
69 }
70 }()
71 return done
72 }
73
74 func (e *testExporter) Records() [][]Record {
75 out := make(chan [][]Record, 1)
76 e.input <- instruction{Flush: out}
77 return <-out
78 }
79
80 func (e *testExporter) Export(ctx context.Context, r []Record) error {
81 e.exportN.Add(1)
82 if e.ExportTrigger != nil {
83 select {
84 case <-e.ExportTrigger:
85 case <-ctx.Done():
86 return ctx.Err()
87 }
88 }
89 e.inputMu.Lock()
90 defer e.inputMu.Unlock()
91 if !e.stopped.Load() {
92 e.input <- instruction{Record: &r}
93 }
94 return e.Err
95 }
96
97 func (e *testExporter) ExportN() int {
98 return int(e.exportN.Load())
99 }
100
101 func (e *testExporter) Stop() {
102 if e.stopped.Swap(true) {
103 return
104 }
105 e.inputMu.Lock()
106 defer e.inputMu.Unlock()
107
108 close(e.input)
109 <-e.done
110 }
111
112 func (e *testExporter) Shutdown(context.Context) error {
113 e.shutdownN.Add(1)
114 return e.Err
115 }
116
117 func (e *testExporter) ShutdownN() int {
118 return int(e.shutdownN.Load())
119 }
120
121 func (e *testExporter) ForceFlush(context.Context) error {
122 e.forceFlushN.Add(1)
123 return e.Err
124 }
125
126 func (e *testExporter) ForceFlushN() int {
127 return int(e.forceFlushN.Load())
128 }
129
130 func TestChunker(t *testing.T) {
131 t.Run("ZeroSize", func(t *testing.T) {
132 exp := newTestExporter(nil)
133 t.Cleanup(exp.Stop)
134 c := newChunkExporter(exp, 0)
135 const size = 100
136 _ = c.Export(t.Context(), make([]Record, size))
137
138 assert.Equal(t, 1, exp.ExportN())
139 records := exp.Records()
140 assert.Len(t, records, 1)
141 assert.Len(t, records[0], size)
142 })
143
144 t.Run("ForceFlush", func(t *testing.T) {
145 exp := newTestExporter(nil)
146 t.Cleanup(exp.Stop)
147 c := newChunkExporter(exp, 0)
148 _ = c.ForceFlush(t.Context())
149 assert.Equal(t, 1, exp.ForceFlushN(), "ForceFlush not passed through")
150 })
151
152 t.Run("Shutdown", func(t *testing.T) {
153 exp := newTestExporter(nil)
154 t.Cleanup(exp.Stop)
155 c := newChunkExporter(exp, 0)
156 _ = c.Shutdown(t.Context())
157 assert.Equal(t, 1, exp.ShutdownN(), "Shutdown not passed through")
158 })
159
160 t.Run("Chunk", func(t *testing.T) {
161 exp := newTestExporter(nil)
162 t.Cleanup(exp.Stop)
163 c := newChunkExporter(exp, 10)
164 assert.NoError(t, c.Export(t.Context(), make([]Record, 5)))
165 assert.NoError(t, c.Export(t.Context(), make([]Record, 25)))
166
167 wantLens := []int{5, 10, 10, 5}
168 records := exp.Records()
169 require.Len(t, records, len(wantLens), "chunks")
170 for i, n := range wantLens {
171 assert.Lenf(t, records[i], n, "chunk %d", i)
172 }
173 })
174
175 t.Run("ExportError", func(t *testing.T) {
176 exp := newTestExporter(assert.AnError)
177 t.Cleanup(exp.Stop)
178 c := newChunkExporter(exp, 0)
179 ctx := t.Context()
180 records := make([]Record, 25)
181 err := c.Export(ctx, records)
182 assert.ErrorIs(t, err, assert.AnError, "no chunking")
183
184 c = newChunkExporter(exp, 10)
185 err = c.Export(ctx, records)
186 assert.ErrorIs(t, err, assert.AnError, "with chunking")
187 })
188 }
189
190 func TestExportSync(t *testing.T) {
191 eventuallyDone := func(t *testing.T, done chan struct{}) {
192 assert.Eventually(t, func() bool {
193 select {
194 case <-done:
195 return true
196 default:
197 return false
198 }
199 }, 2*time.Second, time.Microsecond)
200 }
201
202 t.Run("ErrorHandler", func(t *testing.T) {
203 var got error
204 handler := otel.ErrorHandlerFunc(func(err error) { got = err })
205 otel.SetErrorHandler(handler)
206 t.Cleanup(func() {
207 l := stdlog.New(io.Discard, "", stdlog.LstdFlags)
208 otel.SetErrorHandler(otel.ErrorHandlerFunc(func(err error) {
209 l.Print(err)
210 }))
211 })
212
213 in := make(chan exportData, 1)
214 exp := newTestExporter(assert.AnError)
215 t.Cleanup(exp.Stop)
216 done := exportSync(in, exp)
217
218 var wg sync.WaitGroup
219 wg.Go(func() {
220 in <- exportData{
221 ctx: t.Context(),
222 records: make([]Record, 1),
223 }
224 })
225
226 wg.Wait()
227 close(in)
228 eventuallyDone(t, done)
229
230 assert.ErrorIs(t, got, assert.AnError, "error not passed to ErrorHandler")
231 })
232
233 t.Run("ConcurrentSafe", func(t *testing.T) {
234 in := make(chan exportData, 1)
235 exp := newTestExporter(assert.AnError)
236 t.Cleanup(exp.Stop)
237 done := exportSync(in, exp)
238
239 const goRoutines = 10
240 var wg sync.WaitGroup
241 wg.Add(goRoutines)
242 for i := range goRoutines {
243 go func(n int) {
244 defer wg.Done()
245
246 var r Record
247 r.SetBody(log.IntValue(n))
248
249 resp := make(chan error, 1)
250 in <- exportData{
251 ctx: t.Context(),
252 records: []Record{r},
253 respCh: resp,
254 }
255
256 assert.ErrorIs(t, <-resp, assert.AnError)
257 }(i)
258 }
259
260 // Empty records should be ignored.
261 in <- exportData{ctx: t.Context()}
262
263 wg.Wait()
264
265 close(in)
266 eventuallyDone(t, done)
267
268 assert.Equal(t, goRoutines, exp.ExportN(), "Export calls")
269
270 want := make([]log.Value, goRoutines)
271 for i := range want {
272 want[i] = log.IntValue(i)
273 }
274 records := exp.Records()
275 got := make([]log.Value, len(records))
276 for i := range got {
277 if assert.Len(t, records[i], 1, "number of records exported") {
278 got[i] = records[i][0].Body()
279 }
280 }
281 assert.ElementsMatch(t, want, got, "record bodies")
282 })
283 }
284
285 func TestTimeoutExporter(t *testing.T) {
286 t.Run("ZeroTimeout", func(t *testing.T) {
287 exp := newTestExporter(nil)
288 t.Cleanup(exp.Stop)
289 e := newTimeoutExporter(exp, 0)
290 assert.Same(t, exp, e)
291 })
292
293 t.Run("Timeout", func(t *testing.T) {
294 trigger := make(chan struct{})
295 t.Cleanup(func() { close(trigger) })
296
297 exp := newTestExporter(nil)
298 t.Cleanup(exp.Stop)
299 exp.ExportTrigger = trigger
300 e := newTimeoutExporter(exp, time.Nanosecond)
301
302 out := make(chan error, 1)
303 go func() {
304 out <- e.Export(t.Context(), make([]Record, 1))
305 }()
306
307 var err error
308 assert.Eventually(t, func() bool {
309 select {
310 case err = <-out:
311 return true
312 default:
313 return false
314 }
315 }, 2*time.Second, time.Microsecond)
316
317 assert.ErrorIs(t, err, context.DeadlineExceeded)
318 close(out)
319 })
320 }
321
322 func TestBufferExporter(t *testing.T) {
323 t.Run("ConcurrentSafe", func(t *testing.T) {
324 const goRoutines = 10
325
326 exp := newTestExporter(nil)
327 t.Cleanup(exp.Stop)
328 e := newBufferExporter(exp, goRoutines)
329
330 ctx := t.Context()
331 records := make([]Record, 10)
332
333 stop := make(chan struct{})
334 var wg sync.WaitGroup
335 for range goRoutines {
336 wg.Go(func() {
337 for {
338 select {
339 case <-stop:
340 return
341 default:
342 _ = e.EnqueueExport(records)
343 _ = e.Export(ctx, records)
344 _ = e.ForceFlush(ctx)
345 }
346 }
347 })
348 }
349
350 assert.Eventually(t, func() bool {
351 return exp.ExportN() > 0
352 }, 2*time.Second, time.Microsecond)
353
354 assert.NoError(t, e.Shutdown(ctx))
355 close(stop)
356 wg.Wait()
357 })
358
359 t.Run("Shutdown", func(t *testing.T) {
360 t.Run("Multiple", func(t *testing.T) {
361 exp := newTestExporter(nil)
362 t.Cleanup(exp.Stop)
363 e := newBufferExporter(exp, 1)
364
365 assert.NoError(t, e.Shutdown(t.Context()))
366 assert.Equal(t, 1, exp.ShutdownN(), "first Shutdown")
367
368 assert.NoError(t, e.Shutdown(t.Context()))
369 assert.Equal(t, 1, exp.ShutdownN(), "second Shutdown")
370 })
371
372 t.Run("ContextCancelled", func(t *testing.T) {
373 // Discard error logs.
374 defer func(orig otel.ErrorHandler) {
375 otel.SetErrorHandler(orig)
376 }(otel.GetErrorHandler())
377 handler := otel.ErrorHandlerFunc(func(error) {})
378 otel.SetErrorHandler(handler)
379
380 exp := newTestExporter(assert.AnError)
381 t.Cleanup(exp.Stop)
382
383 trigger := make(chan struct{})
384 exp.ExportTrigger = trigger
385 t.Cleanup(func() { close(trigger) })
386 e := newBufferExporter(exp, 1)
387
388 // Make sure there is something to flush.
389 require.True(t, e.EnqueueExport(make([]Record, 1)))
390
391 ctx, cancel := context.WithCancel(t.Context())
392 cancel()
393
394 err := e.Shutdown(ctx)
395 assert.ErrorIs(t, err, context.Canceled)
396 assert.ErrorIs(t, err, assert.AnError)
397 })
398
399 t.Run("Error", func(t *testing.T) {
400 exp := newTestExporter(assert.AnError)
401 t.Cleanup(exp.Stop)
402
403 e := newBufferExporter(exp, 1)
404 assert.ErrorIs(t, e.Shutdown(t.Context()), assert.AnError)
405 })
406 })
407
408 t.Run("ForceFlush", func(t *testing.T) {
409 t.Run("Multiple", func(t *testing.T) {
410 exp := newTestExporter(nil)
411 t.Cleanup(exp.Stop)
412 e := newBufferExporter(exp, 2)
413
414 ctx := t.Context()
415 records := make([]Record, 1)
416 require.NoError(t, e.enqueue(ctx, records, nil), "enqueue")
417
418 assert.NoError(t, e.ForceFlush(ctx), "ForceFlush records")
419 assert.Equal(t, 1, exp.ExportN(), "Export number incremented")
420 assert.Len(t, exp.Records(), 1, "exported Record batches")
421
422 // Nothing to flush.
423 assert.NoError(t, e.ForceFlush(ctx), "ForceFlush empty")
424 assert.Equal(t, 1, exp.ExportN(), "Export number changed")
425 assert.Empty(t, exp.Records(), "exported non-zero Records")
426 })
427
428 t.Run("ContextCancelled", func(t *testing.T) {
429 exp := newTestExporter(nil)
430 t.Cleanup(exp.Stop)
431
432 trigger := make(chan struct{})
433 exp.ExportTrigger = trigger
434 t.Cleanup(func() { close(trigger) })
435 e := newBufferExporter(exp, 1)
436
437 ctx, cancel := context.WithCancel(t.Context())
438 require.True(t, e.EnqueueExport(make([]Record, 1)))
439
440 got := make(chan error, 1)
441 go func() { got <- e.ForceFlush(ctx) }()
442 require.Eventually(t, func() bool {
443 return exp.ExportN() > 0
444 }, 2*time.Second, time.Microsecond)
445 cancel() // Canceled before export response.
446 err := <-got
447 assert.ErrorIs(t, err, context.Canceled, "enqueued")
448 _ = e.Shutdown(ctx)
449
450 // Zero length buffer
451 e = newBufferExporter(exp, 0)
452 assert.ErrorIs(t, e.ForceFlush(ctx), context.Canceled, "not enqueued")
453 })
454
455 t.Run("Error", func(t *testing.T) {
456 exp := newTestExporter(assert.AnError)
457 t.Cleanup(exp.Stop)
458
459 e := newBufferExporter(exp, 1)
460 assert.ErrorIs(t, e.ForceFlush(t.Context()), assert.AnError)
461 })
462
463 t.Run("Stopped", func(t *testing.T) {
464 exp := newTestExporter(nil)
465 t.Cleanup(exp.Stop)
466
467 e := newBufferExporter(exp, 1)
468
469 ctx := t.Context()
470 _ = e.Shutdown(ctx)
471 assert.NoError(t, e.ForceFlush(ctx))
472 })
473 })
474
475 t.Run("Export", func(t *testing.T) {
476 t.Run("ZeroRecords", func(t *testing.T) {
477 exp := newTestExporter(nil)
478 t.Cleanup(exp.Stop)
479 e := newBufferExporter(exp, 1)
480
481 assert.NoError(t, e.Export(t.Context(), nil))
482 assert.Equal(t, 0, exp.ExportN())
483 })
484
485 t.Run("Multiple", func(t *testing.T) {
486 exp := newTestExporter(nil)
487 t.Cleanup(exp.Stop)
488 e := newBufferExporter(exp, 1)
489
490 ctx := t.Context()
491 records := make([]Record, 1)
492 records[0].SetBody(log.BoolValue(true))
493
494 assert.NoError(t, e.Export(ctx, records))
495
496 n := exp.ExportN()
497 assert.Equal(t, 1, n, "first Export number")
498 assert.Equal(t, [][]Record{records}, exp.Records())
499
500 assert.NoError(t, e.Export(ctx, records))
501 assert.Equal(t, n+1, exp.ExportN(), "second Export number")
502 assert.Equal(t, [][]Record{records}, exp.Records())
503 })
504
505 t.Run("ContextCancelled", func(t *testing.T) {
506 exp := newTestExporter(nil)
507 t.Cleanup(exp.Stop)
508
509 trigger := make(chan struct{})
510 exp.ExportTrigger = trigger
511 t.Cleanup(func() { close(trigger) })
512 e := newBufferExporter(exp, 1)
513
514 records := make([]Record, 1)
515 ctx, cancel := context.WithCancel(t.Context())
516
517 got := make(chan error, 1)
518 go func() { got <- e.Export(ctx, records) }()
519 require.Eventually(t, func() bool {
520 return exp.ExportN() > 0
521 }, 2*time.Second, time.Microsecond)
522 cancel() // Canceled before export response.
523 err := <-got
524 assert.ErrorIs(t, err, context.Canceled, "enqueued")
525 _ = e.Shutdown(ctx)
526
527 // Zero length buffer
528 e = newBufferExporter(exp, 0)
529 assert.ErrorIs(t, e.Export(ctx, records), context.Canceled, "not enqueued")
530 })
531
532 t.Run("Error", func(t *testing.T) {
533 exp := newTestExporter(assert.AnError)
534 t.Cleanup(exp.Stop)
535
536 e := newBufferExporter(exp, 1)
537 ctx, records := t.Context(), make([]Record, 1)
538 assert.ErrorIs(t, e.Export(ctx, records), assert.AnError)
539 })
540
541 t.Run("Stopped", func(t *testing.T) {
542 exp := newTestExporter(nil)
543 t.Cleanup(exp.Stop)
544
545 e := newBufferExporter(exp, 1)
546
547 ctx := t.Context()
548 _ = e.Shutdown(ctx)
549 assert.NoError(t, e.Export(ctx, make([]Record, 1)))
550 assert.Equal(t, 0, exp.ExportN(), "Export called")
551 })
552 })
553
554 t.Run("EnqueueExport", func(t *testing.T) {
555 t.Run("ZeroRecords", func(t *testing.T) {
556 exp := newTestExporter(nil)
557 t.Cleanup(exp.Stop)
558 e := newBufferExporter(exp, 1)
559
560 assert.True(t, e.EnqueueExport(nil))
561 e.ForceFlush(t.Context())
562 assert.Equal(t, 0, exp.ExportN(), "empty batch enqueued")
563 })
564
565 t.Run("Multiple", func(t *testing.T) {
566 exp := newTestExporter(nil)
567 t.Cleanup(exp.Stop)
568 e := newBufferExporter(exp, 2)
569
570 records := make([]Record, 1)
571 records[0].SetBody(log.BoolValue(true))
572
573 assert.True(t, e.EnqueueExport(records))
574 assert.True(t, e.EnqueueExport(records))
575 e.ForceFlush(t.Context())
576
577 n := exp.ExportN()
578 assert.Equal(t, 2, n, "Export number")
579 assert.Equal(t, [][]Record{records, records}, exp.Records())
580 })
581
582 t.Run("Stopped", func(t *testing.T) {
583 exp := newTestExporter(nil)
584 t.Cleanup(exp.Stop)
585 e := newBufferExporter(exp, 1)
586
587 _ = e.Shutdown(t.Context())
588 assert.True(t, e.EnqueueExport(make([]Record, 1)))
589 })
590 })
591 }