go.undefinedlabs.com/scopeagent@v0.4.2/tracer/recorder.go (about)

     1  package tracer
     2  
     3  import "sync"
     4  
     5  // A SpanRecorder handles all of the `RawSpan` data generated via an
     6  // associated `Tracer` (see `NewStandardTracer`) instance. It also names
     7  // the containing process and provides access to a straightforward tag map.
     8  type SpanRecorder interface {
     9  	// Implementations must determine whether and where to store `span`.
    10  	RecordSpan(span RawSpan)
    11  }
    12  
    13  // InMemorySpanRecorder is a simple thread-safe implementation of
    14  // SpanRecorder that stores all reported spans in memory, accessible
    15  // via reporter.GetSpans(). It is primarily intended for testing purposes.
    16  type InMemorySpanRecorder struct {
    17  	sync.RWMutex
    18  	spans []RawSpan
    19  }
    20  
    21  // NewInMemoryRecorder creates new InMemorySpanRecorder
    22  func NewInMemoryRecorder() *InMemorySpanRecorder {
    23  	return new(InMemorySpanRecorder)
    24  }
    25  
    26  // RecordSpan implements the respective method of SpanRecorder.
    27  func (r *InMemorySpanRecorder) RecordSpan(span RawSpan) {
    28  	r.Lock()
    29  	defer r.Unlock()
    30  	r.spans = append(r.spans, span)
    31  }
    32  
    33  // GetSpans returns a copy of the array of spans accumulated so far.
    34  func (r *InMemorySpanRecorder) GetSpans() []RawSpan {
    35  	r.RLock()
    36  	defer r.RUnlock()
    37  	spans := make([]RawSpan, len(r.spans))
    38  	copy(spans, r.spans)
    39  	return spans
    40  }
    41  
    42  // GetSampledSpans returns a slice of spans accumulated so far which were sampled.
    43  func (r *InMemorySpanRecorder) GetSampledSpans() []RawSpan {
    44  	r.RLock()
    45  	defer r.RUnlock()
    46  	spans := make([]RawSpan, 0, len(r.spans))
    47  	for _, span := range r.spans {
    48  		if span.Context.Sampled {
    49  			spans = append(spans, span)
    50  		}
    51  	}
    52  	return spans
    53  }
    54  
    55  // Reset clears the internal array of spans.
    56  func (r *InMemorySpanRecorder) Reset() {
    57  	r.Lock()
    58  	defer r.Unlock()
    59  	r.spans = nil
    60  }