2018-06-08 05:26:10 -04:00
|
|
|
package opentracing
|
|
|
|
|
2020-11-17 05:57:17 -05:00
|
|
|
type registeredTracer struct {
|
|
|
|
tracer Tracer
|
|
|
|
isRegistered bool
|
|
|
|
}
|
|
|
|
|
2018-06-08 05:26:10 -04:00
|
|
|
var (
|
2020-11-17 05:57:17 -05:00
|
|
|
globalTracer = registeredTracer{NoopTracer{}, false}
|
2018-06-08 05:26:10 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// SetGlobalTracer sets the [singleton] opentracing.Tracer returned by
|
|
|
|
// GlobalTracer(). Those who use GlobalTracer (rather than directly manage an
|
|
|
|
// opentracing.Tracer instance) should call SetGlobalTracer as early as
|
|
|
|
// possible in main(), prior to calling the `StartSpan` global func below.
|
|
|
|
// Prior to calling `SetGlobalTracer`, any Spans started via the `StartSpan`
|
|
|
|
// (etc) globals are noops.
|
|
|
|
func SetGlobalTracer(tracer Tracer) {
|
2020-11-17 05:57:17 -05:00
|
|
|
globalTracer = registeredTracer{tracer, true}
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GlobalTracer returns the global singleton `Tracer` implementation.
|
|
|
|
// Before `SetGlobalTracer()` is called, the `GlobalTracer()` is a noop
|
|
|
|
// implementation that drops all data handed to it.
|
|
|
|
func GlobalTracer() Tracer {
|
2020-11-17 05:57:17 -05:00
|
|
|
return globalTracer.tracer
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartSpan defers to `Tracer.StartSpan`. See `GlobalTracer()`.
|
|
|
|
func StartSpan(operationName string, opts ...StartSpanOption) Span {
|
2020-11-17 05:57:17 -05:00
|
|
|
return globalTracer.tracer.StartSpan(operationName, opts...)
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// InitGlobalTracer is deprecated. Please use SetGlobalTracer.
|
|
|
|
func InitGlobalTracer(tracer Tracer) {
|
|
|
|
SetGlobalTracer(tracer)
|
|
|
|
}
|
2020-11-17 05:57:17 -05:00
|
|
|
|
|
|
|
// IsGlobalTracerRegistered returns a `bool` to indicate if a tracer has been globally registered
|
|
|
|
func IsGlobalTracerRegistered() bool {
|
|
|
|
return globalTracer.isRegistered
|
|
|
|
}
|