Elia Battiston | c8d0d46 | 2022-02-22 16:30:51 +0100 | [diff] [blame] | 1 | package opentracing |
| 2 | |
| 3 | type registeredTracer struct { |
| 4 | tracer Tracer |
| 5 | isRegistered bool |
| 6 | } |
| 7 | |
| 8 | var ( |
| 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. |
| 18 | func 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. |
| 25 | func GlobalTracer() Tracer { |
| 26 | return globalTracer.tracer |
| 27 | } |
| 28 | |
| 29 | // StartSpan defers to `Tracer.StartSpan`. See `GlobalTracer()`. |
| 30 | func StartSpan(operationName string, opts ...StartSpanOption) Span { |
| 31 | return globalTracer.tracer.StartSpan(operationName, opts...) |
| 32 | } |
| 33 | |
| 34 | // InitGlobalTracer is deprecated. Please use SetGlobalTracer. |
| 35 | func InitGlobalTracer(tracer Tracer) { |
| 36 | SetGlobalTracer(tracer) |
| 37 | } |
| 38 | |
| 39 | // IsGlobalTracerRegistered returns a `bool` to indicate if a tracer has been globally registered |
| 40 | func IsGlobalTracerRegistered() bool { |
| 41 | return globalTracer.isRegistered |
| 42 | } |