forked from open-telemetry/opentelemetry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexporter_test.go
122 lines (100 loc) · 2.71 KB
/
exporter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package otlploghttp
import (
"context"
"errors"
"runtime"
"sync"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/sdk/log"
logpb "go.opentelemetry.io/proto/otlp/logs/v1"
)
func TestExporterExportErrors(t *testing.T) {
errUpload := errors.New("upload")
c := &client{
uploadLogs: func(context.Context, []*logpb.ResourceLogs) error {
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{
uploadLogs: func(context.Context, []*logpb.ResourceLogs) error {
uploads++
return nil
},
}
orig := transformResourceLogs
var got []log.Record
transformResourceLogs = func(r []log.Record) []*logpb.ResourceLogs {
got = r
return make([]*logpb.ResourceLogs, 1)
}
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")
}
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")
}
func TestExporterForceFlush(t *testing.T) {
ctx := context.Background()
e, err := New(ctx)
require.NoError(t, err, "New")
assert.NoError(t, e.ForceFlush(ctx), "ForceFlush")
}
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()
}