| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package trace // import "go.opentelemetry.io/otel/trace" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
|
| 9 |
"go.opentelemetry.io/otel/attribute" |
| 10 |
"go.opentelemetry.io/otel/codes" |
| 11 |
"go.opentelemetry.io/otel/trace/embedded" |
| 12 |
) |
| 13 |
|
| 14 |
// NewNoopTracerProvider returns an implementation of TracerProvider that |
| 15 |
// performs no operations. The Tracer and Spans created from the returned |
| 16 |
// TracerProvider also perform no operations. |
| 17 |
// |
| 18 |
// Deprecated: Use [go.opentelemetry.io/otel/trace/noop.NewTracerProvider] |
| 19 |
// instead. |
| 20 |
func NewNoopTracerProvider() TracerProvider { |
| 21 |
return noopTracerProvider{} |
| 22 |
} |
| 23 |
|
| 24 |
type noopTracerProvider struct{ embedded.TracerProvider } |
| 25 |
|
| 26 |
var _ TracerProvider = noopTracerProvider{} |
| 27 |
|
| 28 |
// Tracer returns noop implementation of Tracer. |
| 29 |
func (noopTracerProvider) Tracer(string, ...TracerOption) Tracer { |
| 30 |
return noopTracer{} |
| 31 |
} |
| 32 |
|
| 33 |
// noopTracer is an implementation of Tracer that performs no operations. |
| 34 |
type noopTracer struct{ embedded.Tracer } |
| 35 |
|
| 36 |
var _ Tracer = noopTracer{} |
| 37 |
|
| 38 |
// Start carries forward a non-recording Span, if one is present in the context, otherwise it |
| 39 |
// creates a no-op Span. |
| 40 |
func (noopTracer) Start(ctx context.Context, _ string, _ ...SpanStartOption) (context.Context, Span) { |
| 41 |
span := SpanFromContext(ctx) |
| 42 |
if _, ok := span.(nonRecordingSpan); !ok { |
| 43 |
// span is likely already a noopSpan, but let's be sure |
| 44 |
span = noopSpanInstance |
| 45 |
} |
| 46 |
return ContextWithSpan(ctx, span), span |
| 47 |
} |
| 48 |
|
| 49 |
// noopSpan is an implementation of Span that performs no operations. |
| 50 |
type noopSpan struct{ embedded.Span } |
| 51 |
|
| 52 |
var noopSpanInstance Span = noopSpan{} |
| 53 |
|
| 54 |
// SpanContext returns an empty span context. |
| 55 |
func (noopSpan) SpanContext() SpanContext { return SpanContext{} } |
| 56 |
|
| 57 |
// IsRecording always returns false. |
| 58 |
func (noopSpan) IsRecording() bool { return false } |
| 59 |
|
| 60 |
// SetStatus does nothing. |
| 61 |
func (noopSpan) SetStatus(codes.Code, string) {} |
| 62 |
|
| 63 |
// SetError does nothing. |
| 64 |
func (noopSpan) SetError(bool) {} |
| 65 |
|
| 66 |
// SetAttributes does nothing. |
| 67 |
func (noopSpan) SetAttributes(...attribute.KeyValue) {} |
| 68 |
|
| 69 |
// End does nothing. |
| 70 |
func (noopSpan) End(...SpanEndOption) {} |
| 71 |
|
| 72 |
// RecordError does nothing. |
| 73 |
func (noopSpan) RecordError(error, ...EventOption) {} |
| 74 |
|
| 75 |
// AddEvent does nothing. |
| 76 |
func (noopSpan) AddEvent(string, ...EventOption) {} |
| 77 |
|
| 78 |
// AddLink does nothing. |
| 79 |
func (noopSpan) AddLink(Link) {} |
| 80 |
|
| 81 |
// SetName does nothing. |
| 82 |
func (noopSpan) SetName(string) {} |
| 83 |
|
| 84 |
// TracerProvider returns a no-op TracerProvider. |
| 85 |
func (s noopSpan) TracerProvider() TracerProvider { |
| 86 |
return s.tracerProvider(autoInstEnabled) |
| 87 |
} |
| 88 |
|
| 89 |
// autoInstEnabled defines if the auto-instrumentation SDK is enabled. |
| 90 |
// |
| 91 |
// The auto-instrumentation is expected to overwrite this value to true when it |
| 92 |
// attaches to the process. |
| 93 |
var autoInstEnabled = new(bool) |
| 94 |
|
| 95 |
// tracerProvider return a noopTracerProvider if autoEnabled is false, |
| 96 |
// otherwise it will return a TracerProvider from the sdk package used in |
| 97 |
// auto-instrumentation. |
| 98 |
// |
| 99 |
//go:noinline |
| 100 |
func (noopSpan) tracerProvider(autoEnabled *bool) TracerProvider { |
| 101 |
if *autoEnabled { |
| 102 |
return newAutoTracerProvider() |
| 103 |
} |
| 104 |
return noopTracerProvider{} |
| 105 |
} |