forked from grafana/tempo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlivetraces.go
More file actions
73 lines (58 loc) · 1.24 KB
/
livetraces.go
File metadata and controls
73 lines (58 loc) · 1.24 KB
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
72
73
package localblocks
import (
"hash"
"hash/fnv"
"time"
v1 "github.com/grafana/tempo/pkg/tempopb/trace/v1"
)
type liveTrace struct {
id []byte
timestamp time.Time
Batches []*v1.ResourceSpans
}
type liveTraces struct {
hash hash.Hash64
traces map[uint64]*liveTrace
}
func newLiveTraces() *liveTraces {
return &liveTraces{
hash: fnv.New64(),
traces: make(map[uint64]*liveTrace),
}
}
func (l *liveTraces) token(traceID []byte) uint64 {
l.hash.Reset()
l.hash.Write(traceID)
return l.hash.Sum64()
}
func (l *liveTraces) Len() uint64 {
return uint64(len(l.traces))
}
func (l *liveTraces) Push(traceID []byte, batch *v1.ResourceSpans, max uint64) bool {
token := l.token(traceID)
tr := l.traces[token]
if tr == nil {
// Before adding this check against max
// Zero means no limit
if max > 0 && uint64(len(l.traces)) >= max {
return false
}
tr = &liveTrace{
id: traceID,
}
l.traces[token] = tr
}
tr.Batches = append(tr.Batches, batch)
tr.timestamp = time.Now()
return true
}
func (l *liveTraces) CutIdle(idleSince time.Time) []*liveTrace {
res := []*liveTrace{}
for k, tr := range l.traces {
if tr.timestamp.Before(idleSince) {
res = append(res, tr)
delete(l.traces, k)
}
}
return res
}