-
Notifications
You must be signed in to change notification settings - Fork 54
/
spans_buffer.go
57 lines (47 loc) · 1.28 KB
/
spans_buffer.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
package thrift_rpc
import "github.com/lightstep/lightstep-tracer-go/basictracer"
const defaultMaxSpans = 1000
type spansBuffer struct {
rawSpans []basictracer.RawSpan
maxBufferSize int
}
func (b *spansBuffer) setDefaults() {
b.maxBufferSize = defaultMaxSpans
b.rawSpans = make([]basictracer.RawSpan, 0, b.maxBufferSize)
}
func (b *spansBuffer) setMaxBufferSize(size int) {
b.maxBufferSize = size
}
func (b *spansBuffer) len() int {
return len(b.rawSpans)
}
func (b *spansBuffer) cap() int {
return b.maxBufferSize
}
func (b *spansBuffer) reset() {
// Reuse the existing buffer if it's the correct size
if cap(b.rawSpans) == b.maxBufferSize {
b.rawSpans = b.rawSpans[:0]
} else {
b.rawSpans = make([]basictracer.RawSpan, 0, b.maxBufferSize)
}
}
func (b *spansBuffer) current() []basictracer.RawSpan {
dst := make([]basictracer.RawSpan, len(b.rawSpans))
copy(dst, b.rawSpans)
return dst
}
// addSpans returns the number of spans dropped (0 if all were added to the
// buffer).
func (b *spansBuffer) addSpans(spans []basictracer.RawSpan) (droppedSpans int) {
space := b.maxBufferSize - len(b.rawSpans)
count := space
if len(spans) < count {
count = len(spans)
}
if count > 0 {
b.rawSpans = append(b.rawSpans, spans[:count]...)
}
droppedSpans = len(spans) - count
return
}