github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/experimental/tracer/noop_span.go (about)

     1  // Copyright 2022 Meta Platforms, Inc. and affiliates.
     2  //
     3  // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     4  //
     5  // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
     6  //
     7  // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
     8  //
     9  // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    10  //
    11  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    12  
    13  package tracer
    14  
    15  import (
    16  	"time"
    17  
    18  	"github.com/facebookincubator/go-belt"
    19  	"github.com/facebookincubator/go-belt/pkg/field"
    20  )
    21  
    22  // NoopSpan is a no-op implementation of a Span. Supposed
    23  // to be used for sampled out spans.
    24  type NoopSpan struct {
    25  	ParentValue  Span
    26  	NameValue    string
    27  	StartTSValue time.Time
    28  }
    29  
    30  var _ Span = (*NoopSpan)(nil)
    31  
    32  // NewNoopSpan returns a new instance of a NoopSpan.
    33  func NewNoopSpan(name string, parent Span, now time.Time) *NoopSpan {
    34  	return &NoopSpan{
    35  		ParentValue:  parent,
    36  		NameValue:    name,
    37  		StartTSValue: now,
    38  	}
    39  }
    40  
    41  // ID implements Span.
    42  func (*NoopSpan) ID() any {
    43  	return nil
    44  }
    45  
    46  // TraceIDs implements Span.
    47  func (*NoopSpan) TraceIDs() belt.TraceIDs {
    48  	return nil
    49  }
    50  
    51  // Fields implements Span.
    52  func (*NoopSpan) Fields() field.AbstractFields {
    53  	return nil
    54  }
    55  
    56  // Parent implements Span.
    57  func (s *NoopSpan) Parent() Span {
    58  	return s.ParentValue
    59  }
    60  
    61  // SetName implements Span.
    62  func (s *NoopSpan) SetName(name string) { s.NameValue = name }
    63  
    64  // Name implements Span.
    65  func (s *NoopSpan) Name() string { return s.NameValue }
    66  
    67  // StartTS implements Span.
    68  func (s *NoopSpan) StartTS() time.Time { return s.StartTSValue }
    69  
    70  // Annotate implements Span.
    71  func (*NoopSpan) Annotate(time.Time, string) {}
    72  
    73  // SetField implements Span.
    74  func (*NoopSpan) SetField(field.Key, field.Value) {}
    75  
    76  // SetFields implements Span.
    77  func (*NoopSpan) SetFields(field.AbstractFields) {}
    78  
    79  // Finish implements Span.
    80  func (*NoopSpan) Finish() {}
    81  
    82  // FinishWithDuration implements Span.
    83  func (*NoopSpan) FinishWithDuration(time.Duration) {}
    84  
    85  // Flush implements Span.
    86  func (*NoopSpan) Flush() {}
    87  
    88  // IsNoopSpan returns true if the given Span is a NoopSpan.
    89  func IsNoopSpan(span Span) bool {
    90  	_, isNoop := span.(*NoopSpan)
    91  	return isNoop
    92  }