|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package otlplogfile // import "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlplogfile" |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "path" |
| 10 | + "runtime" |
| 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/log" |
| 20 | + |
| 21 | + sdklog "go.opentelemetry.io/otel/sdk/log" |
| 22 | +) |
| 23 | + |
| 24 | +// tempFile creates a temporary file for the given test case and returns its path on disk. |
| 25 | +// The file is automatically cleaned up when the test ends. |
| 26 | +func tempFile(tb testing.TB) string { |
| 27 | + f, err := os.CreateTemp(tb.TempDir(), tb.Name()) |
| 28 | + assert.NoError(tb, err, "must not error when creating temporary file") |
| 29 | + tb.Cleanup(func() { |
| 30 | + assert.NoError(tb, os.RemoveAll(path.Dir(f.Name())), "must clean up files after being written") |
| 31 | + }) |
| 32 | + return f.Name() |
| 33 | +} |
| 34 | + |
| 35 | +// makeRecords is a helper function to generate an array of log record with the desired size. |
| 36 | +func makeRecords(count int, message string) []sdklog.Record { |
| 37 | + var records []sdklog.Record |
| 38 | + for i := 0; i < count; i++ { |
| 39 | + r := sdklog.Record{} |
| 40 | + r.SetSeverityText("INFO") |
| 41 | + r.SetSeverity(log.SeverityInfo) |
| 42 | + r.SetBody(log.StringValue(message)) |
| 43 | + r.SetTimestamp(time.Now()) |
| 44 | + r.SetObservedTimestamp(time.Now()) |
| 45 | + records = append(records, r) |
| 46 | + } |
| 47 | + return records |
| 48 | +} |
| 49 | + |
| 50 | +func TestExporter(t *testing.T) { |
| 51 | + filepath := tempFile(t) |
| 52 | + records := makeRecords(1, "hello, world!") |
| 53 | + |
| 54 | + exporter, err := New(WithPath(filepath)) |
| 55 | + assert.NoError(t, err) |
| 56 | + t.Cleanup(func() { |
| 57 | + assert.NoError(t, exporter.Shutdown(context.TODO())) |
| 58 | + }) |
| 59 | + |
| 60 | + err = exporter.Export(context.TODO(), records) |
| 61 | + assert.NoError(t, err) |
| 62 | + err = exporter.ForceFlush(context.TODO()) |
| 63 | + assert.NoError(t, err) |
| 64 | +} |
| 65 | + |
| 66 | +func TestExporterConcurrentSafe(t *testing.T) { |
| 67 | + filepath := tempFile(t) |
| 68 | + exporter, err := New(WithPath(filepath)) |
| 69 | + require.NoError(t, err, "New()") |
| 70 | + |
| 71 | + const goroutines = 10 |
| 72 | + |
| 73 | + var wg sync.WaitGroup |
| 74 | + ctx, cancel := context.WithCancel(context.Background()) |
| 75 | + runs := new(uint64) |
| 76 | + for i := 0; i < goroutines; i++ { |
| 77 | + wg.Add(1) |
| 78 | + i := i |
| 79 | + go func() { |
| 80 | + defer wg.Done() |
| 81 | + for { |
| 82 | + select { |
| 83 | + case <-ctx.Done(): |
| 84 | + return |
| 85 | + default: |
| 86 | + _ = exporter.Export(ctx, makeRecords(1, fmt.Sprintf("log from goroutine %d", i))) |
| 87 | + _ = exporter.ForceFlush(ctx) |
| 88 | + atomic.AddUint64(runs, 1) |
| 89 | + } |
| 90 | + } |
| 91 | + }() |
| 92 | + } |
| 93 | + |
| 94 | + for atomic.LoadUint64(runs) == 0 { |
| 95 | + runtime.Gosched() |
| 96 | + } |
| 97 | + |
| 98 | + assert.NoError(t, exporter.Shutdown(ctx), "must not error when shutting down") |
| 99 | + cancel() |
| 100 | + wg.Wait() |
| 101 | +} |
| 102 | + |
| 103 | +func BenchmarkExporter(b *testing.B) { |
| 104 | + for _, logCount := range []int{ |
| 105 | + 10, |
| 106 | + 100, |
| 107 | + 500, |
| 108 | + 1000, |
| 109 | + } { |
| 110 | + records := makeRecords(logCount, "benchmark") |
| 111 | + |
| 112 | + for name, interval := range map[string]time.Duration{ |
| 113 | + "no-flush": 0, |
| 114 | + "flush-10ms": 10 * time.Millisecond, |
| 115 | + "flush-100ms": 100 * time.Millisecond, |
| 116 | + "flush-1s": time.Second, |
| 117 | + "flush-10s": 10 * time.Second, |
| 118 | + } { |
| 119 | + filepath := tempFile(b) |
| 120 | + exporter, err := New(WithPath(filepath), WithFlushInterval(interval)) |
| 121 | + require.NoError(b, err, "must not error when calling New()") |
| 122 | + |
| 123 | + b.Run(fmt.Sprintf("%s/%d-logs", name, logCount), func(b *testing.B) { |
| 124 | + b.ReportAllocs() |
| 125 | + b.ResetTimer() |
| 126 | + |
| 127 | + for i := 0; i < b.N; i++ { |
| 128 | + if err := exporter.Export(context.Background(), records); err != nil { |
| 129 | + b.Fatalf("failed to export records: %v", err) |
| 130 | + } |
| 131 | + } |
| 132 | + }) |
| 133 | + |
| 134 | + if err := exporter.Shutdown(context.Background()); err != nil { |
| 135 | + b.Fatalf("failed to shutdown exporter: %v", err) |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments