2020-10-26 18:20:49 +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.
|
|
|
|
|
|
|
|
package trace
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-11 07:28:41 +02:00
|
|
|
"errors"
|
2020-10-26 18:20:49 +02:00
|
|
|
"testing"
|
2020-12-11 07:28:41 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-05-27 21:22:38 +02:00
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2020-10-26 18:20:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type basicSpanProcesor struct {
|
2020-12-11 07:28:41 +02:00
|
|
|
running bool
|
|
|
|
injectShutdownError error
|
2020-10-26 18:20:49 +02:00
|
|
|
}
|
|
|
|
|
2020-10-27 04:06:55 +02:00
|
|
|
func (t *basicSpanProcesor) Shutdown(context.Context) error {
|
2020-10-26 18:20:49 +02:00
|
|
|
t.running = false
|
2020-12-11 07:28:41 +02:00
|
|
|
return t.injectShutdownError
|
2020-10-26 18:20:49 +02:00
|
|
|
}
|
|
|
|
|
2021-03-08 21:12:13 +02:00
|
|
|
func (t *basicSpanProcesor) OnStart(context.Context, ReadWriteSpan) {}
|
|
|
|
func (t *basicSpanProcesor) OnEnd(ReadOnlySpan) {}
|
|
|
|
func (t *basicSpanProcesor) ForceFlush(context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-26 18:20:49 +02:00
|
|
|
|
|
|
|
func TestShutdownTraceProvider(t *testing.T) {
|
|
|
|
stp := NewTracerProvider()
|
|
|
|
sp := &basicSpanProcesor{}
|
|
|
|
stp.RegisterSpanProcessor(sp)
|
|
|
|
|
|
|
|
sp.running = true
|
|
|
|
|
|
|
|
_ = stp.Shutdown(context.Background())
|
|
|
|
|
|
|
|
if sp.running != false {
|
|
|
|
t.Errorf("Error shutdown basicSpanProcesor\n")
|
|
|
|
}
|
|
|
|
}
|
2020-12-11 07:28:41 +02:00
|
|
|
|
|
|
|
func TestFailedProcessorShutdown(t *testing.T) {
|
|
|
|
stp := NewTracerProvider()
|
|
|
|
spErr := errors.New("basic span processor shutdown failure")
|
|
|
|
sp := &basicSpanProcesor{
|
|
|
|
running: true,
|
|
|
|
injectShutdownError: spErr,
|
|
|
|
}
|
|
|
|
stp.RegisterSpanProcessor(sp)
|
|
|
|
|
2021-03-08 21:12:13 +02:00
|
|
|
err := stp.Shutdown(context.Background())
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, err, spErr)
|
2020-12-11 07:28:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFailedProcessorShutdownInUnregister(t *testing.T) {
|
|
|
|
handler.Reset()
|
|
|
|
stp := NewTracerProvider()
|
|
|
|
spErr := errors.New("basic span processor shutdown failure")
|
|
|
|
sp := &basicSpanProcesor{
|
|
|
|
running: true,
|
|
|
|
injectShutdownError: spErr,
|
|
|
|
}
|
|
|
|
stp.RegisterSpanProcessor(sp)
|
|
|
|
stp.UnregisterSpanProcessor(sp)
|
|
|
|
|
|
|
|
assert.Contains(t, handler.errs, spErr)
|
|
|
|
|
2021-03-08 21:12:13 +02:00
|
|
|
err := stp.Shutdown(context.Background())
|
|
|
|
assert.NoError(t, err)
|
2020-12-11 07:28:41 +02:00
|
|
|
}
|
2021-05-27 21:22:38 +02:00
|
|
|
|
|
|
|
func TestSchemaURL(t *testing.T) {
|
|
|
|
stp := NewTracerProvider()
|
|
|
|
schemaURL := "https://opentelemetry.io/schemas/1.2.0"
|
|
|
|
tracerIface := stp.Tracer("tracername", trace.WithSchemaURL(schemaURL))
|
|
|
|
|
|
|
|
// Verify that the SchemaURL of the constructed Tracer is correctly populated.
|
|
|
|
tracerStruct := tracerIface.(*tracer)
|
|
|
|
assert.EqualValues(t, schemaURL, tracerStruct.instrumentationLibrary.SchemaURL)
|
|
|
|
}
|