2024-04-08 00:47:11 -07:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package otlploghttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-04-10 07:47:24 -07:00
|
|
|
"errors"
|
2024-04-10 07:11:21 -07:00
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2024-04-08 00:47:11 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-04-08 09:14:58 -07:00
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/sdk/log"
|
2024-04-22 22:12:25 -07:00
|
|
|
logpb "go.opentelemetry.io/proto/otlp/logs/v1"
|
2024-04-08 00:47:11 -07:00
|
|
|
)
|
|
|
|
|
2024-04-10 07:47:24 -07:00
|
|
|
func TestExporterExportErrors(t *testing.T) {
|
2024-04-12 07:28:25 -07:00
|
|
|
errUpload := errors.New("upload")
|
2024-04-10 07:47:24 -07:00
|
|
|
c := &client{
|
2024-04-12 07:28:25 -07:00
|
|
|
uploadLogs: func(context.Context, []*logpb.ResourceLogs) error {
|
2024-04-10 07:47:24 -07:00
|
|
|
return errUpload
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
e, err := newExporter(c, config{})
|
|
|
|
require.NoError(t, err, "New")
|
|
|
|
|
|
|
|
err = e.Export(context.Background(), make([]log.Record, 1))
|
|
|
|
assert.ErrorIs(t, err, errUpload)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExporterExport(t *testing.T) {
|
|
|
|
var uploads int
|
|
|
|
c := &client{
|
2024-04-12 07:28:25 -07:00
|
|
|
uploadLogs: func(context.Context, []*logpb.ResourceLogs) error {
|
2024-04-10 07:47:24 -07:00
|
|
|
uploads++
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
orig := transformResourceLogs
|
|
|
|
var got []log.Record
|
2024-04-12 07:28:25 -07:00
|
|
|
transformResourceLogs = func(r []log.Record) []*logpb.ResourceLogs {
|
2024-04-10 07:47:24 -07:00
|
|
|
got = r
|
2024-04-12 07:28:25 -07:00
|
|
|
return make([]*logpb.ResourceLogs, 1)
|
2024-04-10 07:47:24 -07:00
|
|
|
}
|
|
|
|
t.Cleanup(func() { transformResourceLogs = orig })
|
|
|
|
|
|
|
|
e, err := newExporter(c, config{})
|
|
|
|
require.NoError(t, err, "New")
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
want := make([]log.Record, 1)
|
|
|
|
assert.NoError(t, e.Export(ctx, want))
|
|
|
|
|
|
|
|
assert.Equal(t, 1, uploads, "client UploadLogs calls")
|
|
|
|
assert.Equal(t, want, got, "transformed log records")
|
|
|
|
}
|
|
|
|
|
2024-04-08 09:14:58 -07:00
|
|
|
func TestExporterShutdown(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
e, err := New(ctx)
|
|
|
|
require.NoError(t, err, "New")
|
|
|
|
assert.NoError(t, e.Shutdown(ctx), "Shutdown Exporter")
|
|
|
|
|
|
|
|
// After Shutdown is called, calls to Export, Shutdown, or ForceFlush
|
|
|
|
// should perform no operation and return nil error.
|
|
|
|
r := make([]log.Record, 1)
|
|
|
|
assert.NoError(t, e.Export(ctx, r), "Export on Shutdown Exporter")
|
|
|
|
assert.NoError(t, e.ForceFlush(ctx), "ForceFlush on Shutdown Exporter")
|
|
|
|
assert.NoError(t, e.Shutdown(ctx), "Shutdown on Shutdown Exporter")
|
|
|
|
}
|
|
|
|
|
2024-04-08 00:47:11 -07:00
|
|
|
func TestExporterForceFlush(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
e, err := New(ctx)
|
|
|
|
require.NoError(t, err, "New")
|
|
|
|
|
|
|
|
assert.NoError(t, e.ForceFlush(ctx), "ForceFlush")
|
|
|
|
}
|
2024-04-10 07:11:21 -07:00
|
|
|
|
|
|
|
func TestExporterConcurrentSafe(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
e, err := New(ctx)
|
|
|
|
require.NoError(t, err, "newExporter")
|
|
|
|
|
|
|
|
const goroutines = 10
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
runs := new(uint64)
|
|
|
|
for i := 0; i < goroutines; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
r := make([]log.Record, 1)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
_ = e.Export(ctx, r)
|
|
|
|
_ = e.ForceFlush(ctx)
|
|
|
|
atomic.AddUint64(runs, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for atomic.LoadUint64(runs) == 0 {
|
|
|
|
runtime.Gosched()
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = e.Shutdown(ctx)
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
|
|
|
}
|