| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otlploggrpc |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"runtime" |
| 10 |
"sync" |
| 11 |
"sync/atomic" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc/internal" |
| 15 |
|
| 16 |
"github.com/stretchr/testify/assert" |
| 17 |
"github.com/stretchr/testify/require" |
| 18 |
collogpb "go.opentelemetry.io/proto/otlp/collector/logs/v1" |
| 19 |
logpb "go.opentelemetry.io/proto/otlp/logs/v1" |
| 20 |
|
| 21 |
"go.opentelemetry.io/otel/log" |
| 22 |
sdklog "go.opentelemetry.io/otel/sdk/log" |
| 23 |
) |
| 24 |
|
| 25 |
var records []sdklog.Record |
| 26 |
|
| 27 |
func init() { |
| 28 |
var r sdklog.Record |
| 29 |
r.SetTimestamp(ts) |
| 30 |
r.SetBody(log.StringValue("A")) |
| 31 |
records = append(records, r) |
| 32 |
|
| 33 |
r.SetBody(log.StringValue("B")) |
| 34 |
records = append(records, r) |
| 35 |
} |
| 36 |
|
| 37 |
type mockClient struct { |
| 38 |
err error |
| 39 |
|
| 40 |
uploads int |
| 41 |
} |
| 42 |
|
| 43 |
func (m *mockClient) UploadLogs(context.Context, []*logpb.ResourceLogs) error { |
| 44 |
m.uploads++ |
| 45 |
return m.err |
| 46 |
} |
| 47 |
|
| 48 |
func (m *mockClient) Shutdown(context.Context) error { |
| 49 |
return m.err |
| 50 |
} |
| 51 |
|
| 52 |
func TestExporterExport(t *testing.T) { |
| 53 |
errClient := errors.New("client") |
| 54 |
|
| 55 |
testCases := []struct { |
| 56 |
name string |
| 57 |
logs []sdklog.Record |
| 58 |
err error |
| 59 |
|
| 60 |
wantLogs []sdklog.Record |
| 61 |
wantErr error |
| 62 |
}{ |
| 63 |
{ |
| 64 |
name: "NoError", |
| 65 |
logs: make([]sdklog.Record, 2), |
| 66 |
wantLogs: make([]sdklog.Record, 2), |
| 67 |
}, |
| 68 |
{ |
| 69 |
name: "Error", |
| 70 |
logs: make([]sdklog.Record, 2), |
| 71 |
err: errClient, |
| 72 |
wantErr: errClient, |
| 73 |
}, |
| 74 |
} |
| 75 |
|
| 76 |
for _, tc := range testCases { |
| 77 |
t.Run(tc.name, func(t *testing.T) { |
| 78 |
orig := transformResourceLogs |
| 79 |
var got []sdklog.Record |
| 80 |
transformResourceLogs = func(r []sdklog.Record) []*logpb.ResourceLogs { |
| 81 |
got = r |
| 82 |
return make([]*logpb.ResourceLogs, len(r)) |
| 83 |
} |
| 84 |
t.Cleanup(func() { transformResourceLogs = orig }) |
| 85 |
|
| 86 |
mockCli := mockClient{err: tc.err} |
| 87 |
|
| 88 |
e := newExporter(&mockCli) |
| 89 |
|
| 90 |
err := e.Export(t.Context(), tc.logs) |
| 91 |
assert.Equal(t, tc.wantErr, err) |
| 92 |
assert.Equal(t, tc.logs, got) |
| 93 |
assert.Equal(t, 1, mockCli.uploads) |
| 94 |
}) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
func TestExporterShutdown(t *testing.T) { |
| 99 |
ctx := t.Context() |
| 100 |
e, err := New(ctx) |
| 101 |
require.NoError(t, err, "New") |
| 102 |
assert.NoError(t, e.Shutdown(ctx), "Shutdown Exporter") |
| 103 |
|
| 104 |
// After Shutdown is called, calls to Export, Shutdown, or ForceFlush |
| 105 |
// should perform no operation and return nil error. |
| 106 |
r := make([]sdklog.Record, 1) |
| 107 |
assert.NoError(t, e.Export(ctx, r), "Export on Shutdown Exporter") |
| 108 |
assert.NoError(t, e.ForceFlush(ctx), "ForceFlush on Shutdown Exporter") |
| 109 |
assert.NoError(t, e.Shutdown(ctx), "Shutdown on Shutdown Exporter") |
| 110 |
} |
| 111 |
|
| 112 |
func TestExporterForceFlush(t *testing.T) { |
| 113 |
ctx := t.Context() |
| 114 |
e, err := New(ctx) |
| 115 |
require.NoError(t, err, "New") |
| 116 |
|
| 117 |
assert.NoError(t, e.ForceFlush(ctx), "ForceFlush") |
| 118 |
} |
| 119 |
|
| 120 |
func TestExporterConcurrentSafe(t *testing.T) { |
| 121 |
e := newExporter(&mockClient{}) |
| 122 |
|
| 123 |
const goroutines = 10 |
| 124 |
|
| 125 |
var wg sync.WaitGroup |
| 126 |
ctx, cancel := context.WithCancel(t.Context()) |
| 127 |
var runs atomic.Uint64 |
| 128 |
for range goroutines { |
| 129 |
wg.Go(func() { |
| 130 |
r := make([]sdklog.Record, 1) |
| 131 |
for { |
| 132 |
select { |
| 133 |
case <-ctx.Done(): |
| 134 |
return |
| 135 |
default: |
| 136 |
_ = e.Export(ctx, r) |
| 137 |
_ = e.ForceFlush(ctx) |
| 138 |
runs.Add(1) |
| 139 |
} |
| 140 |
} |
| 141 |
}) |
| 142 |
} |
| 143 |
|
| 144 |
for runs.Load() == 0 { |
| 145 |
runtime.Gosched() |
| 146 |
} |
| 147 |
|
| 148 |
_ = e.Shutdown(ctx) |
| 149 |
cancel() |
| 150 |
wg.Wait() |
| 151 |
} |
| 152 |
|
| 153 |
// TestExporter runs integration test against the real OTLP collector. |
| 154 |
func TestExporter(t *testing.T) { |
| 155 |
t.Run("ExporterHonorsContextErrors", func(t *testing.T) { |
| 156 |
t.Run("Export", testCtxErrs(func() func(context.Context) error { |
| 157 |
c, _ := clientFactory(t, nil) |
| 158 |
e := newExporter(c) |
| 159 |
return func(ctx context.Context) error { |
| 160 |
return e.Export(ctx, []sdklog.Record{{}}) |
| 161 |
} |
| 162 |
})) |
| 163 |
|
| 164 |
t.Run("Shutdown", testCtxErrs(func() func(context.Context) error { |
| 165 |
c, _ := clientFactory(t, nil) |
| 166 |
e := newExporter(c) |
| 167 |
return e.Shutdown |
| 168 |
})) |
| 169 |
}) |
| 170 |
|
| 171 |
t.Run("Export", func(t *testing.T) { |
| 172 |
ctx := t.Context() |
| 173 |
c, coll := clientFactory(t, nil) |
| 174 |
e := newExporter(c) |
| 175 |
|
| 176 |
require.NoError(t, e.Export(ctx, records)) |
| 177 |
require.NoError(t, e.Shutdown(ctx)) |
| 178 |
got := coll.Collect().Dump() |
| 179 |
require.Len(t, got, 1, "upload of one ResourceLogs") |
| 180 |
require.Len(t, got[0].ScopeLogs, 1, "upload of one ScopeLogs") |
| 181 |
require.Len(t, got[0].ScopeLogs[0].LogRecords, 2, "upload of two ScopeLogs") |
| 182 |
|
| 183 |
// Check body |
| 184 |
assert.Equal(t, "A", got[0].ScopeLogs[0].LogRecords[0].Body.GetStringValue()) |
| 185 |
assert.Equal(t, "B", got[0].ScopeLogs[0].LogRecords[1].Body.GetStringValue()) |
| 186 |
}) |
| 187 |
|
| 188 |
t.Run("PartialSuccess", func(t *testing.T) { |
| 189 |
const n, msg = 2, "bad data" |
| 190 |
rCh := make(chan exportResult, 3) |
| 191 |
rCh <- exportResult{ |
| 192 |
Response: &collogpb.ExportLogsServiceResponse{ |
| 193 |
PartialSuccess: &collogpb.ExportLogsPartialSuccess{ |
| 194 |
RejectedLogRecords: n, |
| 195 |
ErrorMessage: msg, |
| 196 |
}, |
| 197 |
}, |
| 198 |
} |
| 199 |
rCh <- exportResult{ |
| 200 |
Response: &collogpb.ExportLogsServiceResponse{ |
| 201 |
PartialSuccess: &collogpb.ExportLogsPartialSuccess{ |
| 202 |
// Should not be logged. |
| 203 |
RejectedLogRecords: 0, |
| 204 |
ErrorMessage: "", |
| 205 |
}, |
| 206 |
}, |
| 207 |
} |
| 208 |
rCh <- exportResult{ |
| 209 |
Response: &collogpb.ExportLogsServiceResponse{}, |
| 210 |
} |
| 211 |
|
| 212 |
ctx := t.Context() |
| 213 |
c, _ := clientFactory(t, rCh) |
| 214 |
e := newExporter(c) |
| 215 |
|
| 216 |
assert.ErrorIs(t, e.Export(ctx, records), internal.PartialSuccess{}) |
| 217 |
assert.NoError(t, e.Export(ctx, records)) |
| 218 |
assert.NoError(t, e.Export(ctx, records)) |
| 219 |
}) |
| 220 |
} |