| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package log // import "go.opentelemetry.io/otel/sdk/log" |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"context" |
| 9 |
stdlog "log" |
| 10 |
"slices" |
| 11 |
"strconv" |
| 12 |
"strings" |
| 13 |
"sync" |
| 14 |
"testing" |
| 15 |
"time" |
| 16 |
"unsafe" |
| 17 |
|
| 18 |
"github.com/go-logr/stdr" |
| 19 |
"github.com/stretchr/testify/assert" |
| 20 |
"github.com/stretchr/testify/require" |
| 21 |
|
| 22 |
"go.opentelemetry.io/otel" |
| 23 |
"go.opentelemetry.io/otel/internal/global" |
| 24 |
"go.opentelemetry.io/otel/log" |
| 25 |
) |
| 26 |
|
| 27 |
type concurrentBuffer struct { |
| 28 |
b bytes.Buffer |
| 29 |
m sync.Mutex |
| 30 |
} |
| 31 |
|
| 32 |
func (b *concurrentBuffer) Write(p []byte) (n int, err error) { |
| 33 |
b.m.Lock() |
| 34 |
defer b.m.Unlock() |
| 35 |
return b.b.Write(p) |
| 36 |
} |
| 37 |
|
| 38 |
func (b *concurrentBuffer) String() string { |
| 39 |
b.m.Lock() |
| 40 |
defer b.m.Unlock() |
| 41 |
return b.b.String() |
| 42 |
} |
| 43 |
|
| 44 |
func TestEmptyBatchConfig(t *testing.T) { |
| 45 |
assert.NotPanics(t, func() { |
| 46 |
var bp BatchProcessor |
| 47 |
ctx := t.Context() |
| 48 |
record := new(Record) |
| 49 |
assert.NoError(t, bp.OnEmit(ctx, record), "OnEmit") |
| 50 |
assert.NoError(t, bp.ForceFlush(ctx), "ForceFlush") |
| 51 |
assert.NoError(t, bp.Shutdown(ctx), "Shutdown") |
| 52 |
}) |
| 53 |
} |
| 54 |
|
| 55 |
func TestNewBatchConfig(t *testing.T) { |
| 56 |
otel.SetErrorHandler(otel.ErrorHandlerFunc(func(err error) { |
| 57 |
t.Log(err) |
| 58 |
})) |
| 59 |
|
| 60 |
testcases := []struct { |
| 61 |
name string |
| 62 |
envars map[string]string |
| 63 |
options []BatchProcessorOption |
| 64 |
want batchConfig |
| 65 |
}{ |
| 66 |
{ |
| 67 |
name: "Defaults", |
| 68 |
want: batchConfig{ |
| 69 |
maxQSize: newSetting(dfltMaxQSize), |
| 70 |
expInterval: newSetting(dfltExpInterval), |
| 71 |
expTimeout: newSetting(dfltExpTimeout), |
| 72 |
expMaxBatchSize: newSetting(dfltExpMaxBatchSize), |
| 73 |
expBufferSize: newSetting(dfltExpBufferSize), |
| 74 |
}, |
| 75 |
}, |
| 76 |
{ |
| 77 |
name: "Options", |
| 78 |
options: []BatchProcessorOption{ |
| 79 |
WithMaxQueueSize(10), |
| 80 |
WithExportInterval(time.Microsecond), |
| 81 |
WithExportTimeout(time.Hour), |
| 82 |
WithExportMaxBatchSize(2), |
| 83 |
WithExportBufferSize(3), |
| 84 |
}, |
| 85 |
want: batchConfig{ |
| 86 |
maxQSize: newSetting(10), |
| 87 |
expInterval: newSetting(time.Microsecond), |
| 88 |
expTimeout: newSetting(time.Hour), |
| 89 |
expMaxBatchSize: newSetting(2), |
| 90 |
expBufferSize: newSetting(3), |
| 91 |
}, |
| 92 |
}, |
| 93 |
{ |
| 94 |
name: "Environment", |
| 95 |
envars: map[string]string{ |
| 96 |
envarMaxQSize: strconv.Itoa(10), |
| 97 |
envarExpInterval: strconv.Itoa(100), |
| 98 |
envarExpTimeout: strconv.Itoa(1000), |
| 99 |
envarExpMaxBatchSize: strconv.Itoa(1), |
| 100 |
}, |
| 101 |
want: batchConfig{ |
| 102 |
maxQSize: newSetting(10), |
| 103 |
expInterval: newSetting(100 * time.Millisecond), |
| 104 |
expTimeout: newSetting(1000 * time.Millisecond), |
| 105 |
expMaxBatchSize: newSetting(1), |
| 106 |
expBufferSize: newSetting(dfltExpBufferSize), |
| 107 |
}, |
| 108 |
}, |
| 109 |
{ |
| 110 |
name: "InvalidOptions", |
| 111 |
options: []BatchProcessorOption{ |
| 112 |
WithMaxQueueSize(-11), |
| 113 |
WithExportInterval(-1 * time.Microsecond), |
| 114 |
WithExportTimeout(-1 * time.Hour), |
| 115 |
WithExportMaxBatchSize(-2), |
| 116 |
WithExportBufferSize(-2), |
| 117 |
}, |
| 118 |
want: batchConfig{ |
| 119 |
maxQSize: newSetting(dfltMaxQSize), |
| 120 |
expInterval: newSetting(dfltExpInterval), |
| 121 |
expTimeout: newSetting(dfltExpTimeout), |
| 122 |
expMaxBatchSize: newSetting(dfltExpMaxBatchSize), |
| 123 |
expBufferSize: newSetting(dfltExpBufferSize), |
| 124 |
}, |
| 125 |
}, |
| 126 |
{ |
| 127 |
name: "InvalidEnvironment", |
| 128 |
envars: map[string]string{ |
| 129 |
envarMaxQSize: "-1", |
| 130 |
envarExpInterval: "-1", |
| 131 |
envarExpTimeout: "-1", |
| 132 |
envarExpMaxBatchSize: "-1", |
| 133 |
}, |
| 134 |
want: batchConfig{ |
| 135 |
maxQSize: newSetting(dfltMaxQSize), |
| 136 |
expInterval: newSetting(dfltExpInterval), |
| 137 |
expTimeout: newSetting(dfltExpTimeout), |
| 138 |
expMaxBatchSize: newSetting(dfltExpMaxBatchSize), |
| 139 |
expBufferSize: newSetting(dfltExpBufferSize), |
| 140 |
}, |
| 141 |
}, |
| 142 |
{ |
| 143 |
name: "Precedence", |
| 144 |
envars: map[string]string{ |
| 145 |
envarMaxQSize: strconv.Itoa(1), |
| 146 |
envarExpInterval: strconv.Itoa(100), |
| 147 |
envarExpTimeout: strconv.Itoa(1000), |
| 148 |
envarExpMaxBatchSize: strconv.Itoa(10), |
| 149 |
}, |
| 150 |
options: []BatchProcessorOption{ |
| 151 |
// These override the environment variables. |
| 152 |
WithMaxQueueSize(3), |
| 153 |
WithExportInterval(time.Microsecond), |
| 154 |
WithExportTimeout(time.Hour), |
| 155 |
WithExportMaxBatchSize(2), |
| 156 |
WithExportBufferSize(2), |
| 157 |
}, |
| 158 |
want: batchConfig{ |
| 159 |
maxQSize: newSetting(3), |
| 160 |
expInterval: newSetting(time.Microsecond), |
| 161 |
expTimeout: newSetting(time.Hour), |
| 162 |
expMaxBatchSize: newSetting(2), |
| 163 |
expBufferSize: newSetting(2), |
| 164 |
}, |
| 165 |
}, |
| 166 |
{ |
| 167 |
name: "BatchLessThanOrEqualToQSize", |
| 168 |
options: []BatchProcessorOption{ |
| 169 |
WithMaxQueueSize(1), |
| 170 |
WithExportMaxBatchSize(10), |
| 171 |
WithExportBufferSize(3), |
| 172 |
}, |
| 173 |
want: batchConfig{ |
| 174 |
maxQSize: newSetting(1), |
| 175 |
expInterval: newSetting(dfltExpInterval), |
| 176 |
expTimeout: newSetting(dfltExpTimeout), |
| 177 |
expMaxBatchSize: newSetting(1), |
| 178 |
expBufferSize: newSetting(3), |
| 179 |
}, |
| 180 |
}, |
| 181 |
} |
| 182 |
|
| 183 |
for _, tc := range testcases { |
| 184 |
t.Run(tc.name, func(t *testing.T) { |
| 185 |
for key, value := range tc.envars { |
| 186 |
t.Setenv(key, value) |
| 187 |
} |
| 188 |
assert.Equal(t, tc.want, newBatchConfig(tc.options)) |
| 189 |
}) |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
func TestBatchProcessor(t *testing.T) { |
| 194 |
ctx := t.Context() |
| 195 |
|
| 196 |
t.Run("NilExporter", func(t *testing.T) { |
| 197 |
assert.NotPanics(t, func() { NewBatchProcessor(nil) }) |
| 198 |
}) |
| 199 |
|
| 200 |
t.Run("Polling", func(t *testing.T) { |
| 201 |
e := newTestExporter(nil) |
| 202 |
const size = 15 |
| 203 |
b := NewBatchProcessor( |
| 204 |
e, |
| 205 |
WithMaxQueueSize(2*size), |
| 206 |
WithExportMaxBatchSize(2*size), |
| 207 |
WithExportInterval(time.Nanosecond), |
| 208 |
WithExportTimeout(time.Hour), |
| 209 |
) |
| 210 |
for range size { |
| 211 |
assert.NoError(t, b.OnEmit(ctx, new(Record))) |
| 212 |
} |
| 213 |
var got []Record |
| 214 |
assert.Eventually(t, func() bool { |
| 215 |
for _, r := range e.Records() { |
| 216 |
got = append(got, r...) |
| 217 |
} |
| 218 |
return len(got) == size |
| 219 |
}, 2*time.Second, time.Microsecond) |
| 220 |
_ = b.Shutdown(ctx) |
| 221 |
}) |
| 222 |
|
| 223 |
t.Run("Enabled", func(t *testing.T) { |
| 224 |
e := newTestExporter(nil) |
| 225 |
b := NewBatchProcessor(e) |
| 226 |
enabled := b.Enabled(ctx, EnabledParameters{}) |
| 227 |
assert.True(t, enabled, "Enabled should return true") |
| 228 |
}) |
| 229 |
|
| 230 |
t.Run("OnEmit", func(t *testing.T) { |
| 231 |
const batch = 10 |
| 232 |
e := newTestExporter(nil) |
| 233 |
b := NewBatchProcessor( |
| 234 |
e, |
| 235 |
WithMaxQueueSize(10*batch), |
| 236 |
WithExportMaxBatchSize(batch), |
| 237 |
WithExportInterval(time.Hour), |
| 238 |
WithExportTimeout(time.Hour), |
| 239 |
) |
| 240 |
for range 10 * batch { |
| 241 |
assert.NoError(t, b.OnEmit(ctx, new(Record))) |
| 242 |
} |
| 243 |
assert.Eventually(t, func() bool { |
| 244 |
return e.ExportN() > 1 |
| 245 |
}, 2*time.Second, time.Microsecond, "multi-batch flush") |
| 246 |
|
| 247 |
assert.NoError(t, b.Shutdown(ctx)) |
| 248 |
assert.GreaterOrEqual(t, e.ExportN(), 10) |
| 249 |
}) |
| 250 |
|
| 251 |
t.Run("RetriggerFlushNonBlocking", func(t *testing.T) { |
| 252 |
e := newTestExporter(nil) |
| 253 |
e.ExportTrigger = make(chan struct{}) |
| 254 |
|
| 255 |
const batch = 10 |
| 256 |
b := NewBatchProcessor( |
| 257 |
e, |
| 258 |
WithMaxQueueSize(3*batch), |
| 259 |
WithExportMaxBatchSize(batch), |
| 260 |
WithExportInterval(time.Hour), |
| 261 |
WithExportTimeout(time.Hour), |
| 262 |
) |
| 263 |
for range 2 * batch { |
| 264 |
assert.NoError(t, b.OnEmit(ctx, new(Record))) |
| 265 |
} |
| 266 |
|
| 267 |
var n int |
| 268 |
require.Eventually(t, func() bool { |
| 269 |
n = e.ExportN() |
| 270 |
return n > 0 |
| 271 |
}, 2*time.Second, time.Microsecond, "blocked export not attempted") |
| 272 |
|
| 273 |
var err error |
| 274 |
require.Eventually(t, func() bool { |
| 275 |
err = b.OnEmit(ctx, new(Record)) |
| 276 |
return true |
| 277 |
}, time.Second, time.Microsecond, "OnEmit blocked") |
| 278 |
assert.NoError(t, err) |
| 279 |
|
| 280 |
e.ExportTrigger <- struct{}{} |
| 281 |
assert.Eventually(t, func() bool { |
| 282 |
return e.ExportN() > n |
| 283 |
}, 2*time.Second, time.Microsecond, "flush not retriggered") |
| 284 |
|
| 285 |
close(e.ExportTrigger) |
| 286 |
assert.NoError(t, b.Shutdown(ctx)) |
| 287 |
assert.Equal(t, 3, e.ExportN()) |
| 288 |
}) |
| 289 |
|
| 290 |
t.Run("Shutdown", func(t *testing.T) { |
| 291 |
t.Run("Error", func(t *testing.T) { |
| 292 |
e := newTestExporter(assert.AnError) |
| 293 |
b := NewBatchProcessor(e) |
| 294 |
assert.ErrorIs(t, b.Shutdown(ctx), assert.AnError, "exporter error not returned") |
| 295 |
assert.NoError(t, b.Shutdown(ctx)) |
| 296 |
}) |
| 297 |
|
| 298 |
t.Run("Multiple", func(t *testing.T) { |
| 299 |
e := newTestExporter(nil) |
| 300 |
b := NewBatchProcessor(e) |
| 301 |
|
| 302 |
const shutdowns = 3 |
| 303 |
for range shutdowns { |
| 304 |
assert.NoError(t, b.Shutdown(ctx)) |
| 305 |
} |
| 306 |
assert.Equal(t, 1, e.ShutdownN(), "exporter Shutdown calls") |
| 307 |
}) |
| 308 |
|
| 309 |
t.Run("OnEmit", func(t *testing.T) { |
| 310 |
e := newTestExporter(nil) |
| 311 |
b := NewBatchProcessor(e) |
| 312 |
assert.NoError(t, b.Shutdown(ctx)) |
| 313 |
|
| 314 |
want := e.ExportN() |
| 315 |
assert.NoError(t, b.OnEmit(ctx, new(Record))) |
| 316 |
assert.Equal(t, want, e.ExportN(), "Export called after shutdown") |
| 317 |
}) |
| 318 |
|
| 319 |
t.Run("ForceFlush", func(t *testing.T) { |
| 320 |
e := newTestExporter(nil) |
| 321 |
b := NewBatchProcessor(e) |
| 322 |
|
| 323 |
assert.NoError(t, b.OnEmit(ctx, new(Record))) |
| 324 |
assert.NoError(t, b.Shutdown(ctx)) |
| 325 |
|
| 326 |
assert.NoError(t, b.ForceFlush(ctx)) |
| 327 |
assert.Equal(t, 0, e.ForceFlushN(), "ForceFlush called after shutdown") |
| 328 |
}) |
| 329 |
|
| 330 |
t.Run("CanceledContext", func(t *testing.T) { |
| 331 |
e := newTestExporter(nil) |
| 332 |
e.ExportTrigger = make(chan struct{}) |
| 333 |
t.Cleanup(func() { close(e.ExportTrigger) }) |
| 334 |
b := NewBatchProcessor(e) |
| 335 |
|
| 336 |
ctx := t.Context() |
| 337 |
c, cancel := context.WithCancel(ctx) |
| 338 |
cancel() |
| 339 |
|
| 340 |
assert.ErrorIs(t, b.Shutdown(c), context.Canceled) |
| 341 |
}) |
| 342 |
}) |
| 343 |
|
| 344 |
t.Run("ForceFlush", func(t *testing.T) { |
| 345 |
t.Run("Flush", func(t *testing.T) { |
| 346 |
e := newTestExporter(assert.AnError) |
| 347 |
b := NewBatchProcessor( |
| 348 |
e, |
| 349 |
WithMaxQueueSize(100), |
| 350 |
WithExportMaxBatchSize(10), |
| 351 |
WithExportInterval(time.Hour), |
| 352 |
WithExportTimeout(time.Hour), |
| 353 |
) |
| 354 |
t.Cleanup(func() { _ = b.Shutdown(ctx) }) |
| 355 |
|
| 356 |
r := new(Record) |
| 357 |
r.SetBody(log.BoolValue(true)) |
| 358 |
require.NoError(t, b.OnEmit(ctx, r)) |
| 359 |
|
| 360 |
assert.ErrorIs(t, b.ForceFlush(ctx), assert.AnError, "exporter error not returned") |
| 361 |
assert.Equal(t, 1, e.ForceFlushN(), "exporter ForceFlush calls") |
| 362 |
if assert.Equal(t, 1, e.ExportN(), "exporter Export calls") { |
| 363 |
got := e.Records() |
| 364 |
if assert.Len(t, got[0], 1, "records received") { |
| 365 |
assert.Equal(t, *r, got[0][0]) |
| 366 |
} |
| 367 |
} |
| 368 |
}) |
| 369 |
|
| 370 |
t.Run("ErrorPartialFlush", func(t *testing.T) { |
| 371 |
e := newTestExporter(nil) |
| 372 |
e.ExportTrigger = make(chan struct{}) |
| 373 |
|
| 374 |
ctxErrCalled := make(chan struct{}) |
| 375 |
orig := ctxErr |
| 376 |
ctxErr = func(ctx context.Context) error { |
| 377 |
close(ctxErrCalled) |
| 378 |
return orig(ctx) |
| 379 |
} |
| 380 |
t.Cleanup(func() { ctxErr = orig }) |
| 381 |
|
| 382 |
const batch = 1 |
| 383 |
b := NewBatchProcessor( |
| 384 |
e, |
| 385 |
WithMaxQueueSize(10*batch), |
| 386 |
WithExportMaxBatchSize(batch), |
| 387 |
WithExportInterval(time.Hour), |
| 388 |
WithExportTimeout(time.Hour), |
| 389 |
) |
| 390 |
|
| 391 |
// Enqueue 10 x "batch size" amount of records. |
| 392 |
for range 10 * batch { |
| 393 |
require.NoError(t, b.OnEmit(ctx, new(Record))) |
| 394 |
} |
| 395 |
assert.Eventually(t, func() bool { |
| 396 |
return e.ExportN() > 0 && len(b.exporter.input) == cap(b.exporter.input) |
| 397 |
}, 2*time.Second, time.Microsecond) |
| 398 |
// 1 export being performed, 1 export in buffer chan, >1 batch |
| 399 |
// still in queue that an attempt to flush will be made on. |
| 400 |
// |
| 401 |
// Stop the poll routine to prevent contention with the queue lock. |
| 402 |
// This is outside of "normal" operations, but we are testing if |
| 403 |
// ForceFlush will return the correct error when an EnqueueExport |
| 404 |
// fails and not if ForceFlush will ever get the queue lock in high |
| 405 |
// throughput situations. |
| 406 |
close(b.pollDone) |
| 407 |
<-b.pollDone |
| 408 |
|
| 409 |
// Cancel the flush ctx from the start so errPartialFlush is |
| 410 |
// returned right away. |
| 411 |
fCtx, cancel := context.WithCancel(ctx) |
| 412 |
cancel() |
| 413 |
|
| 414 |
errCh := make(chan error, 1) |
| 415 |
go func() { |
| 416 |
errCh <- b.ForceFlush(fCtx) |
| 417 |
close(errCh) |
| 418 |
}() |
| 419 |
// Wait for ctxErrCalled to close before closing ExportTrigger so |
| 420 |
// we know the errPartialFlush will be returned in ForceFlush. |
| 421 |
<-ctxErrCalled |
| 422 |
close(e.ExportTrigger) |
| 423 |
|
| 424 |
err := <-errCh |
| 425 |
assert.ErrorIs(t, err, errPartialFlush, "partial flush error") |
| 426 |
assert.ErrorIs(t, err, context.Canceled, "ctx canceled error") |
| 427 |
}) |
| 428 |
|
| 429 |
t.Run("CanceledContext", func(t *testing.T) { |
| 430 |
e := newTestExporter(nil) |
| 431 |
e.ExportTrigger = make(chan struct{}) |
| 432 |
b := NewBatchProcessor(e) |
| 433 |
t.Cleanup(func() { _ = b.Shutdown(ctx) }) |
| 434 |
|
| 435 |
r := new(Record) |
| 436 |
r.SetBody(log.BoolValue(true)) |
| 437 |
_ = b.OnEmit(ctx, r) |
| 438 |
t.Cleanup(func() { _ = b.Shutdown(ctx) }) |
| 439 |
t.Cleanup(func() { close(e.ExportTrigger) }) |
| 440 |
|
| 441 |
c, cancel := context.WithCancel(ctx) |
| 442 |
cancel() |
| 443 |
assert.ErrorIs(t, b.ForceFlush(c), context.Canceled) |
| 444 |
}) |
| 445 |
}) |
| 446 |
|
| 447 |
t.Run("DroppedLogs", func(t *testing.T) { |
| 448 |
orig := global.GetLogger() |
| 449 |
t.Cleanup(func() { global.SetLogger(orig) }) |
| 450 |
// Use concurrentBuffer for concurrent-safe reading. |
| 451 |
buf := new(concurrentBuffer) |
| 452 |
stdr.SetVerbosity(1) |
| 453 |
global.SetLogger(stdr.New(stdlog.New(buf, "", 0))) |
| 454 |
|
| 455 |
e := newTestExporter(nil) |
| 456 |
e.ExportTrigger = make(chan struct{}) |
| 457 |
|
| 458 |
b := NewBatchProcessor( |
| 459 |
e, |
| 460 |
WithMaxQueueSize(1), |
| 461 |
WithExportMaxBatchSize(1), |
| 462 |
WithExportInterval(time.Hour), |
| 463 |
WithExportTimeout(time.Hour), |
| 464 |
) |
| 465 |
r := new(Record) |
| 466 |
// First record will be blocked by testExporter.Export |
| 467 |
assert.NoError(t, b.OnEmit(ctx, r), "exported record") |
| 468 |
require.Eventually(t, func() bool { |
| 469 |
return e.ExportN() > 0 |
| 470 |
}, 2*time.Second, time.Microsecond, "blocked export not attempted") |
| 471 |
|
| 472 |
// Second record will be written to export queue |
| 473 |
assert.NoError(t, b.OnEmit(ctx, r), "export queue record") |
| 474 |
require.Eventually(t, func() bool { |
| 475 |
return len(b.exporter.input) == cap(b.exporter.input) |
| 476 |
}, 2*time.Second, time.Microsecond, "blocked queue read not attempted") |
| 477 |
|
| 478 |
// Third record will be written to BatchProcessor.q |
| 479 |
assert.NoError(t, b.OnEmit(ctx, r), "first queued") |
| 480 |
// The previous record will be dropped, as the new one will be written to BatchProcessor.q |
| 481 |
assert.NoError(t, b.OnEmit(ctx, r), "second queued") |
| 482 |
|
| 483 |
wantMsg := `"level"=1 "msg"="dropped log records" "dropped"=1` |
| 484 |
assert.Eventually(t, func() bool { |
| 485 |
return strings.Contains(buf.String(), wantMsg) |
| 486 |
}, 2*time.Second, time.Microsecond) |
| 487 |
|
| 488 |
close(e.ExportTrigger) |
| 489 |
_ = b.Shutdown(ctx) |
| 490 |
}) |
| 491 |
|
| 492 |
t.Run("ConcurrentSafe", func(t *testing.T) { |
| 493 |
const goRoutines = 10 |
| 494 |
|
| 495 |
e := newTestExporter(nil) |
| 496 |
b := NewBatchProcessor(e) |
| 497 |
|
| 498 |
ctx, cancel := context.WithCancel(ctx) |
| 499 |
var wg sync.WaitGroup |
| 500 |
for range goRoutines - 1 { |
| 501 |
wg.Go(func() { |
| 502 |
for { |
| 503 |
select { |
| 504 |
case <-ctx.Done(): |
| 505 |
return |
| 506 |
default: |
| 507 |
assert.NoError(t, b.OnEmit(ctx, new(Record))) |
| 508 |
// Ignore partial flush errors. |
| 509 |
_ = b.ForceFlush(ctx) |
| 510 |
} |
| 511 |
} |
| 512 |
}) |
| 513 |
} |
| 514 |
|
| 515 |
require.Eventually(t, func() bool { |
| 516 |
return e.ExportN() > 0 |
| 517 |
}, 2*time.Second, time.Microsecond, "export before shutdown") |
| 518 |
|
| 519 |
wg.Go(func() { |
| 520 |
assert.NoError(t, b.Shutdown(ctx)) |
| 521 |
cancel() |
| 522 |
}) |
| 523 |
|
| 524 |
wg.Wait() |
| 525 |
}) |
| 526 |
} |
| 527 |
|
| 528 |
func TestQueue(t *testing.T) { |
| 529 |
var r Record |
| 530 |
r.SetBody(log.BoolValue(true)) |
| 531 |
|
| 532 |
t.Run("newQueue", func(t *testing.T) { |
| 533 |
const size = 1 |
| 534 |
q := newQueue(size) |
| 535 |
assert.Equal(t, 0, q.len) |
| 536 |
assert.Equal(t, size, q.cap, "capacity") |
| 537 |
assert.Equal(t, size, q.read.Len(), "read ring") |
| 538 |
assert.Same(t, q.read, q.write, "different rings") |
| 539 |
}) |
| 540 |
|
| 541 |
t.Run("Enqueue", func(t *testing.T) { |
| 542 |
const size = 2 |
| 543 |
q := newQueue(size) |
| 544 |
|
| 545 |
var notR Record |
| 546 |
notR.SetBody(log.IntValue(10)) |
| 547 |
|
| 548 |
assert.Equal(t, 1, q.Enqueue(notR), "incomplete batch") |
| 549 |
assert.Equal(t, 1, q.len, "length") |
| 550 |
assert.Equal(t, size, q.cap, "capacity") |
| 551 |
|
| 552 |
assert.Equal(t, 2, q.Enqueue(r), "complete batch") |
| 553 |
assert.Equal(t, 2, q.len, "length") |
| 554 |
assert.Equal(t, size, q.cap, "capacity") |
| 555 |
|
| 556 |
assert.Equal(t, 2, q.Enqueue(r), "overflow batch") |
| 557 |
assert.Equal(t, 2, q.len, "length") |
| 558 |
assert.Equal(t, size, q.cap, "capacity") |
| 559 |
|
| 560 |
assert.Equal(t, []Record{r, r}, q.Flush(), "flushed Records") |
| 561 |
}) |
| 562 |
|
| 563 |
t.Run("Dropped", func(t *testing.T) { |
| 564 |
q := newQueue(1) |
| 565 |
|
| 566 |
_ = q.Enqueue(r) |
| 567 |
_ = q.Enqueue(r) |
| 568 |
assert.Equal(t, uint64(1), q.Dropped(), "fist") |
| 569 |
|
| 570 |
_ = q.Enqueue(r) |
| 571 |
_ = q.Enqueue(r) |
| 572 |
assert.Equal(t, uint64(2), q.Dropped(), "second") |
| 573 |
}) |
| 574 |
|
| 575 |
t.Run("Flush", func(t *testing.T) { |
| 576 |
const size = 2 |
| 577 |
q := newQueue(size) |
| 578 |
q.write.Value = r |
| 579 |
q.write = q.write.Next() |
| 580 |
q.len = 1 |
| 581 |
|
| 582 |
assert.Equal(t, []Record{r}, q.Flush(), "flushed") |
| 583 |
}) |
| 584 |
|
| 585 |
t.Run("TryFlush", func(t *testing.T) { |
| 586 |
const size = 3 |
| 587 |
q := newQueue(size) |
| 588 |
for range size - 1 { |
| 589 |
q.write.Value = r |
| 590 |
q.write = q.write.Next() |
| 591 |
q.len++ |
| 592 |
} |
| 593 |
|
| 594 |
buf := make([]Record, 1) |
| 595 |
f := func([]Record) bool { return false } |
| 596 |
assert.Equal(t, size-1, q.TryDequeue(buf, f), "not flushed") |
| 597 |
require.Equal(t, size-1, q.len, "length") |
| 598 |
require.NotSame(t, q.read, q.write, "read ring advanced") |
| 599 |
|
| 600 |
var flushed []Record |
| 601 |
f = func(r []Record) bool { |
| 602 |
flushed = append(flushed, r...) |
| 603 |
return true |
| 604 |
} |
| 605 |
if assert.Equal(t, size-2, q.TryDequeue(buf, f), "did not flush len(buf)") { |
| 606 |
assert.Equal(t, []Record{r}, flushed, "Records") |
| 607 |
} |
| 608 |
|
| 609 |
buf = slices.Grow(buf, size) |
| 610 |
flushed = flushed[:0] |
| 611 |
if assert.Equal(t, 0, q.TryDequeue(buf, f), "did not flush len(queue)") { |
| 612 |
assert.Equal(t, []Record{r}, flushed, "Records") |
| 613 |
} |
| 614 |
}) |
| 615 |
|
| 616 |
t.Run("ConcurrentSafe", func(t *testing.T) { |
| 617 |
const goRoutines = 10 |
| 618 |
|
| 619 |
flushed := make(chan []Record, goRoutines) |
| 620 |
out := make([]Record, 0, goRoutines) |
| 621 |
done := make(chan struct{}) |
| 622 |
go func() { |
| 623 |
defer close(done) |
| 624 |
for recs := range flushed { |
| 625 |
out = append(out, recs...) |
| 626 |
} |
| 627 |
}() |
| 628 |
|
| 629 |
var wg sync.WaitGroup |
| 630 |
wg.Add(goRoutines) |
| 631 |
|
| 632 |
b := newQueue(goRoutines) |
| 633 |
for range goRoutines { |
| 634 |
go func() { |
| 635 |
defer wg.Done() |
| 636 |
b.Enqueue(Record{}) |
| 637 |
flushed <- b.Flush() |
| 638 |
}() |
| 639 |
} |
| 640 |
|
| 641 |
wg.Wait() |
| 642 |
close(flushed) |
| 643 |
<-done |
| 644 |
|
| 645 |
assert.Len(t, out, goRoutines, "flushed Records") |
| 646 |
}) |
| 647 |
} |
| 648 |
|
| 649 |
func BenchmarkBatchProcessorOnEmit(b *testing.B) { |
| 650 |
r := new(Record) |
| 651 |
body := log.BoolValue(true) |
| 652 |
r.SetBody(body) |
| 653 |
|
| 654 |
rSize := unsafe.Sizeof(r) + unsafe.Sizeof(body) |
| 655 |
ctx := b.Context() |
| 656 |
bp := NewBatchProcessor( |
| 657 |
defaultNoopExporter, |
| 658 |
WithMaxQueueSize(b.N+1), |
| 659 |
WithExportMaxBatchSize(b.N+1), |
| 660 |
WithExportInterval(time.Hour), |
| 661 |
WithExportTimeout(time.Hour), |
| 662 |
) |
| 663 |
b.Cleanup(func() { _ = bp.Shutdown(ctx) }) |
| 664 |
|
| 665 |
b.SetBytes(int64(rSize)) |
| 666 |
b.ReportAllocs() |
| 667 |
b.ResetTimer() |
| 668 |
b.RunParallel(func(pb *testing.PB) { |
| 669 |
var err error |
| 670 |
for pb.Next() { |
| 671 |
err = bp.OnEmit(ctx, r) |
| 672 |
} |
| 673 |
_ = err |
| 674 |
}) |
| 675 |
} |