2020-03-23 22:41:10 -07: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.
|
|
|
|
|
2019-12-23 23:03:04 -08:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2020-10-02 12:27:16 -07:00
|
|
|
"go.opentelemetry.io/otel"
|
2019-12-23 23:03:04 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2020-08-31 10:02:04 -07:00
|
|
|
tracerProviderHolder struct {
|
2020-10-08 19:58:56 -07:00
|
|
|
tp otel.TracerProvider
|
2019-12-23 23:03:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
meterProviderHolder struct {
|
2020-10-17 09:48:21 -07:00
|
|
|
mp otel.MeterProvider
|
2019-12-23 23:03:04 -08:00
|
|
|
}
|
2020-03-05 10:12:10 -08:00
|
|
|
|
|
|
|
propagatorsHolder struct {
|
2020-10-02 12:27:16 -07:00
|
|
|
tm otel.TextMapPropagator
|
2020-03-05 10:12:10 -08:00
|
|
|
}
|
2019-12-23 23:03:04 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-03-05 10:12:10 -08:00
|
|
|
globalTracer = defaultTracerValue()
|
|
|
|
globalMeter = defaultMeterValue()
|
|
|
|
globalPropagators = defaultPropagatorsValue()
|
2019-12-23 23:03:04 -08:00
|
|
|
|
2020-10-15 10:35:47 -07:00
|
|
|
delegateMeterOnce sync.Once
|
|
|
|
delegateTraceOnce sync.Once
|
|
|
|
delegateTextMapPropagatorOnce sync.Once
|
2019-12-23 23:03:04 -08:00
|
|
|
)
|
|
|
|
|
2020-08-31 10:02:04 -07:00
|
|
|
// TracerProvider is the internal implementation for global.TracerProvider.
|
2020-10-08 19:58:56 -07:00
|
|
|
func TracerProvider() otel.TracerProvider {
|
2020-08-31 10:02:04 -07:00
|
|
|
return globalTracer.Load().(tracerProviderHolder).tp
|
2019-12-23 23:03:04 -08:00
|
|
|
}
|
|
|
|
|
2020-08-31 10:02:04 -07:00
|
|
|
// SetTracerProvider is the internal implementation for global.SetTracerProvider.
|
2020-10-08 19:58:56 -07:00
|
|
|
func SetTracerProvider(tp otel.TracerProvider) {
|
2020-01-02 13:20:38 -08:00
|
|
|
delegateTraceOnce.Do(func() {
|
2020-08-31 10:02:04 -07:00
|
|
|
current := TracerProvider()
|
2020-01-02 13:20:38 -08:00
|
|
|
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.
|
2020-09-23 15:16:13 -07:00
|
|
|
panic("invalid TracerProvider, the global instance cannot be reinstalled")
|
2020-08-31 10:02:04 -07:00
|
|
|
} else if def, ok := current.(*tracerProvider); ok {
|
2020-01-02 13:20:38 -08:00
|
|
|
def.setDelegate(tp)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
2020-08-31 10:02:04 -07:00
|
|
|
globalTracer.Store(tracerProviderHolder{tp: tp})
|
2019-12-23 23:03:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MeterProvider is the internal implementation for global.MeterProvider.
|
2020-10-17 09:48:21 -07:00
|
|
|
func MeterProvider() otel.MeterProvider {
|
2019-12-23 23:03:04 -08:00
|
|
|
return globalMeter.Load().(meterProviderHolder).mp
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMeterProvider is the internal implementation for global.SetMeterProvider.
|
2020-10-17 09:48:21 -07:00
|
|
|
func SetMeterProvider(mp otel.MeterProvider) {
|
2019-12-23 23:03:04 -08:00
|
|
|
delegateMeterOnce.Do(func() {
|
|
|
|
current := MeterProvider()
|
|
|
|
|
|
|
|
if current == mp {
|
|
|
|
// Setting the provider to the prior default is nonsense, panic.
|
|
|
|
// Panic is acceptable because we are likely still early in the
|
|
|
|
// process lifetime.
|
2020-09-23 15:16:13 -07:00
|
|
|
panic("invalid MeterProvider, the global instance cannot be reinstalled")
|
2019-12-23 23:03:04 -08:00
|
|
|
} else if def, ok := current.(*meterProvider); ok {
|
|
|
|
def.setDelegate(mp)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
globalMeter.Store(meterProviderHolder{mp: mp})
|
|
|
|
}
|
|
|
|
|
2020-10-02 12:27:16 -07:00
|
|
|
// TextMapPropagator is the internal implementation for global.TextMapPropagator.
|
|
|
|
func TextMapPropagator() otel.TextMapPropagator {
|
|
|
|
return globalPropagators.Load().(propagatorsHolder).tm
|
2020-03-05 10:12:10 -08:00
|
|
|
}
|
|
|
|
|
2020-10-02 12:27:16 -07:00
|
|
|
// SetTextMapPropagator is the internal implementation for global.SetTextMapPropagator.
|
|
|
|
func SetTextMapPropagator(p otel.TextMapPropagator) {
|
2020-10-15 10:35:47 -07:00
|
|
|
// For the textMapPropagator already returned by TextMapPropagator
|
|
|
|
// delegate to p.
|
|
|
|
delegateTextMapPropagatorOnce.Do(func() {
|
|
|
|
if current := TextMapPropagator(); current == p {
|
|
|
|
// 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 TextMapPropagator, the global instance cannot be reinstalled")
|
|
|
|
} else if def, ok := current.(*textMapPropagator); ok {
|
|
|
|
def.SetDelegate(p)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// Return p when subsequent calls to TextMapPropagator are made.
|
2020-10-02 12:27:16 -07:00
|
|
|
globalPropagators.Store(propagatorsHolder{tm: p})
|
2020-03-05 10:12:10 -08:00
|
|
|
}
|
|
|
|
|
2019-12-23 23:03:04 -08:00
|
|
|
func defaultTracerValue() *atomic.Value {
|
|
|
|
v := &atomic.Value{}
|
2020-08-31 10:02:04 -07:00
|
|
|
v.Store(tracerProviderHolder{tp: &tracerProvider{}})
|
2019-12-23 23:03:04 -08:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultMeterValue() *atomic.Value {
|
|
|
|
v := &atomic.Value{}
|
2020-03-24 10:54:08 -07:00
|
|
|
v.Store(meterProviderHolder{mp: newMeterProvider()})
|
2019-12-23 23:03:04 -08:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2020-03-05 10:12:10 -08:00
|
|
|
func defaultPropagatorsValue() *atomic.Value {
|
|
|
|
v := &atomic.Value{}
|
2020-10-15 10:35:47 -07:00
|
|
|
v.Store(propagatorsHolder{tm: newTextMapPropagator()})
|
2020-03-05 10:12:10 -08:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2019-12-23 23:03:04 -08:00
|
|
|
// ResetForTest restores the initial global state, for testing purposes.
|
|
|
|
func ResetForTest() {
|
|
|
|
globalTracer = defaultTracerValue()
|
|
|
|
globalMeter = defaultMeterValue()
|
2020-03-05 10:12:10 -08:00
|
|
|
globalPropagators = defaultPropagatorsValue()
|
2019-12-23 23:03:04 -08:00
|
|
|
delegateMeterOnce = sync.Once{}
|
2020-01-02 13:20:38 -08:00
|
|
|
delegateTraceOnce = sync.Once{}
|
2020-10-16 17:50:57 -07:00
|
|
|
delegateTextMapPropagatorOnce = sync.Once{}
|
2019-12-23 23:03:04 -08:00
|
|
|
}
|