github.com/matrixorigin/matrixone@v0.7.0/pkg/util/trace/noop_trace.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  // Portions of this file are additionally subject to the following
    16  // copyright.
    17  //
    18  // Copyright (C) 2022 Matrix Origin.
    19  //
    20  // Modified the behavior and the interface of the step.
    21  
    22  package trace
    23  
    24  import (
    25  	"context"
    26  )
    27  
    28  var _ TracerProvider = &noopTracerProvider{}
    29  var _ Tracer = &NoopTracer{}
    30  var _ Span = &NoopSpan{}
    31  
    32  type noopTracerProvider struct{}
    33  
    34  func (n noopTracerProvider) Tracer(string, ...TracerOption) Tracer {
    35  	return NoopTracer{}
    36  }
    37  
    38  // NoopTracer is an implementation of Tracer that preforms no operations.
    39  type NoopTracer struct{}
    40  
    41  // Start carries forward a non-recording Span, if one is present in the context, otherwise it
    42  // creates a no-op Span.
    43  func (t NoopTracer) Start(ctx context.Context, name string, _ ...SpanOption) (context.Context, Span) {
    44  	span := SpanFromContext(ctx)
    45  	if _, ok := span.(NoopSpan); !ok {
    46  		// span is likely already a NoopSpan, but let's be sure
    47  		span = NoopSpan{}
    48  	}
    49  	return ContextWithSpan(ctx, span), span
    50  }
    51  
    52  func (t NoopTracer) Debug(ctx context.Context, name string, opts ...SpanOption) (context.Context, Span) {
    53  	return t.Start(ctx, name, opts...)
    54  }
    55  
    56  func (t NoopTracer) IsEnable() bool { return false }
    57  
    58  // NoopSpan is an implementation of Span that preforms no operations.
    59  type NoopSpan struct{}
    60  
    61  var _ Span = NoopSpan{}
    62  
    63  // SpanContext returns an empty span context.
    64  func (NoopSpan) SpanContext() SpanContext { return SpanContext{} }
    65  
    66  func (NoopSpan) ParentSpanContext() SpanContext { return SpanContext{} }
    67  
    68  // End does nothing.
    69  func (NoopSpan) End(...SpanEndOption) {}
    70  
    71  // SetName does nothing.
    72  func (NoopSpan) SetName(string) {}
    73  
    74  // TracerProvider returns a no-op TracerProvider.
    75  func (NoopSpan) TracerProvider() TracerProvider { return noopTracerProvider{} }
    76  
    77  // NonRecordingSpan keep SpanContext{TraceID, SpanID}
    78  type NonRecordingSpan struct {
    79  	NoopSpan
    80  	sc SpanContext
    81  }
    82  
    83  func (s *NonRecordingSpan) SpanContext() SpanContext       { return s.sc }
    84  func (s *NonRecordingSpan) ParentSpanContext() SpanContext { return SpanContext{} }