2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-12-05 00:00:35 +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.
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
package oteltest_test
|
2019-12-05 00:00:35 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-28 19:47:08 +02:00
|
|
|
"fmt"
|
2019-12-05 00:00:35 +02:00
|
|
|
"sync"
|
2020-07-28 19:47:08 +02:00
|
|
|
"sync/atomic"
|
2019-12-05 00:00:35 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/internal/matchers"
|
2020-08-18 05:25:03 +02:00
|
|
|
"go.opentelemetry.io/otel/label"
|
2020-10-09 04:58:56 +02:00
|
|
|
"go.opentelemetry.io/otel/oteltest"
|
2020-11-07 00:13:31 +02:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2019-12-05 00:00:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTracer(t *testing.T) {
|
2020-10-09 04:58:56 +02:00
|
|
|
tp := oteltest.NewTracerProvider()
|
2020-07-28 19:47:08 +02:00
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
oteltest.NewHarness(t).TestTracer(func() func() trace.Tracer {
|
2020-10-09 04:58:56 +02:00
|
|
|
tp := oteltest.NewTracerProvider()
|
2020-07-28 19:47:08 +02:00
|
|
|
var i uint64
|
2020-11-07 00:13:31 +02:00
|
|
|
return func() trace.Tracer {
|
2020-07-28 19:47:08 +02:00
|
|
|
return tp.Tracer(fmt.Sprintf("tracer %d", atomic.AddUint64(&i, 1)))
|
|
|
|
}
|
|
|
|
}())
|
2019-12-05 00:00:35 +02:00
|
|
|
|
|
|
|
t.Run("#Start", func(t *testing.T) {
|
2020-11-07 00:13:31 +02:00
|
|
|
testTracedSpan(t, func(tracer trace.Tracer, name string) (trace.Span, error) {
|
2019-12-05 00:00:35 +02:00
|
|
|
_, span := tracer.Start(context.Background(), name)
|
|
|
|
|
|
|
|
return span, nil
|
|
|
|
})
|
|
|
|
|
2020-09-03 16:34:36 +02:00
|
|
|
t.Run("uses the start time from WithTimestamp", func(t *testing.T) {
|
2019-12-05 00:00:35 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
e := matchers.NewExpecter(t)
|
|
|
|
|
|
|
|
expectedStartTime := time.Now().AddDate(5, 0, 0)
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
subject := tp.Tracer(t.Name())
|
2020-11-07 00:13:31 +02:00
|
|
|
_, span := subject.Start(context.Background(), "test", trace.WithTimestamp(expectedStartTime))
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
testSpan, ok := span.(*oteltest.Span)
|
2019-12-05 00:00:35 +02:00
|
|
|
e.Expect(ok).ToBeTrue()
|
|
|
|
|
|
|
|
e.Expect(testSpan.StartTime()).ToEqual(expectedStartTime)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("uses the attributes from WithAttributes", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
e := matchers.NewExpecter(t)
|
|
|
|
|
2020-08-18 05:25:03 +02:00
|
|
|
attr1 := label.String("a", "1")
|
|
|
|
attr2 := label.String("b", "2")
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
subject := tp.Tracer(t.Name())
|
2020-11-07 00:13:31 +02:00
|
|
|
_, span := subject.Start(context.Background(), "test", trace.WithAttributes(attr1, attr2))
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
testSpan, ok := span.(*oteltest.Span)
|
2019-12-05 00:00:35 +02:00
|
|
|
e.Expect(ok).ToBeTrue()
|
|
|
|
|
|
|
|
attributes := testSpan.Attributes()
|
|
|
|
e.Expect(attributes[attr1.Key]).ToEqual(attr1.Value)
|
|
|
|
e.Expect(attributes[attr2.Key]).ToEqual(attr2.Value)
|
|
|
|
})
|
|
|
|
|
2020-02-04 18:55:03 +02:00
|
|
|
t.Run("uses the current span from context as parent", func(t *testing.T) {
|
2019-12-05 00:00:35 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
e := matchers.NewExpecter(t)
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
subject := tp.Tracer(t.Name())
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-02-04 18:55:03 +02:00
|
|
|
parent, parentSpan := subject.Start(context.Background(), "parent")
|
|
|
|
parentSpanContext := parentSpan.SpanContext()
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-02-04 18:55:03 +02:00
|
|
|
_, span := subject.Start(parent, "child")
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
testSpan, ok := span.(*oteltest.Span)
|
2019-12-05 00:00:35 +02:00
|
|
|
e.Expect(ok).ToBeTrue()
|
|
|
|
|
|
|
|
childSpanContext := testSpan.SpanContext()
|
|
|
|
e.Expect(childSpanContext.TraceID).ToEqual(parentSpanContext.TraceID)
|
|
|
|
e.Expect(childSpanContext.SpanID).NotToEqual(parentSpanContext.SpanID)
|
|
|
|
e.Expect(testSpan.ParentSpanID()).ToEqual(parentSpanContext.SpanID)
|
|
|
|
})
|
|
|
|
|
2020-02-04 18:55:03 +02:00
|
|
|
t.Run("uses the current span from context as parent, even if it has remote span context", func(t *testing.T) {
|
2019-12-05 00:00:35 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
e := matchers.NewExpecter(t)
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
subject := tp.Tracer(t.Name())
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-02-04 18:55:03 +02:00
|
|
|
parent, parentSpan := subject.Start(context.Background(), "parent")
|
|
|
|
_, remoteParentSpan := subject.Start(context.Background(), "remote not-a-parent")
|
2020-11-07 00:13:31 +02:00
|
|
|
parent = trace.ContextWithRemoteSpanContext(parent, remoteParentSpan.SpanContext())
|
2020-02-04 18:55:03 +02:00
|
|
|
parentSpanContext := parentSpan.SpanContext()
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-02-04 18:55:03 +02:00
|
|
|
_, span := subject.Start(parent, "child")
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
testSpan, ok := span.(*oteltest.Span)
|
2019-12-05 00:00:35 +02:00
|
|
|
e.Expect(ok).ToBeTrue()
|
|
|
|
|
|
|
|
childSpanContext := testSpan.SpanContext()
|
|
|
|
e.Expect(childSpanContext.TraceID).ToEqual(parentSpanContext.TraceID)
|
|
|
|
e.Expect(childSpanContext.SpanID).NotToEqual(parentSpanContext.SpanID)
|
|
|
|
e.Expect(testSpan.ParentSpanID()).ToEqual(parentSpanContext.SpanID)
|
|
|
|
})
|
|
|
|
|
2020-02-04 18:55:03 +02:00
|
|
|
t.Run("uses the remote span context from context as parent, if current span is missing", func(t *testing.T) {
|
2019-12-05 00:00:35 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
e := matchers.NewExpecter(t)
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
subject := tp.Tracer(t.Name())
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-02-04 18:55:03 +02:00
|
|
|
_, remoteParentSpan := subject.Start(context.Background(), "remote parent")
|
2020-11-07 00:13:31 +02:00
|
|
|
parent := trace.ContextWithRemoteSpanContext(context.Background(), remoteParentSpan.SpanContext())
|
2020-02-04 18:55:03 +02:00
|
|
|
remoteParentSpanContext := remoteParentSpan.SpanContext()
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-02-04 18:55:03 +02:00
|
|
|
_, span := subject.Start(parent, "child")
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
testSpan, ok := span.(*oteltest.Span)
|
2019-12-05 00:00:35 +02:00
|
|
|
e.Expect(ok).ToBeTrue()
|
|
|
|
|
|
|
|
childSpanContext := testSpan.SpanContext()
|
2020-02-04 18:55:03 +02:00
|
|
|
e.Expect(childSpanContext.TraceID).ToEqual(remoteParentSpanContext.TraceID)
|
|
|
|
e.Expect(childSpanContext.SpanID).NotToEqual(remoteParentSpanContext.SpanID)
|
|
|
|
e.Expect(testSpan.ParentSpanID()).ToEqual(remoteParentSpanContext.SpanID)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("creates new root when both current span and remote span context are missing", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
e := matchers.NewExpecter(t)
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
subject := tp.Tracer(t.Name())
|
2020-02-04 18:55:03 +02:00
|
|
|
|
|
|
|
_, parentSpan := subject.Start(context.Background(), "not-a-parent")
|
|
|
|
_, remoteParentSpan := subject.Start(context.Background(), "remote not-a-parent")
|
|
|
|
parentSpanContext := parentSpan.SpanContext()
|
|
|
|
remoteParentSpanContext := remoteParentSpan.SpanContext()
|
|
|
|
|
|
|
|
_, span := subject.Start(context.Background(), "child")
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
testSpan, ok := span.(*oteltest.Span)
|
2020-02-04 18:55:03 +02:00
|
|
|
e.Expect(ok).ToBeTrue()
|
|
|
|
|
|
|
|
childSpanContext := testSpan.SpanContext()
|
|
|
|
e.Expect(childSpanContext.TraceID).NotToEqual(parentSpanContext.TraceID)
|
|
|
|
e.Expect(childSpanContext.TraceID).NotToEqual(remoteParentSpanContext.TraceID)
|
2019-12-05 00:00:35 +02:00
|
|
|
e.Expect(childSpanContext.SpanID).NotToEqual(parentSpanContext.SpanID)
|
2020-02-04 18:55:03 +02:00
|
|
|
e.Expect(childSpanContext.SpanID).NotToEqual(remoteParentSpanContext.SpanID)
|
|
|
|
e.Expect(testSpan.ParentSpanID().IsValid()).ToBeFalse()
|
2019-12-05 00:00:35 +02:00
|
|
|
})
|
|
|
|
|
2020-02-04 18:55:03 +02:00
|
|
|
t.Run("creates new root when requested, even if both current span and remote span context are in context", func(t *testing.T) {
|
2019-12-05 00:00:35 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
e := matchers.NewExpecter(t)
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
subject := tp.Tracer(t.Name())
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-02-04 18:55:03 +02:00
|
|
|
parentCtx, parentSpan := subject.Start(context.Background(), "not-a-parent")
|
|
|
|
_, remoteParentSpan := subject.Start(context.Background(), "remote not-a-parent")
|
|
|
|
parentSpanContext := parentSpan.SpanContext()
|
|
|
|
remoteParentSpanContext := remoteParentSpan.SpanContext()
|
2020-11-07 00:13:31 +02:00
|
|
|
parentCtx = trace.ContextWithRemoteSpanContext(parentCtx, remoteParentSpanContext)
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
_, span := subject.Start(parentCtx, "child", trace.WithNewRoot())
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
testSpan, ok := span.(*oteltest.Span)
|
2019-12-05 00:00:35 +02:00
|
|
|
e.Expect(ok).ToBeTrue()
|
|
|
|
|
|
|
|
childSpanContext := testSpan.SpanContext()
|
2020-02-04 18:55:03 +02:00
|
|
|
e.Expect(childSpanContext.TraceID).NotToEqual(parentSpanContext.TraceID)
|
|
|
|
e.Expect(childSpanContext.TraceID).NotToEqual(remoteParentSpanContext.TraceID)
|
2019-12-05 00:00:35 +02:00
|
|
|
e.Expect(childSpanContext.SpanID).NotToEqual(parentSpanContext.SpanID)
|
2020-02-04 18:55:03 +02:00
|
|
|
e.Expect(childSpanContext.SpanID).NotToEqual(remoteParentSpanContext.SpanID)
|
|
|
|
e.Expect(testSpan.ParentSpanID().IsValid()).ToBeFalse()
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
expectedLinks := []trace.Link{
|
2020-02-04 18:55:03 +02:00
|
|
|
{
|
|
|
|
SpanContext: parentSpanContext,
|
2020-08-18 05:25:03 +02:00
|
|
|
Attributes: []label.KeyValue{
|
|
|
|
label.String("ignored-on-demand", "current"),
|
2020-02-04 18:55:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SpanContext: remoteParentSpanContext,
|
2020-08-18 05:25:03 +02:00
|
|
|
Attributes: []label.KeyValue{
|
|
|
|
label.String("ignored-on-demand", "remote"),
|
2020-02-04 18:55:03 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-12-21 23:11:48 +02:00
|
|
|
gotLinks := testSpan.Links()
|
2020-02-04 18:55:03 +02:00
|
|
|
e.Expect(gotLinks).ToMatchInAnyOrder(expectedLinks)
|
2019-12-05 00:00:35 +02:00
|
|
|
})
|
|
|
|
|
2020-09-03 16:34:36 +02:00
|
|
|
t.Run("uses the links provided through WithLinks", func(t *testing.T) {
|
2019-12-05 00:00:35 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
e := matchers.NewExpecter(t)
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
subject := tp.Tracer(t.Name())
|
2019-12-05 00:00:35 +02:00
|
|
|
|
|
|
|
_, span := subject.Start(context.Background(), "link1")
|
2020-11-07 00:13:31 +02:00
|
|
|
link1 := trace.Link{
|
2019-12-05 00:00:35 +02:00
|
|
|
SpanContext: span.SpanContext(),
|
2020-08-18 05:25:03 +02:00
|
|
|
Attributes: []label.KeyValue{
|
|
|
|
label.String("a", "1"),
|
2019-12-05 00:00:35 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, span = subject.Start(context.Background(), "link2")
|
2020-11-07 00:13:31 +02:00
|
|
|
link2 := trace.Link{
|
2019-12-05 00:00:35 +02:00
|
|
|
SpanContext: span.SpanContext(),
|
2020-08-18 05:25:03 +02:00
|
|
|
Attributes: []label.KeyValue{
|
|
|
|
label.String("b", "2"),
|
2019-12-05 00:00:35 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
_, span = subject.Start(context.Background(), "test", trace.WithLinks(link1, link2))
|
2019-12-05 00:00:35 +02:00
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
testSpan, ok := span.(*oteltest.Span)
|
2019-12-05 00:00:35 +02:00
|
|
|
e.Expect(ok).ToBeTrue()
|
|
|
|
|
|
|
|
links := testSpan.Links()
|
2020-12-21 23:11:48 +02:00
|
|
|
e.Expect(links[0].Attributes).ToEqual(link1.Attributes)
|
|
|
|
e.Expect(links[1].Attributes).ToEqual(link2.Attributes)
|
2019-12-05 00:00:35 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-07 00:13:31 +02:00
|
|
|
func testTracedSpan(t *testing.T, fn func(tracer trace.Tracer, name string) (trace.Span, error)) {
|
2020-10-09 04:58:56 +02:00
|
|
|
tp := oteltest.NewTracerProvider()
|
2019-12-05 00:00:35 +02:00
|
|
|
t.Run("starts a span with the expected name", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
e := matchers.NewExpecter(t)
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
subject := tp.Tracer(t.Name())
|
2019-12-05 00:00:35 +02:00
|
|
|
|
|
|
|
expectedName := "test name"
|
|
|
|
span, err := fn(subject, expectedName)
|
|
|
|
|
|
|
|
e.Expect(err).ToBeNil()
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
testSpan, ok := span.(*oteltest.Span)
|
2019-12-05 00:00:35 +02:00
|
|
|
e.Expect(ok).ToBeTrue()
|
|
|
|
|
|
|
|
e.Expect(testSpan.Name()).ToEqual(expectedName)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("uses the current time as the start time", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
e := matchers.NewExpecter(t)
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
subject := tp.Tracer(t.Name())
|
2019-12-05 00:00:35 +02:00
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
span, err := fn(subject, "test")
|
|
|
|
end := time.Now()
|
|
|
|
|
|
|
|
e.Expect(err).ToBeNil()
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
testSpan, ok := span.(*oteltest.Span)
|
2019-12-05 00:00:35 +02:00
|
|
|
e.Expect(ok).ToBeTrue()
|
|
|
|
|
|
|
|
e.Expect(testSpan.StartTime()).ToBeTemporally(matchers.AfterOrSameTime, start)
|
|
|
|
e.Expect(testSpan.StartTime()).ToBeTemporally(matchers.BeforeOrSameTime, end)
|
|
|
|
})
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
t.Run("calls SpanRecorder.OnStart", func(t *testing.T) {
|
2019-12-05 00:00:35 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
e := matchers.NewExpecter(t)
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
sr := new(oteltest.StandardSpanRecorder)
|
|
|
|
subject := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr)).Tracer(t.Name())
|
2019-12-05 00:00:35 +02:00
|
|
|
subject.Start(context.Background(), "span1")
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
e.Expect(len(sr.Started())).ToEqual(1)
|
2019-12-05 00:00:35 +02:00
|
|
|
|
|
|
|
span, err := fn(subject, "span2")
|
|
|
|
e.Expect(err).ToBeNil()
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
spans := sr.Started()
|
2019-12-05 00:00:35 +02:00
|
|
|
|
|
|
|
e.Expect(len(spans)).ToEqual(2)
|
|
|
|
e.Expect(spans[1]).ToEqual(span)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("can be run concurrently with another call", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
e := matchers.NewExpecter(t)
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
sr := new(oteltest.StandardSpanRecorder)
|
|
|
|
subject := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr)).Tracer(t.Name())
|
2019-12-05 00:00:35 +02:00
|
|
|
|
|
|
|
numSpans := 2
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
wg.Add(numSpans)
|
|
|
|
|
|
|
|
for i := 0; i < numSpans; i++ {
|
|
|
|
go func() {
|
|
|
|
_, err := fn(subject, "test")
|
|
|
|
e.Expect(err).ToBeNil()
|
|
|
|
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
e.Expect(len(sr.Started())).ToEqual(numSpans)
|
2019-12-05 00:00:35 +02:00
|
|
|
})
|
|
|
|
}
|