blob: 4f7066a925cd9e2e80e2430c0a2e1fb8361f6f9b [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301package opentracing
2
3type registeredTracer struct {
4 tracer Tracer
5 isRegistered bool
6}
7
8var (
9 globalTracer = registeredTracer{NoopTracer{}, false}
10)
11
12// SetGlobalTracer sets the [singleton] opentracing.Tracer returned by
13// GlobalTracer(). Those who use GlobalTracer (rather than directly manage an
14// opentracing.Tracer instance) should call SetGlobalTracer as early as
15// possible in main(), prior to calling the `StartSpan` global func below.
16// Prior to calling `SetGlobalTracer`, any Spans started via the `StartSpan`
17// (etc) globals are noops.
18func SetGlobalTracer(tracer Tracer) {
19 globalTracer = registeredTracer{tracer, true}
20}
21
22// GlobalTracer returns the global singleton `Tracer` implementation.
23// Before `SetGlobalTracer()` is called, the `GlobalTracer()` is a noop
24// implementation that drops all data handed to it.
25func GlobalTracer() Tracer {
26 return globalTracer.tracer
27}
28
29// StartSpan defers to `Tracer.StartSpan`. See `GlobalTracer()`.
30func StartSpan(operationName string, opts ...StartSpanOption) Span {
31 return globalTracer.tracer.StartSpan(operationName, opts...)
32}
33
34// InitGlobalTracer is deprecated. Please use SetGlobalTracer.
35func InitGlobalTracer(tracer Tracer) {
36 SetGlobalTracer(tracer)
37}
38
39// IsGlobalTracerRegistered returns a `bool` to indicate if a tracer has been globally registered
40func IsGlobalTracerRegistered() bool {
41 return globalTracer.isRegistered
42}