github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.opentelemetry.io/otel/trace/noop.go (about)

     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  	"go.opentelemetry.io/otel/trace/embedded"
    23  )
    24  
    25  // NewNoopTracerProvider returns an implementation of TracerProvider that
    26  // performs no operations. The Tracer and Spans created from the returned
    27  // TracerProvider also perform no operations.
    28  //
    29  // Deprecated: Use [go.opentelemetry.io/otel/trace/noop.NewTracerProvider]
    30  // instead.
    31  func NewNoopTracerProvider() TracerProvider {
    32  	return noopTracerProvider{}
    33  }
    34  
    35  type noopTracerProvider struct{ embedded.TracerProvider }
    36  
    37  var _ TracerProvider = noopTracerProvider{}
    38  
    39  // Tracer returns noop implementation of Tracer.
    40  func (p noopTracerProvider) Tracer(string, ...TracerOption) Tracer {
    41  	return noopTracer{}
    42  }
    43  
    44  // noopTracer is an implementation of Tracer that performs no operations.
    45  type noopTracer struct{ embedded.Tracer }
    46  
    47  var _ Tracer = noopTracer{}
    48  
    49  // Start carries forward a non-recording Span, if one is present in the context, otherwise it
    50  // creates a no-op Span.
    51  func (t noopTracer) Start(ctx context.Context, name string, _ ...SpanStartOption) (context.Context, Span) {
    52  	span := SpanFromContext(ctx)
    53  	if _, ok := span.(nonRecordingSpan); !ok {
    54  		// span is likely already a noopSpan, but let's be sure
    55  		span = noopSpan{}
    56  	}
    57  	return ContextWithSpan(ctx, span), span
    58  }
    59  
    60  // noopSpan is an implementation of Span that performs no operations.
    61  type noopSpan struct{ embedded.Span }
    62  
    63  var _ Span = noopSpan{}
    64  
    65  // SpanContext returns an empty span context.
    66  func (noopSpan) SpanContext() SpanContext { return SpanContext{} }
    67  
    68  // IsRecording always returns false.
    69  func (noopSpan) IsRecording() bool { return false }
    70  
    71  // SetStatus does nothing.
    72  func (noopSpan) SetStatus(codes.Code, string) {}
    73  
    74  // SetError does nothing.
    75  func (noopSpan) SetError(bool) {}
    76  
    77  // SetAttributes does nothing.
    78  func (noopSpan) SetAttributes(...attribute.KeyValue) {}
    79  
    80  // End does nothing.
    81  func (noopSpan) End(...SpanEndOption) {}
    82  
    83  // RecordError does nothing.
    84  func (noopSpan) RecordError(error, ...EventOption) {}
    85  
    86  // AddEvent does nothing.
    87  func (noopSpan) AddEvent(string, ...EventOption) {}
    88  
    89  // SetName does nothing.
    90  func (noopSpan) SetName(string) {}
    91  
    92  // TracerProvider returns a no-op TracerProvider.
    93  func (noopSpan) TracerProvider() TracerProvider { return noopTracerProvider{} }