| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 1 | // Copyright The OpenTelemetry Authors |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | package global // import "go.opentelemetry.io/otel/internal/global" |
| 5 | |
| 6 | /* |
| 7 | This file contains the forwarding implementation of the TracerProvider used as |
| 8 | the default global instance. Prior to initialization of an SDK, Tracers |
| 9 | returned by the global TracerProvider will provide no-op functionality. This |
| 10 | means that all Span created prior to initialization are no-op Spans. |
| 11 | |
| 12 | Once an SDK has been initialized, all provided no-op Tracers are swapped for |
| 13 | Tracers provided by the SDK defined TracerProvider. However, any Span started |
| 14 | prior to this initialization does not change its behavior. Meaning, the Span |
| 15 | remains a no-op Span. |
| 16 | |
| 17 | The implementation to track and swap Tracers locks all new Tracer creation |
| 18 | until the swap is complete. This assumes that this operation is not |
| 19 | performance-critical. If that assumption is incorrect, be sure to configure an |
| 20 | SDK prior to any Tracer creation. |
| 21 | */ |
| 22 | |
| 23 | import ( |
| 24 | "context" |
| 25 | "sync" |
| 26 | "sync/atomic" |
| 27 | |
| 28 | "go.opentelemetry.io/auto/sdk" |
| 29 | "go.opentelemetry.io/otel/attribute" |
| 30 | "go.opentelemetry.io/otel/codes" |
| 31 | "go.opentelemetry.io/otel/trace" |
| 32 | "go.opentelemetry.io/otel/trace/embedded" |
| 33 | ) |
| 34 | |
| 35 | // tracerProvider is a placeholder for a configured SDK TracerProvider. |
| 36 | // |
| 37 | // All TracerProvider functionality is forwarded to a delegate once |
| 38 | // configured. |
| 39 | type tracerProvider struct { |
| 40 | embedded.TracerProvider |
| 41 | |
| 42 | mtx sync.Mutex |
| 43 | tracers map[il]*tracer |
| 44 | delegate trace.TracerProvider |
| 45 | } |
| 46 | |
| 47 | // Compile-time guarantee that tracerProvider implements the TracerProvider |
| 48 | // interface. |
| 49 | var _ trace.TracerProvider = &tracerProvider{} |
| 50 | |
| 51 | // setDelegate configures p to delegate all TracerProvider functionality to |
| 52 | // provider. |
| 53 | // |
| 54 | // All Tracers provided prior to this function call are switched out to be |
| 55 | // Tracers provided by provider. |
| 56 | // |
| 57 | // It is guaranteed by the caller that this happens only once. |
| 58 | func (p *tracerProvider) setDelegate(provider trace.TracerProvider) { |
| 59 | p.mtx.Lock() |
| 60 | defer p.mtx.Unlock() |
| 61 | |
| 62 | p.delegate = provider |
| 63 | |
| 64 | if len(p.tracers) == 0 { |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | for _, t := range p.tracers { |
| 69 | t.setDelegate(provider) |
| 70 | } |
| 71 | |
| 72 | p.tracers = nil |
| 73 | } |
| 74 | |
| 75 | // Tracer implements TracerProvider. |
| 76 | func (p *tracerProvider) Tracer(name string, opts ...trace.TracerOption) trace.Tracer { |
| 77 | p.mtx.Lock() |
| 78 | defer p.mtx.Unlock() |
| 79 | |
| 80 | if p.delegate != nil { |
| 81 | return p.delegate.Tracer(name, opts...) |
| 82 | } |
| 83 | |
| 84 | // At this moment it is guaranteed that no sdk is installed, save the tracer in the tracers map. |
| 85 | |
| 86 | c := trace.NewTracerConfig(opts...) |
| 87 | key := il{ |
| 88 | name: name, |
| 89 | version: c.InstrumentationVersion(), |
| 90 | schema: c.SchemaURL(), |
| 91 | attrs: c.InstrumentationAttributes(), |
| 92 | } |
| 93 | |
| 94 | if p.tracers == nil { |
| 95 | p.tracers = make(map[il]*tracer) |
| 96 | } |
| 97 | |
| 98 | if val, ok := p.tracers[key]; ok { |
| 99 | return val |
| 100 | } |
| 101 | |
| 102 | t := &tracer{name: name, opts: opts, provider: p} |
| 103 | p.tracers[key] = t |
| 104 | return t |
| 105 | } |
| 106 | |
| 107 | type il struct { |
| 108 | name string |
| 109 | version string |
| 110 | schema string |
| 111 | attrs attribute.Set |
| 112 | } |
| 113 | |
| 114 | // tracer is a placeholder for a trace.Tracer. |
| 115 | // |
| 116 | // All Tracer functionality is forwarded to a delegate once configured. |
| 117 | // Otherwise, all functionality is forwarded to a NoopTracer. |
| 118 | type tracer struct { |
| 119 | embedded.Tracer |
| 120 | |
| 121 | name string |
| 122 | opts []trace.TracerOption |
| 123 | provider *tracerProvider |
| 124 | |
| 125 | delegate atomic.Value |
| 126 | } |
| 127 | |
| 128 | // Compile-time guarantee that tracer implements the trace.Tracer interface. |
| 129 | var _ trace.Tracer = &tracer{} |
| 130 | |
| 131 | // setDelegate configures t to delegate all Tracer functionality to Tracers |
| 132 | // created by provider. |
| 133 | // |
| 134 | // All subsequent calls to the Tracer methods will be passed to the delegate. |
| 135 | // |
| 136 | // It is guaranteed by the caller that this happens only once. |
| 137 | func (t *tracer) setDelegate(provider trace.TracerProvider) { |
| 138 | t.delegate.Store(provider.Tracer(t.name, t.opts...)) |
| 139 | } |
| 140 | |
| 141 | // Start implements trace.Tracer by forwarding the call to t.delegate if |
| 142 | // set, otherwise it forwards the call to a NoopTracer. |
| 143 | func (t *tracer) Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) { |
| 144 | delegate := t.delegate.Load() |
| 145 | if delegate != nil { |
| 146 | return delegate.(trace.Tracer).Start(ctx, name, opts...) |
| 147 | } |
| 148 | |
| 149 | return t.newSpan(ctx, autoInstEnabled, name, opts) |
| 150 | } |
| 151 | |
| 152 | // autoInstEnabled determines if the auto-instrumentation SDK span is returned |
| 153 | // from the tracer when not backed by a delegate and auto-instrumentation has |
| 154 | // attached to this process. |
| 155 | // |
| 156 | // The auto-instrumentation is expected to overwrite this value to true when it |
| 157 | // attaches. By default, this will point to false and mean a tracer will return |
| 158 | // a nonRecordingSpan by default. |
| 159 | var autoInstEnabled = new(bool) |
| 160 | |
| 161 | // newSpan is called by tracer.Start so auto-instrumentation can attach an eBPF |
| 162 | // uprobe to this code. |
| 163 | // |
| 164 | // "noinline" pragma prevents the method from ever being inlined. |
| 165 | // |
| 166 | //go:noinline |
| 167 | func (t *tracer) newSpan( |
| 168 | ctx context.Context, |
| 169 | autoSpan *bool, |
| 170 | name string, |
| 171 | opts []trace.SpanStartOption, |
| 172 | ) (context.Context, trace.Span) { |
| 173 | // autoInstEnabled is passed to newSpan via the autoSpan parameter. This is |
| 174 | // so the auto-instrumentation can define a uprobe for (*t).newSpan and be |
| 175 | // provided with the address of the bool autoInstEnabled points to. It |
| 176 | // needs to be a parameter so that pointer can be reliably determined, it |
| 177 | // should not be read from the global. |
| 178 | |
| 179 | if *autoSpan { |
| 180 | tracer := sdk.TracerProvider().Tracer(t.name, t.opts...) |
| 181 | return tracer.Start(ctx, name, opts...) |
| 182 | } |
| 183 | |
| 184 | s := nonRecordingSpan{sc: trace.SpanContextFromContext(ctx), tracer: t} |
| 185 | ctx = trace.ContextWithSpan(ctx, s) |
| 186 | return ctx, s |
| 187 | } |
| 188 | |
| 189 | // nonRecordingSpan is a minimal implementation of a Span that wraps a |
| 190 | // SpanContext. It performs no operations other than to return the wrapped |
| 191 | // SpanContext. |
| 192 | type nonRecordingSpan struct { |
| 193 | embedded.Span |
| 194 | |
| 195 | sc trace.SpanContext |
| 196 | tracer *tracer |
| 197 | } |
| 198 | |
| 199 | var _ trace.Span = nonRecordingSpan{} |
| 200 | |
| 201 | // SpanContext returns the wrapped SpanContext. |
| 202 | func (s nonRecordingSpan) SpanContext() trace.SpanContext { return s.sc } |
| 203 | |
| 204 | // IsRecording always returns false. |
| 205 | func (nonRecordingSpan) IsRecording() bool { return false } |
| 206 | |
| 207 | // SetStatus does nothing. |
| 208 | func (nonRecordingSpan) SetStatus(codes.Code, string) {} |
| 209 | |
| 210 | // SetError does nothing. |
| 211 | func (nonRecordingSpan) SetError(bool) {} |
| 212 | |
| 213 | // SetAttributes does nothing. |
| 214 | func (nonRecordingSpan) SetAttributes(...attribute.KeyValue) {} |
| 215 | |
| 216 | // End does nothing. |
| 217 | func (nonRecordingSpan) End(...trace.SpanEndOption) {} |
| 218 | |
| 219 | // RecordError does nothing. |
| 220 | func (nonRecordingSpan) RecordError(error, ...trace.EventOption) {} |
| 221 | |
| 222 | // AddEvent does nothing. |
| 223 | func (nonRecordingSpan) AddEvent(string, ...trace.EventOption) {} |
| 224 | |
| 225 | // AddLink does nothing. |
| 226 | func (nonRecordingSpan) AddLink(trace.Link) {} |
| 227 | |
| 228 | // SetName does nothing. |
| 229 | func (nonRecordingSpan) SetName(string) {} |
| 230 | |
| 231 | func (s nonRecordingSpan) TracerProvider() trace.TracerProvider { return s.tracer.provider } |