trace.go

  1// Copyright The OpenTelemetry Authors
  2// SPDX-License-Identifier: Apache-2.0
  3
  4package global // import "go.opentelemetry.io/otel/internal/global"
  5
  6/*
  7This file contains the forwarding implementation of the TracerProvider used as
  8the default global instance. Prior to initialization of an SDK, Tracers
  9returned by the global TracerProvider will provide no-op functionality. This
 10means that all Span created prior to initialization are no-op Spans.
 11
 12Once an SDK has been initialized, all provided no-op Tracers are swapped for
 13Tracers provided by the SDK defined TracerProvider. However, any Span started
 14prior to this initialization does not change its behavior. Meaning, the Span
 15remains a no-op Span.
 16
 17The implementation to track and swap Tracers locks all new Tracer creation
 18until the swap is complete. This assumes that this operation is not
 19performance-critical. If that assumption is incorrect, be sure to configure an
 20SDK prior to any Tracer creation.
 21*/
 22
 23import (
 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.
 39type 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.
 49var _ 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.
 58func (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.
 76func (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
107type 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.
118type 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.
129var _ 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.
137func (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.
143func (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.
159var autoInstEnabled = new(bool)
160
161func (t *tracer) newSpan(ctx context.Context, autoSpan *bool, name string, opts []trace.SpanStartOption) (context.Context, trace.Span) {
162	// autoInstEnabled is passed to newSpan via the autoSpan parameter. This is
163	// so the auto-instrumentation can define a uprobe for (*t).newSpan and be
164	// provided with the address of the bool autoInstEnabled points to. It
165	// needs to be a parameter so that pointer can be reliably determined, it
166	// should not be read from the global.
167
168	if *autoSpan {
169		tracer := sdk.TracerProvider().Tracer(t.name, t.opts...)
170		return tracer.Start(ctx, name, opts...)
171	}
172
173	s := nonRecordingSpan{sc: trace.SpanContextFromContext(ctx), tracer: t}
174	ctx = trace.ContextWithSpan(ctx, s)
175	return ctx, s
176}
177
178// nonRecordingSpan is a minimal implementation of a Span that wraps a
179// SpanContext. It performs no operations other than to return the wrapped
180// SpanContext.
181type nonRecordingSpan struct {
182	embedded.Span
183
184	sc     trace.SpanContext
185	tracer *tracer
186}
187
188var _ trace.Span = nonRecordingSpan{}
189
190// SpanContext returns the wrapped SpanContext.
191func (s nonRecordingSpan) SpanContext() trace.SpanContext { return s.sc }
192
193// IsRecording always returns false.
194func (nonRecordingSpan) IsRecording() bool { return false }
195
196// SetStatus does nothing.
197func (nonRecordingSpan) SetStatus(codes.Code, string) {}
198
199// SetError does nothing.
200func (nonRecordingSpan) SetError(bool) {}
201
202// SetAttributes does nothing.
203func (nonRecordingSpan) SetAttributes(...attribute.KeyValue) {}
204
205// End does nothing.
206func (nonRecordingSpan) End(...trace.SpanEndOption) {}
207
208// RecordError does nothing.
209func (nonRecordingSpan) RecordError(error, ...trace.EventOption) {}
210
211// AddEvent does nothing.
212func (nonRecordingSpan) AddEvent(string, ...trace.EventOption) {}
213
214// AddLink does nothing.
215func (nonRecordingSpan) AddLink(trace.Link) {}
216
217// SetName does nothing.
218func (nonRecordingSpan) SetName(string) {}
219
220func (s nonRecordingSpan) TracerProvider() trace.TracerProvider { return s.tracer.provider }