2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// 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-11-16 19:30:54 +02:00
|
|
|
package global_test
|
2020-01-02 23:20:38 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-01-02 23:20:38 +02:00
|
|
|
|
2020-11-16 19:30:54 +02:00
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"go.opentelemetry.io/otel/internal/global"
|
2020-10-09 04:58:56 +02:00
|
|
|
"go.opentelemetry.io/otel/oteltest"
|
2020-01-02 23:20:38 +02:00
|
|
|
)
|
|
|
|
|
2020-07-28 19:47:08 +02:00
|
|
|
func TestTraceWithSDK(t *testing.T) {
|
2020-11-16 19:30:54 +02:00
|
|
|
global.ResetForTest()
|
2020-01-02 23:20:38 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2020-11-16 19:30:54 +02:00
|
|
|
gtp := otel.GetTracerProvider()
|
2020-01-02 23:20:38 +02:00
|
|
|
tracer1 := gtp.Tracer("pre")
|
2020-08-08 21:10:36 +02:00
|
|
|
// This is started before an SDK was registered and should be dropped.
|
2020-01-02 23:20:38 +02:00
|
|
|
_, span1 := tracer1.Start(ctx, "span1")
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
sr := new(oteltest.StandardSpanRecorder)
|
|
|
|
tp := oteltest.NewTracerProvider(oteltest.WithSpanRecorder(sr))
|
2020-11-16 19:30:54 +02:00
|
|
|
otel.SetTracerProvider(tp)
|
2020-01-02 23:20:38 +02:00
|
|
|
|
|
|
|
// This span was started before initialization, it is expected to be dropped.
|
|
|
|
span1.End()
|
|
|
|
|
|
|
|
// The existing Tracer should have been configured to now use the configured SDK.
|
|
|
|
_, span2 := tracer1.Start(ctx, "span2")
|
|
|
|
span2.End()
|
|
|
|
|
2020-09-24 00:16:13 +02:00
|
|
|
// The global TracerProvider should now create Tracers that also use the newly configured SDK.
|
2020-01-02 23:20:38 +02:00
|
|
|
tracer2 := gtp.Tracer("post")
|
|
|
|
_, span3 := tracer2.Start(ctx, "span3")
|
|
|
|
span3.End()
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
filterNames := func(spans []*oteltest.Span) []string {
|
2020-07-28 19:47:08 +02:00
|
|
|
names := make([]string, len(spans))
|
|
|
|
for i := range spans {
|
|
|
|
names[i] = spans[i].Name()
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
2020-08-08 21:10:36 +02:00
|
|
|
expected := []string{"span2", "span3"}
|
2020-07-28 19:47:08 +02:00
|
|
|
assert.ElementsMatch(t, expected, filterNames(sr.Started()))
|
|
|
|
assert.ElementsMatch(t, expected, filterNames(sr.Completed()))
|
2020-01-02 23:20:38 +02:00
|
|
|
}
|