noop.go (2766B)
1 // Copyright The OpenTelemetry Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package trace // import "go.opentelemetry.io/otel/trace" 16 17 import ( 18 "context" 19 20 "go.opentelemetry.io/otel/attribute" 21 "go.opentelemetry.io/otel/codes" 22 ) 23 24 // NewNoopTracerProvider returns an implementation of TracerProvider that 25 // performs no operations. The Tracer and Spans created from the returned 26 // TracerProvider also perform no operations. 27 func NewNoopTracerProvider() TracerProvider { 28 return noopTracerProvider{} 29 } 30 31 type noopTracerProvider struct{} 32 33 var _ TracerProvider = noopTracerProvider{} 34 35 // Tracer returns noop implementation of Tracer. 36 func (p noopTracerProvider) Tracer(string, ...TracerOption) Tracer { 37 return noopTracer{} 38 } 39 40 // noopTracer is an implementation of Tracer that performs no operations. 41 type noopTracer struct{} 42 43 var _ Tracer = noopTracer{} 44 45 // Start carries forward a non-recording Span, if one is present in the context, otherwise it 46 // creates a no-op Span. 47 func (t noopTracer) Start(ctx context.Context, name string, _ ...SpanStartOption) (context.Context, Span) { 48 span := SpanFromContext(ctx) 49 if _, ok := span.(nonRecordingSpan); !ok { 50 // span is likely already a noopSpan, but let's be sure 51 span = noopSpan{} 52 } 53 return ContextWithSpan(ctx, span), span 54 } 55 56 // noopSpan is an implementation of Span that performs no operations. 57 type noopSpan struct{} 58 59 var _ Span = noopSpan{} 60 61 // SpanContext returns an empty span context. 62 func (noopSpan) SpanContext() SpanContext { return SpanContext{} } 63 64 // IsRecording always returns false. 65 func (noopSpan) IsRecording() bool { return false } 66 67 // SetStatus does nothing. 68 func (noopSpan) SetStatus(codes.Code, string) {} 69 70 // SetError does nothing. 71 func (noopSpan) SetError(bool) {} 72 73 // SetAttributes does nothing. 74 func (noopSpan) SetAttributes(...attribute.KeyValue) {} 75 76 // End does nothing. 77 func (noopSpan) End(...SpanEndOption) {} 78 79 // RecordError does nothing. 80 func (noopSpan) RecordError(error, ...EventOption) {} 81 82 // AddEvent does nothing. 83 func (noopSpan) AddEvent(string, ...EventOption) {} 84 85 // SetName does nothing. 86 func (noopSpan) SetName(string) {} 87 88 // TracerProvider returns a no-op TracerProvider. 89 func (noopSpan) TracerProvider() TracerProvider { return noopTracerProvider{} }