1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-06-25 00:16:49 +02:00

Add ExportTimeout option to batch span processor (#1755)

* Add ExportTimeout option

* Adjust tests

* Update CHANGELOG

* Beef up the exporter timeout test

* Beef up exporter test - attempt #2

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
Matej Gera
2021-04-05 19:38:03 +02:00
committed by GitHub
parent c6b92d5b20
commit 3c7facee73
3 changed files with 59 additions and 9 deletions

View File

@ -36,12 +36,23 @@ type testBatchExporter struct {
sizes []int
batchCount int
shutdownCount int
delay time.Duration
err error
}
func (t *testBatchExporter) ExportSpans(ctx context.Context, ss []*export.SpanSnapshot) error {
t.mu.Lock()
defer t.mu.Unlock()
time.Sleep(t.delay)
select {
case <-ctx.Done():
t.err = ctx.Err()
return ctx.Err()
default:
}
t.spans = append(t.spans, ss...)
t.sizes = append(t.sizes, len(ss))
t.batchCount++
@ -88,16 +99,19 @@ func TestNewBatchSpanProcessorWithNilExporter(t *testing.T) {
}
type testOption struct {
name string
o []sdktrace.BatchSpanProcessorOption
wantNumSpans int
wantBatchCount int
genNumSpans int
parallel bool
name string
o []sdktrace.BatchSpanProcessorOption
wantNumSpans int
wantBatchCount int
wantExportTimeout bool
genNumSpans int
delayExportBy time.Duration
parallel bool
}
func TestNewBatchSpanProcessorWithOptions(t *testing.T) {
schDelay := 200 * time.Millisecond
exportTimeout := time.Millisecond
options := []testOption{
{
name: "default BatchSpanProcessorOptions",
@ -105,6 +119,15 @@ func TestNewBatchSpanProcessorWithOptions(t *testing.T) {
wantBatchCount: 4,
genNumSpans: 2053,
},
{
name: "non-default ExportTimeout",
o: []sdktrace.BatchSpanProcessorOption{
sdktrace.WithExportTimeout(exportTimeout),
},
wantExportTimeout: true,
genNumSpans: 2053,
delayExportBy: 2 * exportTimeout, // to ensure export timeout
},
{
name: "non-default BatchTimeout",
o: []sdktrace.BatchSpanProcessorOption{
@ -171,7 +194,9 @@ func TestNewBatchSpanProcessorWithOptions(t *testing.T) {
}
for _, option := range options {
t.Run(option.name, func(t *testing.T) {
te := testBatchExporter{}
te := testBatchExporter{
delay: option.delayExportBy,
}
tp := basicTracerProvider(t)
ssp := createAndRegisterBatchSP(option, &te)
if ssp == nil {
@ -185,17 +210,22 @@ func TestNewBatchSpanProcessorWithOptions(t *testing.T) {
tp.UnregisterSpanProcessor(ssp)
gotNumOfSpans := te.len()
if option.wantNumSpans != gotNumOfSpans {
if option.wantNumSpans > 0 && option.wantNumSpans != gotNumOfSpans {
t.Errorf("number of exported span: got %+v, want %+v\n",
gotNumOfSpans, option.wantNumSpans)
}
gotBatchCount := te.getBatchCount()
if gotBatchCount < option.wantBatchCount {
if option.wantBatchCount > 0 && gotBatchCount < option.wantBatchCount {
t.Errorf("number batches: got %+v, want >= %+v\n",
gotBatchCount, option.wantBatchCount)
t.Errorf("Batches %v\n", te.sizes)
}
if option.wantExportTimeout && te.err != context.DeadlineExceeded {
t.Errorf("context deadline: got err %+v, want %+v\n",
te.err, context.DeadlineExceeded)
}
})
}
}