-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathpipeline.go
395 lines (324 loc) · 10.2 KB
/
pipeline.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package log
import (
"context"
"reflect"
"sync"
"unsafe"
"github.com/prometheus/prometheus/storage/remote/otlptranslator/prometheus"
"github.com/prometheus/prometheus/model/labels"
)
// NoopStage is a stage that doesn't process a log line.
var NoopStage Stage = &noopStage{}
// Pipeline can create pipelines for each log stream.
type Pipeline interface {
ForStream(labels labels.Labels) StreamPipeline
Reset()
}
// StreamPipeline transform and filter log lines and labels.
// A StreamPipeline never mutate the received line.
type StreamPipeline interface {
BaseLabels() LabelsResult
// Process processes a log line and returns the transformed line and the labels.
// The buffer returned for the log line can be reused on subsequent calls to Process and therefore must be copied.
Process(ts int64, line []byte, structuredMetadata ...labels.Label) (resultLine []byte, resultLabels LabelsResult, matches bool)
ProcessString(ts int64, line string, structuredMetadata ...labels.Label) (resultLine string, resultLabels LabelsResult, matches bool)
ReferencedStructuredMetadata() bool
}
// Stage is a single step of a Pipeline.
// A Stage implementation should never mutate the line passed, but instead either
// return the line unchanged or allocate a new line.
type Stage interface {
Process(ts int64, line []byte, lbs *LabelsBuilder) ([]byte, bool)
RequiredLabelNames() []string
}
// PipelineWrapper takes a pipeline, wraps it is some desired functionality and
// returns a new pipeline
type PipelineWrapper interface {
Wrap(ctx context.Context, pipeline Pipeline, query, tenant string) Pipeline
}
// NewNoopPipeline creates a pipelines that does not process anything and returns log streams as is.
func NewNoopPipeline() Pipeline {
return &noopPipeline{
cache: map[uint64]*noopStreamPipeline{},
baseBuilder: NewBaseLabelsBuilder(),
}
}
type noopPipeline struct {
cache map[uint64]*noopStreamPipeline
baseBuilder *BaseLabelsBuilder
mu sync.RWMutex
}
func (n *noopPipeline) ForStream(labels labels.Labels) StreamPipeline {
h := n.baseBuilder.Hash(labels)
n.mu.RLock()
if cached, ok := n.cache[h]; ok {
n.mu.RUnlock()
return cached
}
n.mu.RUnlock()
sp := &noopStreamPipeline{n.baseBuilder.ForLabels(labels, h), make([]int, 0, 10)}
n.mu.Lock()
defer n.mu.Unlock()
n.cache[h] = sp
return sp
}
func (n *noopPipeline) Reset() {
n.mu.Lock()
defer n.mu.Unlock()
for k := range n.cache {
delete(n.cache, k)
}
}
// IsNoopPipeline tells if a pipeline is a Noop.
func IsNoopPipeline(p Pipeline) bool {
_, ok := p.(*noopPipeline)
return ok
}
type noopStreamPipeline struct {
builder *LabelsBuilder
offsetsBuf []int
}
func (n noopStreamPipeline) ReferencedStructuredMetadata() bool {
return false
}
func (n noopStreamPipeline) Process(_ int64, line []byte, structuredMetadata ...labels.Label) ([]byte, LabelsResult, bool) {
n.builder.Reset()
for i, lb := range structuredMetadata {
structuredMetadata[i].Name = prometheus.NormalizeLabel(lb.Name)
}
n.builder.Add(StructuredMetadataLabel, structuredMetadata...)
return line, n.builder.LabelsResult(), true
}
func (n noopStreamPipeline) ProcessString(ts int64, line string, structuredMetadata ...labels.Label) (string, LabelsResult, bool) {
_, lr, ok := n.Process(ts, unsafeGetBytes(line), structuredMetadata...)
return line, lr, ok
}
func (n noopStreamPipeline) BaseLabels() LabelsResult { return n.builder.currentResult }
type noopStage struct{}
func (noopStage) Process(_ int64, line []byte, _ *LabelsBuilder) ([]byte, bool) {
return line, true
}
func (noopStage) RequiredLabelNames() []string { return []string{} }
type StageFunc struct {
process func(ts int64, line []byte, lbs *LabelsBuilder) ([]byte, bool)
requiredLabels []string
}
func (fn StageFunc) Process(ts int64, line []byte, lbs *LabelsBuilder) ([]byte, bool) {
return fn.process(ts, line, lbs)
}
func (fn StageFunc) RequiredLabelNames() []string {
if fn.requiredLabels == nil {
return []string{}
}
return fn.requiredLabels
}
// pipeline is a combinations of multiple stages.
// It can also be reduced into a single stage for convenience.
type pipeline struct {
AnalyzablePipeline
stages []Stage
baseBuilder *BaseLabelsBuilder
mu sync.RWMutex
streamPipelines map[uint64]StreamPipeline
}
func (p *pipeline) Stages() []Stage {
return p.stages
}
func (p *pipeline) LabelsBuilder() *BaseLabelsBuilder {
return p.baseBuilder
}
type AnalyzablePipeline interface {
Pipeline
Stages() []Stage
LabelsBuilder() *BaseLabelsBuilder
}
// NewPipeline creates a new pipeline for a given set of stages.
func NewPipeline(stages []Stage) Pipeline {
if len(stages) == 0 {
return NewNoopPipeline()
}
hints := NewParserHint(nil, nil, false, false, "", stages)
builder := NewBaseLabelsBuilderWithGrouping(nil, hints, false, false)
return &pipeline{
stages: stages,
baseBuilder: builder,
streamPipelines: make(map[uint64]StreamPipeline),
}
}
type streamPipeline struct {
stages []Stage
builder *LabelsBuilder
offsetsBuf []int
}
func NewStreamPipeline(stages []Stage, labelsBuilder *LabelsBuilder) StreamPipeline {
return &streamPipeline{stages, labelsBuilder, make([]int, 0, 10)}
}
func (p *pipeline) ForStream(labels labels.Labels) StreamPipeline {
hash := p.baseBuilder.Hash(labels)
p.mu.RLock()
if res, ok := p.streamPipelines[hash]; ok {
p.mu.RUnlock()
return res
}
p.mu.RUnlock()
res := NewStreamPipeline(p.stages, p.baseBuilder.ForLabels(labels, hash))
p.mu.Lock()
defer p.mu.Unlock()
p.streamPipelines[hash] = res
return res
}
func (p *pipeline) Reset() {
p.mu.Lock()
defer p.mu.Unlock()
p.baseBuilder.Reset()
for k := range p.streamPipelines {
delete(p.streamPipelines, k)
}
}
func (p *streamPipeline) ReferencedStructuredMetadata() bool {
return p.builder.referencedStructuredMetadata
}
func (p *streamPipeline) Process(ts int64, line []byte, structuredMetadata ...labels.Label) ([]byte, LabelsResult, bool) {
var ok bool
p.builder.Reset()
for i, lb := range structuredMetadata {
structuredMetadata[i].Name = prometheus.NormalizeLabel(lb.Name)
}
p.builder.Add(StructuredMetadataLabel, structuredMetadata...)
for _, s := range p.stages {
line, ok = s.Process(ts, line, p.builder)
if !ok {
return nil, nil, false
}
}
return line, p.builder.LabelsResult(), true
}
func (p *streamPipeline) ProcessString(ts int64, line string, structuredMetadata ...labels.Label) (string, LabelsResult, bool) {
// Stages only read from the line.
lb, lr, ok := p.Process(ts, unsafeGetBytes(line), structuredMetadata...)
// but the returned line needs to be copied.
return string(lb), lr, ok
}
func (p *streamPipeline) BaseLabels() LabelsResult { return p.builder.currentResult }
// PipelineFilter contains a set of matchers and a pipeline that, when matched,
// causes an entry from a log stream to be skipped. Matching entries must also
// fall between 'start' and 'end', inclusive
type PipelineFilter struct {
Start int64
End int64
Matchers []*labels.Matcher
Pipeline Pipeline
}
// NewFilteringPipeline creates a pipeline where entries from the underlying
// log stream are filtered by pipeline filters before being passed to the
// pipeline representing the queried data. Filters are always upstream of the
// pipeline
func NewFilteringPipeline(f []PipelineFilter, p Pipeline) Pipeline {
return &filteringPipeline{
filters: f,
pipeline: p,
}
}
type filteringPipeline struct {
filters []PipelineFilter
pipeline Pipeline
}
func (p *filteringPipeline) ForStream(labels labels.Labels) StreamPipeline {
var streamFilters []streamFilter
for _, f := range p.filters {
if allMatch(f.Matchers, labels) {
streamFilters = append(streamFilters, streamFilter{
start: f.Start,
end: f.End,
pipeline: f.Pipeline.ForStream(labels),
})
}
}
return &filteringStreamPipeline{
filters: streamFilters,
pipeline: p.pipeline.ForStream(labels),
}
}
func (p *filteringPipeline) Reset() {
p.pipeline.Reset()
}
func allMatch(matchers []*labels.Matcher, labels labels.Labels) bool {
for _, m := range matchers {
if !m.Matches(labels.Get(m.Name)) {
return false
}
}
return true
}
type streamFilter struct {
start int64
end int64
pipeline StreamPipeline
}
type filteringStreamPipeline struct {
filters []streamFilter
pipeline StreamPipeline
}
func (sp *filteringStreamPipeline) ReferencedStructuredMetadata() bool {
return false
}
func (sp *filteringStreamPipeline) BaseLabels() LabelsResult {
return sp.pipeline.BaseLabels()
}
func (sp *filteringStreamPipeline) Process(ts int64, line []byte, structuredMetadata ...labels.Label) ([]byte, LabelsResult, bool) {
for _, filter := range sp.filters {
if ts < filter.start || ts > filter.end {
continue
}
_, _, matches := filter.pipeline.Process(ts, line, structuredMetadata...)
if matches { // When the filter matches, don't run the next step
return nil, nil, false
}
}
return sp.pipeline.Process(ts, line, structuredMetadata...)
}
func (sp *filteringStreamPipeline) ProcessString(ts int64, line string, structuredMetadata ...labels.Label) (string, LabelsResult, bool) {
for _, filter := range sp.filters {
if ts < filter.start || ts > filter.end {
continue
}
_, _, matches := filter.pipeline.ProcessString(ts, line, structuredMetadata...)
if matches { // When the filter matches, don't run the next step
return "", nil, false
}
}
return sp.pipeline.ProcessString(ts, line, structuredMetadata...)
}
// ReduceStages reduces multiple stages into one.
func ReduceStages(stages []Stage) Stage {
if len(stages) == 0 {
return NoopStage
}
var requiredLabelNames []string
for _, s := range stages {
requiredLabelNames = append(requiredLabelNames, s.RequiredLabelNames()...)
}
return StageFunc{
process: func(ts int64, line []byte, lbs *LabelsBuilder) ([]byte, bool) {
var ok bool
for _, p := range stages {
line, ok = p.Process(ts, line, lbs)
if !ok {
return nil, false
}
}
return line, true
},
requiredLabels: requiredLabelNames,
}
}
func unsafeGetBytes(s string) []byte {
var buf []byte
p := unsafe.Pointer(&buf)
*(*string)(p) = s
(*reflect.SliceHeader)(p).Cap = len(s)
return buf
}
func unsafeGetString(buf []byte) string {
return *((*string)(unsafe.Pointer(&buf)))
}