| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otlploghttp |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"runtime" |
| 10 |
"sync" |
| 11 |
"sync/atomic" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
logpb "go.opentelemetry.io/proto/otlp/logs/v1" |
| 17 |
|
| 18 |
"go.opentelemetry.io/otel/sdk/log" |
| 19 |
) |
| 20 |
|
| 21 |
func TestExporterExportErrors(t *testing.T) { |
| 22 |
errUpload := errors.New("upload") |
| 23 |
c := &client{ |
| 24 |
uploadLogs: func(context.Context, []*logpb.ResourceLogs) error { |
| 25 |
return errUpload |
| 26 |
}, |
| 27 |
} |
| 28 |
|
| 29 |
e, err := newExporter(c, config{}) |
| 30 |
require.NoError(t, err, "New") |
| 31 |
|
| 32 |
err = e.Export(t.Context(), make([]log.Record, 1)) |
| 33 |
assert.ErrorIs(t, err, errUpload) |
| 34 |
} |
| 35 |
|
| 36 |
func TestExporterExport(t *testing.T) { |
| 37 |
var uploads int |
| 38 |
c := &client{ |
| 39 |
uploadLogs: func(context.Context, []*logpb.ResourceLogs) error { |
| 40 |
uploads++ |
| 41 |
return nil |
| 42 |
}, |
| 43 |
} |
| 44 |
|
| 45 |
orig := transformResourceLogs |
| 46 |
var got []log.Record |
| 47 |
transformResourceLogs = func(r []log.Record) []*logpb.ResourceLogs { |
| 48 |
got = r |
| 49 |
return make([]*logpb.ResourceLogs, 1) |
| 50 |
} |
| 51 |
t.Cleanup(func() { transformResourceLogs = orig }) |
| 52 |
|
| 53 |
e, err := newExporter(c, config{}) |
| 54 |
require.NoError(t, err, "New") |
| 55 |
|
| 56 |
ctx := t.Context() |
| 57 |
want := make([]log.Record, 1) |
| 58 |
assert.NoError(t, e.Export(ctx, want)) |
| 59 |
|
| 60 |
assert.Equal(t, 1, uploads, "client UploadLogs calls") |
| 61 |
assert.Equal(t, want, got, "transformed log records") |
| 62 |
} |
| 63 |
|
| 64 |
func TestExporterShutdown(t *testing.T) { |
| 65 |
ctx := t.Context() |
| 66 |
e, err := New(ctx) |
| 67 |
require.NoError(t, err, "New") |
| 68 |
assert.NoError(t, e.Shutdown(ctx), "Shutdown Exporter") |
| 69 |
|
| 70 |
// After Shutdown is called, calls to Export, Shutdown, or ForceFlush |
| 71 |
// should perform no operation and return nil error. |
| 72 |
r := make([]log.Record, 1) |
| 73 |
assert.NoError(t, e.Export(ctx, r), "Export on Shutdown Exporter") |
| 74 |
assert.NoError(t, e.ForceFlush(ctx), "ForceFlush on Shutdown Exporter") |
| 75 |
assert.NoError(t, e.Shutdown(ctx), "Shutdown on Shutdown Exporter") |
| 76 |
} |
| 77 |
|
| 78 |
func TestExporterForceFlush(t *testing.T) { |
| 79 |
ctx := t.Context() |
| 80 |
e, err := New(ctx) |
| 81 |
require.NoError(t, err, "New") |
| 82 |
|
| 83 |
assert.NoError(t, e.ForceFlush(ctx), "ForceFlush") |
| 84 |
} |
| 85 |
|
| 86 |
func TestExporterConcurrentSafe(t *testing.T) { |
| 87 |
ctx := t.Context() |
| 88 |
e, err := New(ctx) |
| 89 |
require.NoError(t, err, "newExporter") |
| 90 |
|
| 91 |
const goroutines = 10 |
| 92 |
|
| 93 |
var wg sync.WaitGroup |
| 94 |
ctx, cancel := context.WithCancel(t.Context()) |
| 95 |
var runs atomic.Uint64 |
| 96 |
for range goroutines { |
| 97 |
wg.Go(func() { |
| 98 |
r := make([]log.Record, 1) |
| 99 |
for { |
| 100 |
select { |
| 101 |
case <-ctx.Done(): |
| 102 |
return |
| 103 |
default: |
| 104 |
_ = e.Export(ctx, r) |
| 105 |
_ = e.ForceFlush(ctx) |
| 106 |
runs.Add(1) |
| 107 |
} |
| 108 |
} |
| 109 |
}) |
| 110 |
} |
| 111 |
|
| 112 |
for runs.Load() == 0 { |
| 113 |
runtime.Gosched() |
| 114 |
} |
| 115 |
|
| 116 |
_ = e.Shutdown(ctx) |
| 117 |
cancel() |
| 118 |
wg.Wait() |
| 119 |
} |