1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-30 04:40:41 +02:00

Rename global SetTraceProvider and TraceProvider (#1102)

* Rename global {Set,}TraceProvider

Resolves #1101

* Rename unexported and testing vars
This commit is contained in:
Tyler Yahn 2020-08-31 10:02:04 -07:00 committed by GitHub
parent f36759aa47
commit 1ab708e4b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 55 additions and 55 deletions

View File

@ -25,7 +25,7 @@ import (
)
type (
traceProviderHolder struct {
tracerProviderHolder struct {
tp trace.Provider
}
@ -47,26 +47,26 @@ var (
delegateTraceOnce sync.Once
)
// TraceProvider is the internal implementation for global.TraceProvider.
func TraceProvider() trace.Provider {
return globalTracer.Load().(traceProviderHolder).tp
// TracerProvider is the internal implementation for global.TracerProvider.
func TracerProvider() trace.Provider {
return globalTracer.Load().(tracerProviderHolder).tp
}
// SetTraceProvider is the internal implementation for global.SetTraceProvider.
func SetTraceProvider(tp trace.Provider) {
// SetTracerProvider is the internal implementation for global.SetTracerProvider.
func SetTracerProvider(tp trace.Provider) {
delegateTraceOnce.Do(func() {
current := TraceProvider()
current := TracerProvider()
if current == tp {
// Setting the provider to the prior default is nonsense, panic.
// Panic is acceptable because we are likely still early in the
// process lifetime.
panic("invalid Provider, the global instance cannot be reinstalled")
} else if def, ok := current.(*traceProvider); ok {
} else if def, ok := current.(*tracerProvider); ok {
def.setDelegate(tp)
}
})
globalTracer.Store(traceProviderHolder{tp: tp})
globalTracer.Store(tracerProviderHolder{tp: tp})
}
// MeterProvider is the internal implementation for global.MeterProvider.
@ -103,7 +103,7 @@ func SetPropagators(pr propagation.Propagators) {
func defaultTracerValue() *atomic.Value {
v := &atomic.Value{}
v.Store(traceProviderHolder{tp: &traceProvider{}})
v.Store(tracerProviderHolder{tp: &tracerProvider{}})
return v
}

View File

@ -38,18 +38,18 @@ import (
"go.opentelemetry.io/otel/api/trace"
)
// traceProvider is a placeholder for a configured SDK Provider.
// tracerProvider is a placeholder for a configured SDK Provider.
//
// All Provider functionality is forwarded to a delegate once configured.
type traceProvider struct {
type tracerProvider struct {
mtx sync.Mutex
tracers []*tracer
delegate trace.Provider
}
// Compile-time guarantee that traceProvider implements the trace.Provider interface.
var _ trace.Provider = &traceProvider{}
// Compile-time guarantee that tracerProvider implements the trace.Provider interface.
var _ trace.Provider = &tracerProvider{}
// setDelegate configures p to delegate all Provider functionality to provider.
//
@ -58,7 +58,7 @@ var _ trace.Provider = &traceProvider{}
//
// Delegation only happens on the first call to this method. All subsequent
// calls result in no delegation changes.
func (p *traceProvider) setDelegate(provider trace.Provider) {
func (p *tracerProvider) setDelegate(provider trace.Provider) {
if p.delegate != nil {
return
}
@ -75,7 +75,7 @@ func (p *traceProvider) setDelegate(provider trace.Provider) {
}
// Tracer implements trace.Provider.
func (p *traceProvider) Tracer(name string, opts ...trace.TracerOption) trace.Tracer {
func (p *tracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.Tracer {
p.mtx.Lock()
defer p.mtx.Unlock()

View File

@ -29,14 +29,14 @@ func TestTraceWithSDK(t *testing.T) {
internal.ResetForTest()
ctx := context.Background()
gtp := global.TraceProvider()
gtp := global.TracerProvider()
tracer1 := gtp.Tracer("pre")
// This is started before an SDK was registered and should be dropped.
_, span1 := tracer1.Start(ctx, "span1")
sr := new(tracetest.StandardSpanRecorder)
tp := tracetest.NewProvider(tracetest.WithSpanRecorder(sr))
global.SetTraceProvider(tp)
global.SetTracerProvider(tp)
// This span was started before initialization, it is expected to be dropped.
span1.End()

View File

@ -22,23 +22,23 @@ import (
// Tracer creates a named tracer that implements Tracer interface.
// If the name is an empty string then provider uses default name.
//
// This is short for TraceProvider().Tracer(name)
// This is short for TracerProvider().Tracer(name)
func Tracer(name string) trace.Tracer {
return TraceProvider().Tracer(name)
return TracerProvider().Tracer(name)
}
// TraceProvider returns the registered global trace provider.
// TracerProvider returns the registered global trace provider.
// If none is registered then an instance of trace.NoopProvider is returned.
//
// Use the trace provider to create a named tracer. E.g.
// tracer := global.TraceProvider().Tracer("example.com/foo")
// tracer := global.TracerProvider().Tracer("example.com/foo")
// or
// tracer := global.Tracer("example.com/foo")
func TraceProvider() trace.Provider {
return internal.TraceProvider()
func TracerProvider() trace.Provider {
return internal.TracerProvider()
}
// SetTraceProvider registers `tp` as the global trace provider.
func SetTraceProvider(tp trace.Provider) {
internal.SetTraceProvider(tp)
// SetTracerProvider registers `tp` as the global trace provider.
func SetTracerProvider(tp trace.Provider) {
internal.SetTracerProvider(tp)
}

View File

@ -21,21 +21,21 @@ import (
"go.opentelemetry.io/otel/api/trace"
)
type testTraceProvider struct{}
type testTracerProvider struct{}
var _ trace.Provider = &testTraceProvider{}
var _ trace.Provider = &testTracerProvider{}
func (*testTraceProvider) Tracer(_ string, _ ...trace.TracerOption) trace.Tracer {
func (*testTracerProvider) Tracer(_ string, _ ...trace.TracerOption) trace.Tracer {
return &trace.NoopTracer{}
}
func TestMultipleGlobalTracerProvider(t *testing.T) {
p1 := testTraceProvider{}
p1 := testTracerProvider{}
p2 := trace.NoopProvider{}
global.SetTraceProvider(&p1)
global.SetTraceProvider(&p2)
global.SetTracerProvider(&p1)
global.SetTracerProvider(&p2)
got := global.TraceProvider()
got := global.TracerProvider()
want := &p2
if got != want {
t.Fatalf("Provider: got %p, want %p\n", got, want)

View File

@ -130,7 +130,7 @@ func TestMixedAPIs(t *testing.T) {
t.Log(msg)
})
otelglobal.SetTraceProvider(otelProvider)
otelglobal.SetTracerProvider(otelProvider)
ot.SetGlobalTracer(otTracer)
tc.setup(t, mockOtelTracer)

View File

@ -48,7 +48,7 @@ func initTracer() {
if err != nil {
log.Panicf("failed to initialize trace provider %v\n", err)
}
global.SetTraceProvider(tp)
global.SetTracerProvider(tp)
}
func main() {

View File

@ -54,7 +54,7 @@ func initProvider() (*otlp.Exporter, *push.Controller) {
)
handleErr(err, "failed to create exporter")
traceProvider, err := sdktrace.NewProvider(
tracerProvider, err := sdktrace.NewProvider(
sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
sdktrace.WithResource(resource.New(
// the service name used to display traces in backends
@ -73,7 +73,7 @@ func initProvider() (*otlp.Exporter, *push.Controller) {
push.WithPeriod(2*time.Second),
)
global.SetTraceProvider(traceProvider)
global.SetTracerProvider(tracerProvider)
global.SetMeterProvider(pusher.Provider())
pusher.Start()

View File

@ -57,7 +57,7 @@ func main() {
ctx := context.Background()
tr := global.TraceProvider().Tracer("component-main")
tr := global.TracerProvider().Tracer("component-main")
ctx, span := tr.Start(ctx, "foo")
<-time.After(6 * time.Millisecond)
bar(ctx)
@ -69,7 +69,7 @@ func main() {
}
func bar(ctx context.Context) {
tr := global.TraceProvider().Tracer("component-bar")
tr := global.TracerProvider().Tracer("component-bar")
_, span := tr.Start(ctx, "bar")
<-time.After(6 * time.Millisecond)
span.End()

View File

@ -41,13 +41,13 @@ func main() {
}()
// Note: The exporter can also be used as a Batcher. E.g.
// traceProvider, err := sdktrace.NewProvider(
// tracerProvider, err := sdktrace.NewProvider(
// sdktrace.WithBatcher(exporter,
// sdktrace.WithBatchTimeout(time.Second*15),
// sdktrace.WithMaxExportBatchSize(100),
// ),
// )
traceProvider, err := sdktrace.NewProvider(sdktrace.WithBatcher(exporter))
tracerProvider, err := sdktrace.NewProvider(sdktrace.WithBatcher(exporter))
if err != nil {
log.Fatal("failed to create trace provider: %v", err)
}

View File

@ -46,7 +46,7 @@ func Example_insecure() {
log.Fatalf("error creating trace provider: %v\n", err)
}
global.SetTraceProvider(tp)
global.SetTracerProvider(tp)
tracer := global.Tracer("test-tracer")
@ -87,7 +87,7 @@ func Example_withTLS() {
log.Fatalf("error creating trace provider: %v\n", err)
}
global.SetTraceProvider(tp)
global.SetTracerProvider(tp)
tracer := global.Tracer("test-tracer")

View File

@ -31,7 +31,7 @@ const (
)
var (
tracer = global.TraceProvider().Tracer(
tracer = global.TracerProvider().Tracer(
instrumentationName,
trace.WithInstrumentationVersion(instrumentationVersion),
)

View File

@ -88,11 +88,11 @@ func NewExportPipeline(exportOpts []Option, pushOpts []push.Option) (apitrace.Pr
// defer pipeline.Stop()
// ... Done
func InstallNewPipeline(exportOpts []Option, pushOpts []push.Option) (*push.Controller, error) {
traceProvider, controller, err := NewExportPipeline(exportOpts, pushOpts)
tracerProvider, controller, err := NewExportPipeline(exportOpts, pushOpts)
if err != nil {
return controller, err
}
global.SetTraceProvider(traceProvider)
global.SetTracerProvider(tracerProvider)
global.SetMeterProvider(controller.Provider())
return controller, err
}

View File

@ -178,7 +178,7 @@ func InstallNewPipeline(endpointOption EndpointOption, opts ...Option) (func(),
return nil, err
}
global.SetTraceProvider(tp)
global.SetTracerProvider(tp)
return flushFn, nil
}

View File

@ -82,9 +82,9 @@ func TestInstallNewPipeline(t *testing.T) {
defer fn()
assert.NoError(t, err)
assert.IsType(t, tc.expectedProvider, global.TraceProvider())
assert.IsType(t, tc.expectedProvider, global.TracerProvider())
global.SetTraceProvider(nil)
global.SetTracerProvider(nil)
})
}
}
@ -145,7 +145,7 @@ func TestNewExportPipeline(t *testing.T) {
defer fn()
assert.NoError(t, err)
assert.NotEqual(t, tp, global.TraceProvider())
assert.NotEqual(t, tp, global.TracerProvider())
assert.IsType(t, tc.expectedProviderType, tp)
if tc.testSpanSampling {
@ -345,7 +345,7 @@ func TestExporter_ExportSpan(t *testing.T) {
assert.NoError(t, err)
global.SetTraceProvider(tp)
global.SetTracerProvider(tp)
_, span := global.Tracer("test-tracer").Start(context.Background(), "test-span")
span.End()

View File

@ -133,7 +133,7 @@ func InstallNewPipeline(collectorURL, serviceName string, opts ...Option) error
return err
}
global.SetTraceProvider(tp)
global.SetTracerProvider(tp)
return nil
}

View File

@ -48,7 +48,7 @@ func TestInstallNewPipeline(t *testing.T) {
serviceName,
)
assert.NoError(t, err)
assert.IsType(t, &sdktrace.Provider{}, global.TraceProvider())
assert.IsType(t, &sdktrace.Provider{}, global.TracerProvider())
}
func TestNewExportPipeline(t *testing.T) {
@ -90,7 +90,7 @@ func TestNewExportPipeline(t *testing.T) {
tc.options...,
)
assert.NoError(t, err)
assert.NotEqual(t, tp, global.TraceProvider())
assert.NotEqual(t, tp, global.TracerProvider())
if tc.testSpanSampling {
_, span := tp.Tracer("zipkin test").Start(context.Background(), tc.name)