2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-10-01 22:50:51 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package trace_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-10-23 08:01:33 +02:00
|
|
|
"encoding/binary"
|
2021-04-01 19:42:19 +02:00
|
|
|
"errors"
|
2021-11-05 17:34:57 +02:00
|
|
|
"fmt"
|
2019-10-01 22:50:51 +02:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-01-11 02:58:01 +02:00
|
|
|
"github.com/go-logr/logr/funcr"
|
2021-01-14 03:14:03 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-04-06 23:54:26 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-01-14 03:14:03 +02:00
|
|
|
|
2022-01-11 02:58:01 +02:00
|
|
|
"go.opentelemetry.io/otel/internal/global"
|
2021-11-13 18:35:04 +02:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2021-11-05 17:34:57 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/trace/tracetest"
|
2020-11-07 00:13:31 +02:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2019-10-01 22:50:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type testBatchExporter struct {
|
2021-01-14 03:14:03 +02:00
|
|
|
mu sync.Mutex
|
2021-05-05 01:45:13 +02:00
|
|
|
spans []sdktrace.ReadOnlySpan
|
2021-01-14 03:14:03 +02:00
|
|
|
sizes []int
|
|
|
|
batchCount int
|
|
|
|
shutdownCount int
|
2021-04-29 18:42:11 +02:00
|
|
|
errors []error
|
|
|
|
droppedCount int
|
|
|
|
idx int
|
2021-04-05 19:38:03 +02:00
|
|
|
err error
|
2019-10-01 22:50:51 +02:00
|
|
|
}
|
|
|
|
|
2021-05-05 01:45:13 +02:00
|
|
|
func (t *testBatchExporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlySpan) error {
|
2019-10-01 22:50:51 +02:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
2019-10-08 20:56:58 +02:00
|
|
|
|
2021-04-29 18:42:11 +02:00
|
|
|
if t.idx < len(t.errors) {
|
2021-05-05 01:45:13 +02:00
|
|
|
t.droppedCount += len(spans)
|
2021-04-29 18:42:11 +02:00
|
|
|
err := t.errors[t.idx]
|
|
|
|
t.idx++
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-05 19:38:03 +02:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
t.err = ctx.Err()
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2021-05-05 01:45:13 +02:00
|
|
|
t.spans = append(t.spans, spans...)
|
|
|
|
t.sizes = append(t.sizes, len(spans))
|
2019-10-01 22:50:51 +02:00
|
|
|
t.batchCount++
|
2020-09-09 19:19:03 +02:00
|
|
|
return nil
|
2019-10-01 22:50:51 +02:00
|
|
|
}
|
|
|
|
|
2021-01-14 03:14:03 +02:00
|
|
|
func (t *testBatchExporter) Shutdown(context.Context) error {
|
|
|
|
t.shutdownCount++
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-09 19:19:03 +02:00
|
|
|
|
2019-10-01 22:50:51 +02:00
|
|
|
func (t *testBatchExporter) len() int {
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
return len(t.spans)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testBatchExporter) getBatchCount() int {
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
return t.batchCount
|
|
|
|
}
|
|
|
|
|
2021-04-07 17:03:43 +02:00
|
|
|
var _ sdktrace.SpanExporter = (*testBatchExporter)(nil)
|
2019-10-01 22:50:51 +02:00
|
|
|
|
|
|
|
func TestNewBatchSpanProcessorWithNilExporter(t *testing.T) {
|
2020-12-11 07:15:44 +02:00
|
|
|
tp := basicTracerProvider(t)
|
2020-09-09 19:19:03 +02:00
|
|
|
bsp := sdktrace.NewBatchSpanProcessor(nil)
|
2020-12-11 07:15:44 +02:00
|
|
|
tp.RegisterSpanProcessor(bsp)
|
|
|
|
tr := tp.Tracer("NilExporter")
|
|
|
|
|
|
|
|
_, span := tr.Start(context.Background(), "foo")
|
|
|
|
span.End()
|
|
|
|
|
2020-09-09 19:19:03 +02:00
|
|
|
// These should not panic.
|
2020-12-11 07:15:44 +02:00
|
|
|
bsp.OnStart(context.Background(), span.(sdktrace.ReadWriteSpan))
|
|
|
|
bsp.OnEnd(span.(sdktrace.ReadOnlySpan))
|
2021-03-08 21:12:13 +02:00
|
|
|
if err := bsp.ForceFlush(context.Background()); err != nil {
|
|
|
|
t.Errorf("failed to ForceFlush the BatchSpanProcessor: %v", err)
|
|
|
|
}
|
|
|
|
if err := bsp.Shutdown(context.Background()); err != nil {
|
|
|
|
t.Errorf("failed to Shutdown the BatchSpanProcessor: %v", err)
|
2020-10-27 04:06:55 +02:00
|
|
|
}
|
2019-10-01 22:50:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type testOption struct {
|
2021-06-09 23:05:10 +02:00
|
|
|
name string
|
|
|
|
o []sdktrace.BatchSpanProcessorOption
|
|
|
|
wantNumSpans int
|
|
|
|
wantBatchCount int
|
|
|
|
genNumSpans int
|
|
|
|
parallel bool
|
2019-10-01 22:50:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewBatchSpanProcessorWithOptions(t *testing.T) {
|
2019-12-09 23:03:11 +02:00
|
|
|
schDelay := 200 * time.Millisecond
|
2019-10-01 22:50:51 +02:00
|
|
|
options := []testOption{
|
|
|
|
{
|
|
|
|
name: "default BatchSpanProcessorOptions",
|
2020-05-14 13:02:22 +02:00
|
|
|
wantNumSpans: 2053,
|
2019-10-01 22:50:51 +02:00
|
|
|
wantBatchCount: 4,
|
|
|
|
genNumSpans: 2053,
|
|
|
|
},
|
|
|
|
{
|
2020-05-20 19:12:57 +02:00
|
|
|
name: "non-default BatchTimeout",
|
2019-10-01 22:50:51 +02:00
|
|
|
o: []sdktrace.BatchSpanProcessorOption{
|
2020-05-20 19:12:57 +02:00
|
|
|
sdktrace.WithBatchTimeout(schDelay),
|
2019-10-01 22:50:51 +02:00
|
|
|
},
|
2020-05-14 13:02:22 +02:00
|
|
|
wantNumSpans: 2053,
|
2019-10-01 22:50:51 +02:00
|
|
|
wantBatchCount: 4,
|
|
|
|
genNumSpans: 2053,
|
|
|
|
},
|
|
|
|
{
|
2020-05-20 19:12:57 +02:00
|
|
|
name: "non-default MaxQueueSize and BatchTimeout",
|
2019-10-01 22:50:51 +02:00
|
|
|
o: []sdktrace.BatchSpanProcessorOption{
|
2020-05-20 19:12:57 +02:00
|
|
|
sdktrace.WithBatchTimeout(schDelay),
|
2019-10-01 22:50:51 +02:00
|
|
|
sdktrace.WithMaxQueueSize(200),
|
|
|
|
},
|
2020-05-14 13:02:22 +02:00
|
|
|
wantNumSpans: 205,
|
2019-10-01 22:50:51 +02:00
|
|
|
wantBatchCount: 1,
|
|
|
|
genNumSpans: 205,
|
|
|
|
},
|
|
|
|
{
|
2020-05-20 19:12:57 +02:00
|
|
|
name: "non-default MaxQueueSize, BatchTimeout and MaxExportBatchSize",
|
2019-10-01 22:50:51 +02:00
|
|
|
o: []sdktrace.BatchSpanProcessorOption{
|
2020-05-20 19:12:57 +02:00
|
|
|
sdktrace.WithBatchTimeout(schDelay),
|
2019-10-01 22:50:51 +02:00
|
|
|
sdktrace.WithMaxQueueSize(205),
|
|
|
|
sdktrace.WithMaxExportBatchSize(20),
|
|
|
|
},
|
2020-05-14 13:02:22 +02:00
|
|
|
wantNumSpans: 210,
|
2019-10-01 22:50:51 +02:00
|
|
|
wantBatchCount: 11,
|
|
|
|
genNumSpans: 210,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "blocking option",
|
|
|
|
o: []sdktrace.BatchSpanProcessorOption{
|
2020-05-20 19:12:57 +02:00
|
|
|
sdktrace.WithBatchTimeout(schDelay),
|
2019-10-01 22:50:51 +02:00
|
|
|
sdktrace.WithMaxQueueSize(200),
|
|
|
|
sdktrace.WithMaxExportBatchSize(20),
|
|
|
|
},
|
|
|
|
wantNumSpans: 205,
|
|
|
|
wantBatchCount: 11,
|
|
|
|
genNumSpans: 205,
|
|
|
|
},
|
2020-03-05 23:41:00 +02:00
|
|
|
{
|
|
|
|
name: "parallel span generation",
|
|
|
|
o: []sdktrace.BatchSpanProcessorOption{
|
2020-05-20 19:12:57 +02:00
|
|
|
sdktrace.WithBatchTimeout(schDelay),
|
2020-03-05 23:41:00 +02:00
|
|
|
sdktrace.WithMaxQueueSize(200),
|
|
|
|
},
|
2020-05-14 13:02:22 +02:00
|
|
|
wantNumSpans: 205,
|
2020-03-05 23:41:00 +02:00
|
|
|
wantBatchCount: 1,
|
|
|
|
genNumSpans: 205,
|
|
|
|
parallel: true,
|
|
|
|
},
|
2020-03-28 01:21:20 +02:00
|
|
|
{
|
|
|
|
name: "parallel span blocking",
|
|
|
|
o: []sdktrace.BatchSpanProcessorOption{
|
2020-05-20 19:12:57 +02:00
|
|
|
sdktrace.WithBatchTimeout(schDelay),
|
2020-03-28 01:21:20 +02:00
|
|
|
sdktrace.WithMaxExportBatchSize(200),
|
|
|
|
},
|
|
|
|
wantNumSpans: 2000,
|
|
|
|
wantBatchCount: 10,
|
|
|
|
genNumSpans: 2000,
|
|
|
|
parallel: true,
|
|
|
|
},
|
2019-10-01 22:50:51 +02:00
|
|
|
}
|
|
|
|
for _, option := range options {
|
2020-05-19 18:36:33 +02:00
|
|
|
t.Run(option.name, func(t *testing.T) {
|
2021-06-09 23:05:10 +02:00
|
|
|
te := testBatchExporter{}
|
2020-09-24 00:16:13 +02:00
|
|
|
tp := basicTracerProvider(t)
|
2020-09-09 19:19:03 +02:00
|
|
|
ssp := createAndRegisterBatchSP(option, &te)
|
2020-05-19 18:36:33 +02:00
|
|
|
if ssp == nil {
|
|
|
|
t.Fatalf("%s: Error creating new instance of BatchSpanProcessor\n", option.name)
|
|
|
|
}
|
|
|
|
tp.RegisterSpanProcessor(ssp)
|
|
|
|
tr := tp.Tracer("BatchSpanProcessorWithOptions")
|
|
|
|
|
|
|
|
generateSpan(t, option.parallel, tr, option)
|
|
|
|
|
|
|
|
tp.UnregisterSpanProcessor(ssp)
|
|
|
|
|
2020-06-10 00:20:48 +02:00
|
|
|
gotNumOfSpans := te.len()
|
2021-04-05 19:38:03 +02:00
|
|
|
if option.wantNumSpans > 0 && option.wantNumSpans != gotNumOfSpans {
|
2020-06-10 00:20:48 +02:00
|
|
|
t.Errorf("number of exported span: got %+v, want %+v\n",
|
|
|
|
gotNumOfSpans, option.wantNumSpans)
|
|
|
|
}
|
|
|
|
|
|
|
|
gotBatchCount := te.getBatchCount()
|
2021-04-05 19:38:03 +02:00
|
|
|
if option.wantBatchCount > 0 && gotBatchCount < option.wantBatchCount {
|
2020-06-10 00:20:48 +02:00
|
|
|
t.Errorf("number batches: got %+v, want >= %+v\n",
|
|
|
|
gotBatchCount, option.wantBatchCount)
|
|
|
|
t.Errorf("Batches %v\n", te.sizes)
|
|
|
|
}
|
2020-05-19 18:36:33 +02:00
|
|
|
})
|
2019-10-01 22:50:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 23:05:10 +02:00
|
|
|
type stuckExporter struct {
|
|
|
|
testBatchExporter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExportSpans waits for ctx to expire and returns that error.
|
|
|
|
func (e *stuckExporter) ExportSpans(ctx context.Context, _ []sdktrace.ReadOnlySpan) error {
|
|
|
|
<-ctx.Done()
|
|
|
|
e.err = ctx.Err()
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBatchSpanProcessorExportTimeout(t *testing.T) {
|
|
|
|
exp := new(stuckExporter)
|
|
|
|
bsp := sdktrace.NewBatchSpanProcessor(
|
|
|
|
exp,
|
|
|
|
// Set a non-zero export timeout so a deadline is set.
|
|
|
|
sdktrace.WithExportTimeout(1*time.Microsecond),
|
|
|
|
sdktrace.WithBlocking(),
|
|
|
|
)
|
|
|
|
tp := basicTracerProvider(t)
|
|
|
|
tp.RegisterSpanProcessor(bsp)
|
|
|
|
|
|
|
|
tr := tp.Tracer("BatchSpanProcessorExportTimeout")
|
|
|
|
generateSpan(t, false, tr, testOption{genNumSpans: 1})
|
|
|
|
tp.UnregisterSpanProcessor(bsp)
|
|
|
|
|
|
|
|
if exp.err != context.DeadlineExceeded {
|
|
|
|
t.Errorf("context deadline error not returned: got %+v", exp.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 18:08:29 +02:00
|
|
|
func createAndRegisterBatchSP(option testOption, te *testBatchExporter) sdktrace.SpanProcessor {
|
2020-06-10 00:20:48 +02:00
|
|
|
// Always use blocking queue to avoid flaky tests.
|
|
|
|
options := append(option.o, sdktrace.WithBlocking())
|
2020-09-09 19:19:03 +02:00
|
|
|
return sdktrace.NewBatchSpanProcessor(te, options...)
|
2019-10-01 22:50:51 +02:00
|
|
|
}
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
func generateSpan(t *testing.T, parallel bool, tr trace.Tracer, option testOption) {
|
2019-10-01 22:50:51 +02:00
|
|
|
sc := getSpanContext()
|
|
|
|
|
2020-03-05 23:41:00 +02:00
|
|
|
wg := &sync.WaitGroup{}
|
2019-10-01 22:50:51 +02:00
|
|
|
for i := 0; i < option.genNumSpans; i++ {
|
2021-03-09 18:17:29 +02:00
|
|
|
tid := sc.TraceID()
|
|
|
|
binary.BigEndian.PutUint64(tid[0:8], uint64(i+1))
|
|
|
|
newSc := sc.WithTraceID(tid)
|
|
|
|
|
2020-03-05 23:41:00 +02:00
|
|
|
wg.Add(1)
|
2020-11-07 00:13:31 +02:00
|
|
|
f := func(sc trace.SpanContext) {
|
|
|
|
ctx := trace.ContextWithRemoteSpanContext(context.Background(), sc)
|
2020-03-05 23:41:00 +02:00
|
|
|
_, span := tr.Start(ctx, option.name)
|
|
|
|
span.End()
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
if parallel {
|
2021-03-09 18:17:29 +02:00
|
|
|
go f(newSc)
|
2020-03-05 23:41:00 +02:00
|
|
|
} else {
|
2021-03-09 18:17:29 +02:00
|
|
|
f(newSc)
|
2020-03-05 23:41:00 +02:00
|
|
|
}
|
2019-10-01 22:50:51 +02:00
|
|
|
}
|
2020-03-05 23:41:00 +02:00
|
|
|
wg.Wait()
|
2019-10-01 22:50:51 +02:00
|
|
|
}
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
func getSpanContext() trace.SpanContext {
|
|
|
|
tid, _ := trace.TraceIDFromHex("01020304050607080102040810203040")
|
|
|
|
sid, _ := trace.SpanIDFromHex("0102040810203040")
|
2021-03-09 18:17:29 +02:00
|
|
|
return trace.NewSpanContext(trace.SpanContextConfig{
|
2019-10-01 22:50:51 +02:00
|
|
|
TraceID: tid,
|
|
|
|
SpanID: sid,
|
|
|
|
TraceFlags: 0x1,
|
2021-03-09 18:17:29 +02:00
|
|
|
})
|
2019-10-08 19:34:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBatchSpanProcessorShutdown(t *testing.T) {
|
2021-01-14 03:14:03 +02:00
|
|
|
var bp testBatchExporter
|
|
|
|
bsp := sdktrace.NewBatchSpanProcessor(&bp)
|
2019-10-08 19:34:19 +02:00
|
|
|
|
2020-10-27 04:06:55 +02:00
|
|
|
err := bsp.Shutdown(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error shutting the BatchSpanProcessor down\n")
|
|
|
|
}
|
2021-01-14 03:14:03 +02:00
|
|
|
assert.Equal(t, 1, bp.shutdownCount, "shutdown from span exporter not called")
|
2019-10-08 19:34:19 +02:00
|
|
|
|
|
|
|
// Multiple call to Shutdown() should not panic.
|
2020-10-27 04:06:55 +02:00
|
|
|
err = bsp.Shutdown(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error shutting the BatchSpanProcessor down\n")
|
|
|
|
}
|
2021-01-14 03:14:03 +02:00
|
|
|
assert.Equal(t, 1, bp.shutdownCount)
|
2019-10-08 19:34:19 +02:00
|
|
|
}
|
2021-04-01 19:42:19 +02:00
|
|
|
|
2021-04-06 23:54:26 +02:00
|
|
|
func TestBatchSpanProcessorPostShutdown(t *testing.T) {
|
|
|
|
tp := basicTracerProvider(t)
|
|
|
|
be := testBatchExporter{}
|
|
|
|
bsp := sdktrace.NewBatchSpanProcessor(&be)
|
|
|
|
|
|
|
|
tp.RegisterSpanProcessor(bsp)
|
|
|
|
tr := tp.Tracer("Normal")
|
|
|
|
|
|
|
|
generateSpan(t, true, tr, testOption{
|
|
|
|
o: []sdktrace.BatchSpanProcessorOption{
|
|
|
|
sdktrace.WithMaxExportBatchSize(50),
|
|
|
|
},
|
|
|
|
genNumSpans: 60,
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, bsp.Shutdown(context.Background()), "shutting down BatchSpanProcessor")
|
|
|
|
lenJustAfterShutdown := be.len()
|
|
|
|
|
|
|
|
_, span := tr.Start(context.Background(), "foo")
|
|
|
|
span.End()
|
|
|
|
assert.NoError(t, bsp.ForceFlush(context.Background()), "force flushing BatchSpanProcessor")
|
|
|
|
|
|
|
|
assert.Equal(t, lenJustAfterShutdown, be.len(), "OnEnd and ForceFlush should have no effect after Shutdown")
|
|
|
|
}
|
|
|
|
|
2021-04-01 19:42:19 +02:00
|
|
|
func TestBatchSpanProcessorForceFlushSucceeds(t *testing.T) {
|
|
|
|
te := testBatchExporter{}
|
|
|
|
tp := basicTracerProvider(t)
|
|
|
|
option := testOption{
|
|
|
|
name: "default BatchSpanProcessorOptions",
|
|
|
|
o: []sdktrace.BatchSpanProcessorOption{
|
|
|
|
sdktrace.WithMaxQueueSize(0),
|
|
|
|
sdktrace.WithMaxExportBatchSize(3000),
|
|
|
|
},
|
|
|
|
wantNumSpans: 2053,
|
|
|
|
wantBatchCount: 1,
|
|
|
|
genNumSpans: 2053,
|
|
|
|
}
|
|
|
|
ssp := createAndRegisterBatchSP(option, &te)
|
|
|
|
if ssp == nil {
|
|
|
|
t.Fatalf("%s: Error creating new instance of BatchSpanProcessor\n", option.name)
|
|
|
|
}
|
|
|
|
tp.RegisterSpanProcessor(ssp)
|
|
|
|
tr := tp.Tracer("BatchSpanProcessorWithOption")
|
|
|
|
generateSpan(t, option.parallel, tr, option)
|
|
|
|
|
|
|
|
// Force flush any held span batches
|
|
|
|
err := ssp.ForceFlush(context.Background())
|
|
|
|
|
2021-04-29 18:42:11 +02:00
|
|
|
assertMaxSpanDiff(t, te.len(), option.wantNumSpans, 10)
|
|
|
|
|
2021-04-01 19:42:19 +02:00
|
|
|
gotBatchCount := te.getBatchCount()
|
|
|
|
if gotBatchCount < option.wantBatchCount {
|
|
|
|
t.Errorf("number batches: got %+v, want >= %+v\n",
|
|
|
|
gotBatchCount, option.wantBatchCount)
|
|
|
|
t.Errorf("Batches %v\n", te.sizes)
|
|
|
|
}
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-04-29 18:42:11 +02:00
|
|
|
func TestBatchSpanProcessorDropBatchIfFailed(t *testing.T) {
|
|
|
|
te := testBatchExporter{
|
|
|
|
errors: []error{errors.New("fail to export")},
|
|
|
|
}
|
|
|
|
tp := basicTracerProvider(t)
|
|
|
|
option := testOption{
|
|
|
|
o: []sdktrace.BatchSpanProcessorOption{
|
|
|
|
sdktrace.WithMaxQueueSize(0),
|
|
|
|
sdktrace.WithMaxExportBatchSize(2000),
|
|
|
|
},
|
|
|
|
wantNumSpans: 1000,
|
|
|
|
wantBatchCount: 1,
|
|
|
|
genNumSpans: 1000,
|
|
|
|
}
|
|
|
|
ssp := createAndRegisterBatchSP(option, &te)
|
|
|
|
if ssp == nil {
|
|
|
|
t.Fatalf("%s: Error creating new instance of BatchSpanProcessor\n", option.name)
|
|
|
|
}
|
|
|
|
tp.RegisterSpanProcessor(ssp)
|
|
|
|
tr := tp.Tracer("BatchSpanProcessorWithOption")
|
|
|
|
generateSpan(t, option.parallel, tr, option)
|
|
|
|
|
|
|
|
// Force flush any held span batches
|
|
|
|
err := ssp.ForceFlush(context.Background())
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.EqualError(t, err, "fail to export")
|
|
|
|
|
|
|
|
// First flush will fail, nothing should be exported.
|
|
|
|
assertMaxSpanDiff(t, te.droppedCount, option.wantNumSpans, 10)
|
|
|
|
assert.Equal(t, 0, te.len())
|
|
|
|
assert.Equal(t, 0, te.getBatchCount())
|
|
|
|
|
|
|
|
// Generate a new batch, this will succeed
|
|
|
|
generateSpan(t, option.parallel, tr, option)
|
|
|
|
|
|
|
|
// Force flush any held span batches
|
|
|
|
err = ssp.ForceFlush(context.Background())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assertMaxSpanDiff(t, te.len(), option.wantNumSpans, 10)
|
|
|
|
gotBatchCount := te.getBatchCount()
|
|
|
|
if gotBatchCount < option.wantBatchCount {
|
|
|
|
t.Errorf("number batches: got %+v, want >= %+v\n",
|
|
|
|
gotBatchCount, option.wantBatchCount)
|
|
|
|
t.Errorf("Batches %v\n", te.sizes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertMaxSpanDiff(t *testing.T, want, got, maxDif int) {
|
|
|
|
spanDifference := want - got
|
|
|
|
if spanDifference < 0 {
|
|
|
|
spanDifference = spanDifference * -1
|
|
|
|
}
|
|
|
|
if spanDifference > maxDif {
|
|
|
|
t.Errorf("number of exported span not equal to or within %d less than: got %+v, want %+v\n",
|
|
|
|
maxDif, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 20:01:01 +02:00
|
|
|
type indefiniteExporter struct{}
|
|
|
|
|
|
|
|
func (indefiniteExporter) Shutdown(context.Context) error { return nil }
|
2021-05-05 01:45:13 +02:00
|
|
|
func (indefiniteExporter) ExportSpans(ctx context.Context, _ []sdktrace.ReadOnlySpan) error {
|
2021-04-29 20:01:01 +02:00
|
|
|
<-ctx.Done()
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2021-04-01 19:42:19 +02:00
|
|
|
func TestBatchSpanProcessorForceFlushTimeout(t *testing.T) {
|
|
|
|
// Add timeout to context to test deadline
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
|
|
|
|
defer cancel()
|
|
|
|
<-ctx.Done()
|
|
|
|
|
2021-04-29 20:01:01 +02:00
|
|
|
bsp := sdktrace.NewBatchSpanProcessor(indefiniteExporter{})
|
|
|
|
if got, want := bsp.ForceFlush(ctx), context.DeadlineExceeded; !errors.Is(got, want) {
|
|
|
|
t.Errorf("expected %q error, got %v", want, got)
|
2021-04-01 19:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBatchSpanProcessorForceFlushCancellation(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
// Cancel the context
|
|
|
|
cancel()
|
|
|
|
|
2021-04-29 20:01:01 +02:00
|
|
|
bsp := sdktrace.NewBatchSpanProcessor(indefiniteExporter{})
|
|
|
|
if got, want := bsp.ForceFlush(ctx), context.Canceled; !errors.Is(got, want) {
|
|
|
|
t.Errorf("expected %q error, got %v", want, got)
|
2021-04-01 19:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-05 17:34:57 +02:00
|
|
|
|
|
|
|
func TestBatchSpanProcessorForceFlushQueuedSpans(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
exp := tracetest.NewInMemoryExporter()
|
|
|
|
|
|
|
|
tp := sdktrace.NewTracerProvider(
|
|
|
|
sdktrace.WithBatcher(exp),
|
|
|
|
)
|
|
|
|
|
|
|
|
tracer := tp.Tracer("tracer")
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
_, span := tracer.Start(ctx, fmt.Sprintf("span%d", i))
|
|
|
|
span.End()
|
|
|
|
|
|
|
|
err := tp.ForceFlush(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, exp.GetSpans(), i+1)
|
|
|
|
}
|
|
|
|
}
|
2022-01-11 02:58:01 +02:00
|
|
|
|
|
|
|
func BenchmarkSpanProcessor(b *testing.B) {
|
|
|
|
tp := sdktrace.NewTracerProvider(
|
|
|
|
sdktrace.WithBatcher(
|
|
|
|
tracetest.NewNoopExporter(),
|
|
|
|
sdktrace.WithMaxExportBatchSize(10),
|
|
|
|
))
|
|
|
|
tracer := tp.Tracer("bench")
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
for j := 0; j < 10; j++ {
|
|
|
|
_, span := tracer.Start(ctx, "bench")
|
|
|
|
span.End()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSpanProcessorVerboseLogging(b *testing.B) {
|
|
|
|
global.SetLogger(funcr.New(func(prefix, args string) {}, funcr.Options{Verbosity: 5}))
|
|
|
|
tp := sdktrace.NewTracerProvider(
|
|
|
|
sdktrace.WithBatcher(
|
|
|
|
tracetest.NewNoopExporter(),
|
|
|
|
sdktrace.WithMaxExportBatchSize(10),
|
|
|
|
))
|
|
|
|
tracer := tp.Tracer("bench")
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
for j := 0; j < 10; j++ {
|
|
|
|
_, span := tracer.Start(ctx, "bench")
|
|
|
|
span.End()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|