-
Notifications
You must be signed in to change notification settings - Fork 0
/
visit.go
269 lines (235 loc) · 6.32 KB
/
visit.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package visit
import (
"encoding/hex"
"sync"
"github.com/RoaringBitmap/roaring/roaring64"
"github.com/cespare/xxhash/v2"
v1 "github.com/gernest/requiemdb/gen/go/rq/v1"
"github.com/gernest/requiemdb/internal/bitmaps"
"github.com/gernest/requiemdb/internal/labels"
commonV1 "go.opentelemetry.io/proto/otlp/common/v1"
resourceV1 "go.opentelemetry.io/proto/otlp/resource/v1"
)
type BaseResource interface {
GetSchemaUrl() string
GetResource() *resourceV1.Resource
}
func AcceptResource(r BaseResource, a *All) bool {
return a.AcceptResourceSchema(r.GetSchemaUrl()) &&
a.AcceptResourceAttributes(r.GetResource().GetAttributes())
}
func AcceptScope(schema string, r *commonV1.InstrumentationScope, a *All) bool {
return a.AcceptScopeSchema(schema) &&
a.AcceptScopeName(r.GetName()) &&
a.AcceptScopeVersion(r.GetVersion()) &&
a.AcceptScopeAttributes(r.GetAttributes())
}
type BaseDataPoint interface {
GetAttributes() []*commonV1.KeyValue
GetTimeUnixNano() uint64
}
// We pass start and end to avoid calling TimeRange on each data point
func AcceptDataPoint(tsn BaseDataPoint, a *All) bool {
return a.AcceptTimestamp(tsn.GetTimeUnixNano()) &&
a.AcceptAttributes(tsn.GetAttributes())
}
func VisitData(data *v1.Data, a *All) *v1.Data {
if a == nil {
return data
}
switch e := data.Data.(type) {
case *v1.Data_Metrics:
return &v1.Data{Data: &v1.Data_Metrics{
Metrics: Metrics{}.Visit(e.Metrics, a),
}}
case *v1.Data_Logs:
return &v1.Data{Data: &v1.Data_Logs{
Logs: Logs{}.Visit(e.Logs, a),
}}
case *v1.Data_Traces:
return &v1.Data{Data: &v1.Data_Traces{
Traces: Trace{}.Visit(e.Traces, a),
}}
default:
return data
}
}
type All struct {
resource_schema roaring64.Bitmap
resource_attr roaring64.Bitmap
scope_schema roaring64.Bitmap
scope_name roaring64.Bitmap
scope_version roaring64.Bitmap
scope_attr roaring64.Bitmap
attr roaring64.Bitmap
name roaring64.Bitmap
trace_id roaring64.Bitmap
span_id roaring64.Bitmap
parent_span_id roaring64.Bitmap
log_level roaring64.Bitmap
start_nano, end_nano uint64
}
func New() *All {
return allPool.Get().(*All)
}
var allPool = &sync.Pool{New: func() any { return new(All) }}
func (a *All) Reset() {
reset(
&a.resource_schema,
&a.resource_attr,
&a.scope_schema,
&a.scope_name,
&a.scope_version,
&a.scope_attr,
&a.attr,
&a.name,
&a.trace_id,
&a.span_id,
&a.parent_span_id,
&a.log_level,
)
a.start_nano = 0
a.end_nano = 0
}
func reset(r ...*roaring64.Bitmap) {
for _, v := range r {
v.Clear()
}
}
func (a *All) Release() {
a.Reset()
allPool.Put(a)
}
func (a *All) SetResourceSchema(schema string) {
a.resource_schema.Add(xxhash.Sum64String(schema))
}
func (a *All) SetResourceAttr(k, v string) {
a.resource_attr.Add(attr(k, v))
}
func attr(k, v string) uint64 {
var h xxhash.Digest
h.WriteString(k)
h.Write(labels.ValueSep)
h.WriteString(v)
return h.Sum64()
}
func (a *All) SetScopeSchema(schema string) {
a.scope_schema.Add(xxhash.Sum64String(schema))
}
func (a *All) SetScopeName(name string) {
a.scope_name.Add(xxhash.Sum64String(name))
}
func (a *All) SetScopVersion(version string) {
a.scope_version.Add(xxhash.Sum64String(version))
}
func (a *All) SetScopeAttr(k, v string) {
a.scope_attr.Add(attr(k, v))
}
func (a *All) SetAttr(k, v string) {
a.attr.Add(attr(k, v))
}
func (a *All) SetName(name string) {
a.name.Add(xxhash.Sum64String(name))
}
func (a *All) SetTraceID(id string) error {
h, err := hex.DecodeString(id)
if err != nil {
return err
}
a.trace_id.Add(xxhash.Sum64(h))
return nil
}
func (a *All) SetSpanID(id string) error {
h, err := hex.DecodeString(id)
if err != nil {
return err
}
a.span_id.Add(xxhash.Sum64(h))
return nil
}
func (a *All) SetParentSpanID(id string) error {
h, err := hex.DecodeString(id)
if err != nil {
return err
}
a.parent_span_id.Add(xxhash.Sum64(h))
return nil
}
func (a *All) SetLogLevel(lvl string) {
a.log_level.Add(xxhash.Sum64String(lvl))
}
func (a *All) SetTimeRange(start, end uint64) {
a.start_nano, a.end_nano = start, end
}
func (a *All) AcceptResourceSchema(schema string) bool {
if a.resource_schema.IsEmpty() {
return true
}
return a.resource_schema.Contains(xxhash.Sum64String(schema))
}
func (a *All) AcceptResourceAttributes(kv []*commonV1.KeyValue) bool {
return matchAttr(&a.resource_attr, kv)
}
func (a *All) AcceptScopeSchema(schema string) bool {
return a.scope_schema.IsEmpty() ||
a.scope_schema.Contains(xxhash.Sum64String(schema))
}
func (a *All) AcceptScopeName(name string) bool {
return a.scope_name.IsEmpty() ||
a.scope_name.Contains(xxhash.Sum64String(name))
}
func (a *All) AcceptScopeVersion(version string) bool {
return a.scope_version.IsEmpty() ||
a.scope_version.Contains(xxhash.Sum64String(version))
}
func (a *All) AcceptScopeAttributes(attr []*commonV1.KeyValue) bool {
return matchAttr(&a.scope_attr, attr)
}
func (a *All) AcceptName(name string) bool {
return a.name.IsEmpty() ||
a.name.Contains(xxhash.Sum64String(name))
}
func (a *All) AcceptAttributes(attr []*commonV1.KeyValue) bool {
return matchAttr(&a.attr, attr)
}
func (a *All) TimeRange() (start, end uint64) {
return a.start_nano, a.start_nano
}
func (a *All) AcceptLogLevel(lvl string) bool {
return a.log_level.IsEmpty() ||
a.log_level.Contains(xxhash.Sum64String(lvl))
}
func (a *All) AcceptTraceID(id []byte) bool {
return a.trace_id.IsEmpty() || a.trace_id.Contains(xxhash.Sum64(id))
}
func (a *All) AcceptSpanID(id []byte) bool {
return a.span_id.IsEmpty() || a.span_id.Contains(xxhash.Sum64(id))
}
func (a *All) AcceptParentSpanID(id []byte) bool {
return a.parent_span_id.IsEmpty() || a.parent_span_id.Contains(xxhash.Sum64(id))
}
func (a *All) AcceptTimestamp(ts uint64) bool {
return ts >= a.start_nano && ts < a.end_nano
}
func matchAttr(a *roaring64.Bitmap, ls []*commonV1.KeyValue) bool {
if a.IsEmpty() {
return true
}
b := bitmaps.New()
defer b.Release()
var h xxhash.Digest
for _, k := range ls {
switch e := k.Value.Value.(type) {
case *commonV1.AnyValue_StringValue:
h.Reset()
h.WriteString(k.Key)
h.Write(labels.ValueSep)
h.WriteString(e.StringValue)
b.Add(h.Sum64())
}
}
// For labels, we use And to make sure we only take data points that have all
// labels matching.
b.And(a)
return b.GetCardinality() == a.GetCardinality()
}