github.com/xmidt-org/webpa-common@v1.11.9/tracing/span.go (about)

     1  package tracing
     2  
     3  import (
     4  	"sync/atomic"
     5  	"time"
     6  )
     7  
     8  // Span represents the result of some arbitrary section of code.  Clients create Span objects
     9  // via a Spanner.  A Span is immutable once it has been created via a Spanner closure.
    10  type Span interface {
    11  	// Name is the name of the operation
    12  	Name() string
    13  
    14  	// Start is the time at which the operation started
    15  	Start() time.Time
    16  
    17  	// Duration is how long the operation took.  This value is computed once, when the
    18  	// closure from Spanner.Start is called.
    19  	Duration() time.Duration
    20  
    21  	// Error is any error that occurred.  This will be the error passed to the closure
    22  	// returned from Spanner.Start.  This error can be nil.
    23  	Error() error
    24  }
    25  
    26  // span is the internal Span implementation
    27  type span struct {
    28  	name     string
    29  	start    time.Time
    30  	duration time.Duration
    31  	err      error
    32  
    33  	state uint32
    34  }
    35  
    36  func (s *span) Name() string {
    37  	return s.name
    38  }
    39  
    40  func (s *span) Start() time.Time {
    41  	return s.start
    42  }
    43  
    44  func (s *span) Duration() time.Duration {
    45  	return s.duration
    46  }
    47  
    48  func (s *span) Error() error {
    49  	return s.err
    50  }
    51  
    52  func (s *span) finish(duration time.Duration, err error) bool {
    53  	if atomic.CompareAndSwapUint32(&s.state, 0, 1) {
    54  		s.duration = duration
    55  		s.err = err
    56  		return true
    57  	}
    58  
    59  	return false
    60  }