1// Copyright The OpenTelemetry Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package trace // import "go.opentelemetry.io/otel/trace"
5
6import (
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.
20func NewNoopTracerProvider() TracerProvider {
21 return noopTracerProvider{}
22}
23
24type noopTracerProvider struct{ embedded.TracerProvider }
25
26var _ TracerProvider = noopTracerProvider{}
27
28// Tracer returns noop implementation of Tracer.
29func (p noopTracerProvider) Tracer(string, ...TracerOption) Tracer {
30 return noopTracer{}
31}
32
33// noopTracer is an implementation of Tracer that performs no operations.
34type noopTracer struct{ embedded.Tracer }
35
36var _ 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.
40func (t noopTracer) Start(ctx context.Context, name 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.
50type noopSpan struct{ embedded.Span }
51
52var noopSpanInstance Span = noopSpan{}
53
54// SpanContext returns an empty span context.
55func (noopSpan) SpanContext() SpanContext { return SpanContext{} }
56
57// IsRecording always returns false.
58func (noopSpan) IsRecording() bool { return false }
59
60// SetStatus does nothing.
61func (noopSpan) SetStatus(codes.Code, string) {}
62
63// SetError does nothing.
64func (noopSpan) SetError(bool) {}
65
66// SetAttributes does nothing.
67func (noopSpan) SetAttributes(...attribute.KeyValue) {}
68
69// End does nothing.
70func (noopSpan) End(...SpanEndOption) {}
71
72// RecordError does nothing.
73func (noopSpan) RecordError(error, ...EventOption) {}
74
75// AddEvent does nothing.
76func (noopSpan) AddEvent(string, ...EventOption) {}
77
78// AddLink does nothing.
79func (noopSpan) AddLink(Link) {}
80
81// SetName does nothing.
82func (noopSpan) SetName(string) {}
83
84// TracerProvider returns a no-op TracerProvider.
85func (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.
93var 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.
98func (noopSpan) tracerProvider(autoEnabled *bool) TracerProvider {
99 if *autoEnabled {
100 return newAutoTracerProvider()
101 }
102 return noopTracerProvider{}
103}