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"
|
2019-10-01 22:50:51 +02:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
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 {
|
|
|
|
mu sync.Mutex
|
2019-10-08 20:56:58 +02:00
|
|
|
spans []*export.SpanData
|
2019-10-01 22:50:51 +02:00
|
|
|
sizes []int
|
|
|
|
batchCount int
|
|
|
|
}
|
|
|
|
|
2020-09-09 19:19:03 +02:00
|
|
|
func (t *testBatchExporter) ExportSpans(ctx context.Context, sds []*export.SpanData) error {
|
2019-10-01 22:50:51 +02:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
2019-10-08 20:56:58 +02:00
|
|
|
|
2019-10-01 22:50:51 +02:00
|
|
|
t.spans = append(t.spans, sds...)
|
|
|
|
t.sizes = append(t.sizes, len(sds))
|
|
|
|
t.batchCount++
|
2020-09-09 19:19:03 +02:00
|
|
|
return nil
|
2019-10-01 22:50:51 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 19:19:03 +02:00
|
|
|
func (t *testBatchExporter) Shutdown(context.Context) error { return nil }
|
|
|
|
|
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-09-09 19:19:03 +02:00
|
|
|
bsp := sdktrace.NewBatchSpanProcessor(nil)
|
|
|
|
// These should not panic.
|
2020-11-16 18:45:49 +02:00
|
|
|
bsp.OnStart(context.Background(), &export.SpanData{})
|
2020-09-09 19:19:03 +02:00
|
|
|
bsp.OnEnd(&export.SpanData{})
|
2020-09-20 19:35:44 +02:00
|
|
|
bsp.ForceFlush()
|
2020-10-27 04:06:55 +02:00
|
|
|
err := bsp.Shutdown(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error shutting the BatchSpanProcessor down\n")
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 19:19:03 +02:00
|
|
|
func createAndRegisterBatchSP(option testOption, te *testBatchExporter) *sdktrace.BatchSpanProcessor {
|
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++ {
|
2019-10-23 08:01:33 +02:00
|
|
|
binary.BigEndian.PutUint64(sc.TraceID[0:8], uint64(i+1))
|
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 {
|
|
|
|
go f(sc)
|
|
|
|
} else {
|
|
|
|
f(sc)
|
|
|
|
}
|
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")
|
|
|
|
return trace.SpanContext{
|
2019-10-01 22:50:51 +02:00
|
|
|
TraceID: tid,
|
|
|
|
SpanID: sid,
|
|
|
|
TraceFlags: 0x1,
|
|
|
|
}
|
2019-10-08 19:34:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBatchSpanProcessorShutdown(t *testing.T) {
|
2020-09-09 19:19:03 +02:00
|
|
|
bsp := sdktrace.NewBatchSpanProcessor(&testBatchExporter{})
|
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")
|
|
|
|
}
|
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")
|
|
|
|
}
|
2019-10-08 19:34:19 +02:00
|
|
|
}
|