This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
/
test_utils.go
71 lines (60 loc) · 1.71 KB
/
test_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package tracer
import (
"sync"
"sync/atomic"
)
// InMemorySpanReporter is a simple thread-safe implementation of
// SpanReporter that stores all reported spans in memory, accessible
// via reporter.getSpans(). It is primarily intended for testing purposes.
type InMemorySpanReporter struct {
sync.RWMutex
spans []RawSpan
}
// NewInMemoryReporter creates new InMemorySpanReporter
func NewInMemoryReporter() *InMemorySpanReporter {
return new(InMemorySpanReporter)
}
// ReportSpan implements the respective method of SpanReporter.
func (r *InMemorySpanReporter) ReportSpan(span RawSpan) {
r.Lock()
defer r.Unlock()
r.spans = append(r.spans, span)
}
func (r *InMemorySpanReporter) Close() error {
return nil
}
// getSpans returns a copy of the array of spans accumulated so far.
func (r *InMemorySpanReporter) 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 *InMemorySpanReporter) getSampledSpans() []RawSpan {
r.RLock()
defer r.RUnlock()
spans := make([]RawSpan, 0, len(r.spans))
for _, span := range r.spans {
if !span.Context.IsSampled() || *span.Context.Sampled {
spans = append(spans, span)
}
}
return spans
}
// Reset clears the internal array of spans.
func (r *InMemorySpanReporter) Reset() {
r.Lock()
defer r.Unlock()
r.spans = nil
}
// CountingReporter it is primarily intended for testing purposes.
type CountingReporter int32
// ReportSpan implements the respective method of SpanReporter.
func (c *CountingReporter) ReportSpan(r RawSpan) {
atomic.AddInt32((*int32)(c), 1)
}
func (c *CountingReporter) Close() error {
return nil
}