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"
|
2019-10-01 22:50:51 +02:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-01-14 03:14:03 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
|
2019-11-05 23:08:55 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/trace"
|
2019-11-01 20:40:29 +02:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2019-10-01 22:50:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type testBatchExporter struct {
|
2021-01-14 03:14:03 +02:00
|
|
|
mu sync.Mutex
|
|
|
|
spans []*export.SpanSnapshot
|
|
|
|
sizes []int
|
|
|
|
batchCount int
|
|
|
|
shutdownCount int
|
2019-10-01 22:50:51 +02:00
|
|
|
}
|
|
|
|
|
2020-12-11 07:15:44 +02:00
|
|
|
func (t *testBatchExporter) ExportSpans(ctx context.Context, ss []*export.SpanSnapshot) error {
|
2019-10-01 22:50:51 +02:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
2019-10-08 20:56:58 +02:00
|
|
|
|
2020-12-11 07:15:44 +02:00
|
|
|
t.spans = append(t.spans, ss...)
|
|
|
|
t.sizes = append(t.sizes, len(ss))
|
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
|
|
|
|
}
|
|
|
|
|
2020-09-09 19:19:03 +02:00
|
|
|
var _ export.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 {
|
|
|
|
name string
|
|
|
|
o []sdktrace.BatchSpanProcessorOption
|
|
|
|
wantNumSpans int
|
|
|
|
wantBatchCount int
|
|
|
|
genNumSpans int
|
2020-03-05 23:41:00 +02:00
|
|
|
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) {
|
|
|
|
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()
|
|
|
|
if option.wantNumSpans != gotNumOfSpans {
|
|
|
|
t.Errorf("number of exported span: got %+v, want %+v\n",
|
|
|
|
gotNumOfSpans, option.wantNumSpans)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2020-05-19 18:36:33 +02:00
|
|
|
})
|
2019-10-01 22:50:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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())
|
|
|
|
|
|
|
|
gotNumOfSpans := te.len()
|
|
|
|
spanDifference := option.wantNumSpans - gotNumOfSpans
|
|
|
|
if spanDifference > 10 || spanDifference < 0 {
|
|
|
|
t.Errorf("number of exported span not equal to or within 10 less than: got %+v, want %+v\n",
|
|
|
|
gotNumOfSpans, option.wantNumSpans)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBatchSpanProcessorForceFlushTimeout(t *testing.T) {
|
|
|
|
var bp testBatchExporter
|
|
|
|
bsp := sdktrace.NewBatchSpanProcessor(&bp)
|
|
|
|
// Add timeout to context to test deadline
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
|
|
|
|
defer cancel()
|
|
|
|
<-ctx.Done()
|
|
|
|
|
|
|
|
if err := bsp.ForceFlush(ctx); err == nil {
|
|
|
|
t.Error("expected context DeadlineExceeded error, got nil")
|
|
|
|
} else if !errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
t.Errorf("expected context DeadlineExceeded error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBatchSpanProcessorForceFlushCancellation(t *testing.T) {
|
|
|
|
var bp testBatchExporter
|
|
|
|
bsp := sdktrace.NewBatchSpanProcessor(&bp)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
// Cancel the context
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
if err := bsp.ForceFlush(ctx); err == nil {
|
|
|
|
t.Error("expected context canceled error, got nil")
|
|
|
|
} else if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Errorf("expected context canceled error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|