2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-09-16 22:58:15 +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"
|
2021-04-28 19:15:00 +02:00
|
|
|
"errors"
|
2019-09-16 22:58:15 +02:00
|
|
|
"testing"
|
2021-04-28 19:15:00 +02:00
|
|
|
"time"
|
2019-09-16 22:58:15 +02:00
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2019-09-16 22:58:15 +02:00
|
|
|
)
|
|
|
|
|
2021-03-08 19:50:15 +02:00
|
|
|
var (
|
|
|
|
tid, _ = trace.TraceIDFromHex("01020304050607080102040810203040")
|
|
|
|
sid, _ = trace.SpanIDFromHex("0102040810203040")
|
|
|
|
)
|
|
|
|
|
2019-09-16 22:58:15 +02:00
|
|
|
type testExporter struct {
|
2021-04-07 17:03:43 +02:00
|
|
|
spans []*sdktrace.SpanSnapshot
|
2021-03-08 19:50:15 +02:00
|
|
|
shutdown bool
|
2019-09-16 22:58:15 +02:00
|
|
|
}
|
|
|
|
|
2021-04-07 17:03:43 +02:00
|
|
|
func (t *testExporter) ExportSpans(ctx context.Context, ss []*sdktrace.SpanSnapshot) error {
|
2020-12-11 07:15:44 +02:00
|
|
|
t.spans = append(t.spans, ss...)
|
2020-09-09 19:19:03 +02:00
|
|
|
return nil
|
2019-09-16 22:58:15 +02:00
|
|
|
}
|
|
|
|
|
2021-03-08 19:50:15 +02:00
|
|
|
func (t *testExporter) Shutdown(context.Context) error {
|
|
|
|
t.shutdown = true
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-09 19:19:03 +02:00
|
|
|
|
2021-04-07 17:03:43 +02:00
|
|
|
var _ sdktrace.SpanExporter = (*testExporter)(nil)
|
2019-09-16 22:58:15 +02:00
|
|
|
|
|
|
|
func TestNewSimpleSpanProcessor(t *testing.T) {
|
2021-03-08 19:50:15 +02:00
|
|
|
if ssp := sdktrace.NewSimpleSpanProcessor(&testExporter{}); ssp == nil {
|
|
|
|
t.Error("failed to create new SimpleSpanProcessor")
|
2019-09-16 22:58:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewSimpleSpanProcessorWithNilExporter(t *testing.T) {
|
2021-03-08 19:50:15 +02:00
|
|
|
if ssp := sdktrace.NewSimpleSpanProcessor(nil); ssp == nil {
|
|
|
|
t.Error("failed to create new SimpleSpanProcessor with nil exporter")
|
2019-09-16 22:58:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 19:50:15 +02:00
|
|
|
func startSpan(tp trace.TracerProvider) trace.Span {
|
2019-11-25 19:46:07 +02:00
|
|
|
tr := tp.Tracer("SimpleSpanProcessor")
|
2021-03-09 18:17:29 +02:00
|
|
|
sc := trace.NewSpanContext(trace.SpanContextConfig{
|
2019-09-25 23:37:36 +02:00
|
|
|
TraceID: tid,
|
|
|
|
SpanID: sid,
|
|
|
|
TraceFlags: 0x1,
|
2021-03-09 18:17:29 +02:00
|
|
|
})
|
2020-11-07 00:13:31 +02:00
|
|
|
ctx := trace.ContextWithRemoteSpanContext(context.Background(), sc)
|
2020-02-04 18:55:03 +02:00
|
|
|
_, span := tr.Start(ctx, "OnEnd")
|
2021-03-08 19:50:15 +02:00
|
|
|
return span
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleSpanProcessorOnEnd(t *testing.T) {
|
|
|
|
tp := basicTracerProvider(t)
|
|
|
|
te := testExporter{}
|
|
|
|
ssp := sdktrace.NewSimpleSpanProcessor(&te)
|
|
|
|
|
|
|
|
tp.RegisterSpanProcessor(ssp)
|
|
|
|
startSpan(tp).End()
|
2019-09-16 22:58:15 +02:00
|
|
|
|
|
|
|
wantTraceID := tid
|
2021-03-09 18:17:29 +02:00
|
|
|
gotTraceID := te.spans[0].SpanContext.TraceID()
|
2019-09-16 22:58:15 +02:00
|
|
|
if wantTraceID != gotTraceID {
|
|
|
|
t.Errorf("SimplerSpanProcessor OnEnd() check: got %+v, want %+v\n", gotTraceID, wantTraceID)
|
|
|
|
}
|
|
|
|
}
|
2019-10-08 19:34:19 +02:00
|
|
|
|
|
|
|
func TestSimpleSpanProcessorShutdown(t *testing.T) {
|
2021-03-08 19:50:15 +02:00
|
|
|
exporter := &testExporter{}
|
|
|
|
ssp := sdktrace.NewSimpleSpanProcessor(exporter)
|
|
|
|
|
|
|
|
// Ensure we can export a span before we test we cannot after shutdown.
|
|
|
|
tp := basicTracerProvider(t)
|
|
|
|
tp.RegisterSpanProcessor(ssp)
|
|
|
|
startSpan(tp).End()
|
|
|
|
nExported := len(exporter.spans)
|
|
|
|
if nExported != 1 {
|
|
|
|
t.Error("failed to verify span export")
|
2019-10-08 19:34:19 +02:00
|
|
|
}
|
|
|
|
|
2021-03-08 19:50:15 +02:00
|
|
|
if err := ssp.Shutdown(context.Background()); err != nil {
|
|
|
|
t.Errorf("shutting the SimpleSpanProcessor down: %v", err)
|
2020-10-27 04:06:55 +02:00
|
|
|
}
|
2021-03-08 19:50:15 +02:00
|
|
|
if !exporter.shutdown {
|
|
|
|
t.Error("SimpleSpanProcessor.Shutdown did not shut down exporter")
|
|
|
|
}
|
|
|
|
|
|
|
|
startSpan(tp).End()
|
|
|
|
if len(exporter.spans) > nExported {
|
|
|
|
t.Error("exported span to shutdown exporter")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleSpanProcessorShutdownOnEndConcurrency(t *testing.T) {
|
|
|
|
exporter := &testExporter{}
|
|
|
|
ssp := sdktrace.NewSimpleSpanProcessor(exporter)
|
|
|
|
tp := basicTracerProvider(t)
|
|
|
|
tp.RegisterSpanProcessor(ssp)
|
|
|
|
|
|
|
|
stop := make(chan struct{})
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
startSpan(tp).End()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := ssp.Shutdown(context.Background()); err != nil {
|
|
|
|
t.Errorf("shutting the SimpleSpanProcessor down: %v", err)
|
|
|
|
}
|
|
|
|
if !exporter.shutdown {
|
|
|
|
t.Error("SimpleSpanProcessor.Shutdown did not shut down exporter")
|
|
|
|
}
|
|
|
|
|
|
|
|
stop <- struct{}{}
|
|
|
|
<-done
|
2019-10-08 19:34:19 +02:00
|
|
|
}
|
2021-04-28 19:15:00 +02:00
|
|
|
|
|
|
|
func TestSimpleSpanProcessorShutdownHonorsContextDeadline(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
|
|
|
|
defer cancel()
|
|
|
|
<-ctx.Done()
|
|
|
|
|
|
|
|
ssp := sdktrace.NewSimpleSpanProcessor(&testExporter{})
|
|
|
|
if got, want := ssp.Shutdown(ctx), context.DeadlineExceeded; !errors.Is(got, want) {
|
|
|
|
t.Errorf("SimpleSpanProcessor.Shutdown did not return %v, got %v", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleSpanProcessorShutdownHonorsContextCancel(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
ssp := sdktrace.NewSimpleSpanProcessor(&testExporter{})
|
|
|
|
if got, want := ssp.Shutdown(ctx), context.Canceled; !errors.Is(got, want) {
|
|
|
|
t.Errorf("SimpleSpanProcessor.Shutdown did not return %v, got %v", want, got)
|
|
|
|
}
|
|
|
|
}
|