...

Source file src/go.opentelemetry.io/otel/trace/noop/noop.go

Documentation: go.opentelemetry.io/otel/trace/noop

     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 noop provides an implementation of the OpenTelemetry trace API that
    16  // produces no telemetry and minimizes used computation resources.
    17  //
    18  // Using this package to implement the OpenTelemetry trace API will effectively
    19  // disable OpenTelemetry.
    20  //
    21  // This implementation can be embedded in other implementations of the
    22  // OpenTelemetry trace API. Doing so will mean the implementation defaults to
    23  // no operation for methods it does not implement.
    24  package noop // import "go.opentelemetry.io/otel/trace/noop"
    25  
    26  import (
    27  	"context"
    28  
    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  var (
    36  	// Compile-time check this implements the OpenTelemetry API.
    37  
    38  	_ trace.TracerProvider = TracerProvider{}
    39  	_ trace.Tracer         = Tracer{}
    40  	_ trace.Span           = Span{}
    41  )
    42  
    43  // TracerProvider is an OpenTelemetry No-Op TracerProvider.
    44  type TracerProvider struct{ embedded.TracerProvider }
    45  
    46  // NewTracerProvider returns a TracerProvider that does not record any telemetry.
    47  func NewTracerProvider() TracerProvider {
    48  	return TracerProvider{}
    49  }
    50  
    51  // Tracer returns an OpenTelemetry Tracer that does not record any telemetry.
    52  func (TracerProvider) Tracer(string, ...trace.TracerOption) trace.Tracer {
    53  	return Tracer{}
    54  }
    55  
    56  // Tracer is an OpenTelemetry No-Op Tracer.
    57  type Tracer struct{ embedded.Tracer }
    58  
    59  // Start creates a span. The created span will be set in a child context of ctx
    60  // and returned with the span.
    61  //
    62  // If ctx contains a span context, the returned span will also contain that
    63  // span context. If the span context in ctx is for a non-recording span, that
    64  // span instance will be returned directly.
    65  func (t Tracer) Start(ctx context.Context, _ string, _ ...trace.SpanStartOption) (context.Context, trace.Span) {
    66  	span := trace.SpanFromContext(ctx)
    67  
    68  	// If the parent context contains a non-zero span context, that span
    69  	// context needs to be returned as a non-recording span
    70  	// (https://github.com/open-telemetry/opentelemetry-specification/blob/3a1dde966a4ce87cce5adf464359fe369741bbea/specification/trace/api.md#behavior-of-the-api-in-the-absence-of-an-installed-sdk).
    71  	var zeroSC trace.SpanContext
    72  	if sc := span.SpanContext(); !sc.Equal(zeroSC) {
    73  		if !span.IsRecording() {
    74  			// If the span is not recording return it directly.
    75  			return ctx, span
    76  		}
    77  		// Otherwise, return the span context needs in a non-recording span.
    78  		span = Span{sc: sc}
    79  	} else {
    80  		// No parent, return a No-Op span with an empty span context.
    81  		span = Span{}
    82  	}
    83  	return trace.ContextWithSpan(ctx, span), span
    84  }
    85  
    86  // Span is an OpenTelemetry No-Op Span.
    87  type Span struct {
    88  	embedded.Span
    89  
    90  	sc trace.SpanContext
    91  }
    92  
    93  // SpanContext returns an empty span context.
    94  func (s Span) SpanContext() trace.SpanContext { return s.sc }
    95  
    96  // IsRecording always returns false.
    97  func (Span) IsRecording() bool { return false }
    98  
    99  // SetStatus does nothing.
   100  func (Span) SetStatus(codes.Code, string) {}
   101  
   102  // SetAttributes does nothing.
   103  func (Span) SetAttributes(...attribute.KeyValue) {}
   104  
   105  // End does nothing.
   106  func (Span) End(...trace.SpanEndOption) {}
   107  
   108  // RecordError does nothing.
   109  func (Span) RecordError(error, ...trace.EventOption) {}
   110  
   111  // AddEvent does nothing.
   112  func (Span) AddEvent(string, ...trace.EventOption) {}
   113  
   114  // SetName does nothing.
   115  func (Span) SetName(string) {}
   116  
   117  // TracerProvider returns a No-Op TracerProvider.
   118  func (Span) TracerProvider() trace.TracerProvider { return TracerProvider{} }
   119  

View as plain text