github.com/matrixorigin/matrixone@v1.2.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  	"go.uber.org/zap"
    27  )
    28  
    29  var _ TracerProvider = &noopTracerProvider{}
    30  var _ Tracer = &NoopTracer{}
    31  var _ Span = &NoopSpan{}
    32  
    33  type noopTracerProvider struct{}
    34  
    35  func (n noopTracerProvider) Tracer(string, ...TracerOption) Tracer {
    36  	return NoopTracer{}
    37  }
    38  
    39  // NoopTracer is an implementation of Tracer that preforms no operations.
    40  type NoopTracer struct{}
    41  
    42  // Start carries forward a non-recording Span, if one is present in the context, otherwise it
    43  // creates a no-op Span.
    44  func (t NoopTracer) Start(ctx context.Context, name string, opts ...SpanStartOption) (context.Context, Span) {
    45  	span := SpanFromContext(ctx)
    46  	if _, ok := span.(NoopSpan); !ok {
    47  		// span is likely already a NoopSpan, but let's be sure
    48  		span = NoopSpan{}
    49  		return ContextWithSpan(ctx, span), span
    50  	} else if len(opts) > 0 {
    51  		var sc = SpanConfig{}
    52  		for _, opt := range opts {
    53  			opt.ApplySpanStart(&sc)
    54  		}
    55  		if sc.NewRoot {
    56  			span = NoopSpan{}
    57  			return ContextWithSpan(ctx, span), span
    58  		}
    59  	}
    60  	return ctx, span
    61  }
    62  
    63  func (t NoopTracer) Debug(ctx context.Context, name string, opts ...SpanStartOption) (context.Context, Span) {
    64  	return t.Start(ctx, name, opts...)
    65  }
    66  
    67  func (t NoopTracer) IsEnable(opts ...SpanStartOption) bool { return false }
    68  
    69  // NoopSpan is an implementation of Span that preforms no operations.
    70  type NoopSpan struct{}
    71  
    72  var _ Span = NoopSpan{}
    73  
    74  // SpanContext returns an empty span context.
    75  func (NoopSpan) SpanContext() SpanContext { return SpanContext{} }
    76  
    77  func (NoopSpan) ParentSpanContext() SpanContext { return SpanContext{} }
    78  
    79  // End does nothing.
    80  func (NoopSpan) End(...SpanEndOption) {}
    81  
    82  func (NoopSpan) AddExtraFields(...zap.Field) {}
    83  
    84  // SetName does nothing.
    85  func (NoopSpan) SetName(string) {}
    86  
    87  // TracerProvider returns a no-op TracerProvider.
    88  func (NoopSpan) TracerProvider() TracerProvider { return noopTracerProvider{} }
    89  
    90  // NonRecordingSpan keep SpanContext{TraceID, SpanID}
    91  type NonRecordingSpan struct {
    92  	NoopSpan
    93  	sc SpanContext
    94  }
    95  
    96  func (s *NonRecordingSpan) SpanContext() SpanContext       { return s.sc }
    97  func (s *NonRecordingSpan) ParentSpanContext() SpanContext { return SpanContext{} }