-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make InMemorySpanRecorder public; use sampling.priority to turn sampl…
…ing on and off (#30)
- Loading branch information
1 parent
1050f2f
commit f061a1a
Showing
4 changed files
with
65 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,60 @@ | ||
package basictracer | ||
|
||
import "sync" | ||
|
||
// A SpanRecorder handles all of the `RawSpan` data generated via an | ||
// associated `Tracer` (see `NewStandardTracer`) instance. It also names | ||
// the containing process and provides access to a straightforward tag map. | ||
type SpanRecorder interface { | ||
// Implementations must determine whether and where to store `span`. | ||
RecordSpan(span RawSpan) | ||
} | ||
|
||
// InMemorySpanRecorder is a simple thread-safe implementation of | ||
// SpanRecorder that stores all reported spans in memory, accessible | ||
// via reporter.GetSpans(). It is primarily intended for testing purposes. | ||
type InMemorySpanRecorder struct { | ||
sync.RWMutex | ||
spans []RawSpan | ||
} | ||
|
||
// NewInMemoryRecorder creates new InMemorySpanRecorder | ||
func NewInMemoryRecorder() *InMemorySpanRecorder { | ||
return new(InMemorySpanRecorder) | ||
} | ||
|
||
// RecordSpan implements the respective method of SpanRecorder. | ||
func (r *InMemorySpanRecorder) RecordSpan(span RawSpan) { | ||
r.Lock() | ||
defer r.Unlock() | ||
r.spans = append(r.spans, span) | ||
} | ||
|
||
// GetSpans returns a copy of the array of spans accumulated so far. | ||
func (r *InMemorySpanRecorder) GetSpans() []RawSpan { | ||
r.RLock() | ||
defer r.RUnlock() | ||
spans := make([]RawSpan, len(r.spans)) | ||
copy(spans, r.spans) | ||
return spans | ||
} | ||
|
||
// GetSampledSpans returns a slice of spans accumulated so far which were sampled. | ||
func (r *InMemorySpanRecorder) GetSampledSpans() []RawSpan { | ||
r.RLock() | ||
defer r.RUnlock() | ||
spans := make([]RawSpan, 0, len(r.spans)) | ||
for _, span := range r.spans { | ||
if span.Sampled { | ||
spans = append(spans, span) | ||
} | ||
} | ||
return spans | ||
} | ||
|
||
// Reset clears the internal array of spans. | ||
func (r *InMemorySpanRecorder) Reset() { | ||
r.Lock() | ||
defer r.Unlock() | ||
r.spans = nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters