-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
ast.go
2371 lines (2028 loc) · 59.8 KB
/
ast.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package syntax
import (
"fmt"
"math"
"regexp"
"strconv"
"strings"
"time"
"github.com/grafana/loki/v3/pkg/util"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql"
"github.com/grafana/regexp/syntax"
"github.com/grafana/loki/v3/pkg/logql/log"
"github.com/grafana/loki/v3/pkg/logqlmodel"
)
// Expr is the root expression which can be a SampleExpr or LogSelectorExpr
//
//sumtype:decl
type Expr interface {
logQLExpr() // ensure it's not implemented accidentally
Shardable(topLevel bool) bool // A recursive check on the AST to see if it's shardable.
Walkable
fmt.Stringer
AcceptVisitor
// Pretty prettyfies any LogQL expression at given `level` of the whole LogQL query.
Pretty(level int) string
}
func Clone[T Expr](e T) (T, error) {
var empty T
v := &cloneVisitor{}
e.Accept(v)
cast, ok := v.cloned.(T)
if !ok {
return empty, fmt.Errorf("unexpected type of cloned expression: want %T, got %T", empty, v.cloned)
}
return cast, nil
}
func MustClone[T Expr](e T) T {
copied, err := Clone[T](e)
if err != nil {
panic(err)
}
return copied
}
func ExtractLineFilters(e Expr) []LineFilterExpr {
if e == nil {
return nil
}
var filters []LineFilterExpr
visitor := &DepthFirstTraversal{
VisitLineFilterFn: func(v RootVisitor, e *LineFilterExpr) {
if e != nil {
filters = append(filters, *e)
}
},
}
e.Accept(visitor)
return filters
}
// implicit holds default implementations
type implicit struct{}
func (implicit) logQLExpr() {}
// LogSelectorExpr is a LogQL expression filtering and returning logs.
//
//sumtype:decl
type LogSelectorExpr interface {
Matchers() []*labels.Matcher
Pipeline() (Pipeline, error)
HasFilter() bool
Expr
isLogSelectorExpr()
}
// Type alias for backward compatibility
type (
Pipeline = log.Pipeline
SampleExtractor = log.SampleExtractor
)
// StageExpr is an expression defining a single step into a log pipeline
//
//sumtype:decl
type StageExpr interface {
Stage() (log.Stage, error)
Expr
isStageExpr()
}
// MultiStageExpr is multiple stages which implements a LogSelectorExpr.
type MultiStageExpr []StageExpr
func (m MultiStageExpr) Pipeline() (log.Pipeline, error) {
stages, err := m.stages()
if err != nil {
return nil, err
}
return log.NewPipeline(stages), nil
}
func (m MultiStageExpr) stages() ([]log.Stage, error) {
c := make([]log.Stage, 0, len(m))
for _, e := range m.reorderStages() {
p, err := e.Stage()
if err != nil {
return nil, logqlmodel.NewStageError(e.String(), err)
}
if p == log.NoopStage {
continue
}
c = append(c, p)
}
return c, nil
}
// reorderStages reorders m such that LineFilters
// are as close to the front of the filter as possible.
func (m MultiStageExpr) reorderStages() []StageExpr {
var (
result = make([]StageExpr, 0, len(m))
filters = make([]*LineFilterExpr, 0, len(m))
rest = make([]StageExpr, 0, len(m))
)
for _, s := range m {
switch f := s.(type) {
case *LineFilterExpr:
filters = append(filters, f)
case *LineFmtExpr:
// line_format modifies the contents of the line so any line filter
// originally after a line_format must still be after the same
// line_format.
rest = append(rest, f)
if len(filters) > 0 {
result = append(result, combineFilters(filters))
}
result = append(result, rest...)
filters = filters[:0]
rest = rest[:0]
case *LabelParserExpr:
rest = append(rest, f)
// unpack modifies the contents of the line so any line filter
// originally after an unpack must still be after the same
// unpack.
if f.Op == OpParserTypeUnpack {
if len(filters) > 0 {
result = append(result, combineFilters(filters))
}
result = append(result, rest...)
filters = filters[:0]
rest = rest[:0]
}
default:
rest = append(rest, f)
}
}
if len(filters) > 0 {
result = append(result, combineFilters(filters))
}
return append(result, rest...)
}
func combineFilters(in []*LineFilterExpr) StageExpr {
result := in[len(in)-1]
for i := len(in) - 2; i >= 0; i-- {
leaf := leafNode(result, in[i])
if leaf != nil {
leaf.Left = in[i]
}
}
return result
}
func leafNode(in *LineFilterExpr, child *LineFilterExpr) *LineFilterExpr {
current := in
//nolint:revive
for ; current.Left != nil; current = current.Left {
if current == child || current.Left == child {
return nil
}
}
return current
}
func (m MultiStageExpr) String() string {
var sb strings.Builder
for i, e := range m {
sb.WriteString(e.String())
if i+1 != len(m) {
sb.WriteString(" ")
}
}
return sb.String()
}
func (MultiStageExpr) logQLExpr() {} // nolint:unused
type MatchersExpr struct {
Mts []*labels.Matcher
implicit
}
func newMatcherExpr(matchers []*labels.Matcher) *MatchersExpr {
return &MatchersExpr{Mts: matchers}
}
func (e *MatchersExpr) isLogSelectorExpr() {}
func (e *MatchersExpr) Matchers() []*labels.Matcher {
return e.Mts
}
func (e *MatchersExpr) AppendMatchers(m []*labels.Matcher) {
e.Mts = append(e.Mts, m...)
}
func (e *MatchersExpr) Shardable(_ bool) bool { return true }
func (e *MatchersExpr) Walk(f WalkFn) { f(e) }
func (e *MatchersExpr) Accept(v RootVisitor) { v.VisitMatchers(e) }
func (e *MatchersExpr) String() string {
var sb strings.Builder
sb.WriteString("{")
for i, m := range e.Mts {
sb.WriteString(m.String())
if i+1 != len(e.Mts) {
sb.WriteString(", ")
}
}
sb.WriteString("}")
return sb.String()
}
func (e *MatchersExpr) Pipeline() (log.Pipeline, error) {
return log.NewNoopPipeline(), nil
}
func (e *MatchersExpr) HasFilter() bool {
return false
}
type PipelineExpr struct {
MultiStages MultiStageExpr
Left *MatchersExpr
implicit
}
func newPipelineExpr(left *MatchersExpr, pipeline MultiStageExpr) LogSelectorExpr {
return &PipelineExpr{
Left: left,
MultiStages: pipeline,
}
}
func (e *PipelineExpr) isLogSelectorExpr() {}
func (e *PipelineExpr) Shardable(topLevel bool) bool {
for _, p := range e.MultiStages {
if !p.Shardable(topLevel) {
return false
}
}
return true
}
func (e *PipelineExpr) Walk(f WalkFn) {
f(e)
if e.Left == nil {
return
}
xs := make([]Walkable, 0, len(e.MultiStages)+1)
xs = append(xs, e.Left)
for _, p := range e.MultiStages {
xs = append(xs, p)
}
walkAll(f, xs...)
}
func (e *PipelineExpr) Accept(v RootVisitor) { v.VisitPipeline(e) }
func (e *PipelineExpr) Matchers() []*labels.Matcher {
return e.Left.Matchers()
}
func (e *PipelineExpr) String() string {
var sb strings.Builder
sb.WriteString(e.Left.String())
sb.WriteString(" ")
sb.WriteString(e.MultiStages.String())
return sb.String()
}
func (e *PipelineExpr) Pipeline() (log.Pipeline, error) {
return e.MultiStages.Pipeline()
}
// HasFilter returns true if the pipeline contains stage that can filter out lines.
func (e *PipelineExpr) HasFilter() bool {
for _, p := range e.MultiStages {
switch v := p.(type) {
case *LabelFilterExpr:
return true
case *LineFilterExpr:
// ignore empty matchers as they match everything
if !((v.Ty == log.LineMatchEqual || v.Ty == log.LineMatchRegexp) && v.Match == "") {
return true
}
default:
continue
}
}
return false
}
type LineFilter struct {
Ty log.LineMatchType
Match string
Op string
}
type LineFilterExpr struct {
LineFilter
Left *LineFilterExpr
// Or in LineFilterExpr works as follows.
//
// Case 1: With MatchEqual operators(|= or |~, etc)
// example: `{app="loki"} |= "test" |= "foo" or "bar"`
// expectation: match "test" AND (either "foo" OR "bar")
//
// Case 2: With NotMatchEqual operators (!= or !~, etc)
// example: `{app="loki"} != "test" != "foo" or "bar"`
// expectation: match !"test" AND !"foo" AND !"bar", Basically exactly as if `{app="loki"} != "test" != "foo" != "bar".
// See LineFilterExpr tests for more examples.
Or *LineFilterExpr
IsOrChild bool
implicit
}
func newLineFilterExpr(ty log.LineMatchType, op, match string) *LineFilterExpr {
return &LineFilterExpr{
LineFilter: LineFilter{
Ty: ty,
Match: match,
Op: op,
},
}
}
func newOrLineFilter(left, right *LineFilterExpr) *LineFilterExpr {
right.Ty = left.Ty
// NOTE: Consider, we have chain of "or", != "foo" or "bar" or "baz"
// we parse from right to left, so first time left="bar", right="baz", and we don't know the actual `Ty` (equal: |=, notequal: !=, regex: |~, etc). So
// it will have default (0, LineMatchEqual).
// we only know real `Ty` in next stage, where left="foo", right="bar or baz", at this time, `Ty` is LineMatchNotEqual(!=).
// Now we need to update not just `right.Ty = left.Ty`, we also have to update all the `right.Or` until `right.Or` is nil.
tmp := right
for tmp.Or != nil {
tmp.Or.Ty = left.Ty
tmp = tmp.Or
}
if left.Ty == log.LineMatchEqual || left.Ty == log.LineMatchRegexp || left.Ty == log.LineMatchPattern {
left.Or = right
right.IsOrChild = true
return left
}
// !(left or right) == (!left and !right).
return newNestedLineFilterExpr(left, right)
}
func newNestedLineFilterExpr(left *LineFilterExpr, right *LineFilterExpr) *LineFilterExpr {
// NOTE: When parsing "or" chains in linefilter, particularly variations of NOT filters (!= or !~), we need to transform
// say (!= "foo" or "bar "baz") => (!="foo" != "bar" != "baz")
if right.Or != nil && !(right.Ty == log.LineMatchEqual || right.Ty == log.LineMatchRegexp || right.Ty == log.LineMatchPattern) {
right.Or.IsOrChild = false
tmp := right.Or
right.Or = nil
right = newNestedLineFilterExpr(right, tmp)
}
// NOTE: Before supporting `or` in linefilter, `right` will always be a leaf node (right.next == nil)
// After supporting `or` in linefilter, `right` may not be a leaf node (e.g: |= "a" or "b). Here "b".Left = "a")
// We traverse the tree recursively untile we make `right` leaf node.
// Consider the following expression. {app="loki"} != "test" != "foo" or "bar or "car""
// It first creates following tree on the left and transformed into the one on the right.
// ┌────────────┐
// ┌────────────┐ │ root │
// │ root │ └──────┬─────┘
// └──────┬─────┘ │
// │ │
// │ │
// │ ─────────► ┌────────────────┴─────────────┐
// ┌────────────────┴─────────────┐ │ │
// │ │ ┌───┴────┐ │
// │ │ │ foo │ ┌──┴────┐
// │ ┌─┴────┐ └───┬────┘ │ bar │
// ┌───┴────┐ │ bar │ │ └───────┘
// │ test │ └──┬───┘ │
// └────────┘ │ │
// │ ┌────────┘
// │ │
// ┌─────────┘ │
// │ │
// │ ┌───┴───┐
// │ │ test │
// ┌─┴────┐ └───────┘
// │ foo │
// └──────┘
if right.Left != nil {
left = newNestedLineFilterExpr(left, right.Left)
}
return &LineFilterExpr{
Left: left,
LineFilter: right.LineFilter,
Or: right.Or,
IsOrChild: right.IsOrChild,
}
}
func (*LineFilterExpr) isStageExpr() {}
func (e *LineFilterExpr) Walk(f WalkFn) {
f(e)
if e.Left == nil {
return
}
e.Left.Walk(f)
}
func (e *LineFilterExpr) Accept(v RootVisitor) {
v.VisitLineFilter(e)
}
// AddFilterExpr adds a filter expression to a logselector expression.
func AddFilterExpr(expr LogSelectorExpr, ty log.LineMatchType, op, match string) (LogSelectorExpr, error) {
filter := newLineFilterExpr(ty, op, match)
switch e := expr.(type) {
case *MatchersExpr:
return newPipelineExpr(e, MultiStageExpr{filter}), nil
case *PipelineExpr:
e.MultiStages = append(e.MultiStages, filter)
return e, nil
default:
return nil, fmt.Errorf("unknown LogSelector: %v+", expr)
}
}
func (e *LineFilterExpr) Shardable(_ bool) bool { return true }
func (e *LineFilterExpr) String() string {
var sb strings.Builder
if e.Left != nil {
sb.WriteString(e.Left.String())
sb.WriteString(" ")
}
if !e.IsOrChild { // Only write the type when we're not chaining "or" filters
sb.WriteString(e.Ty.String())
sb.WriteString(" ")
}
if e.Op == "" {
sb.WriteString(strconv.Quote(e.Match))
} else {
sb.WriteString(e.Op)
sb.WriteString("(")
sb.WriteString(strconv.Quote(e.Match))
sb.WriteString(")")
}
if e.Or != nil {
sb.WriteString(" or ")
// This is dirty but removes the leading MatchType from the or expression.
sb.WriteString(e.Or.String())
}
return sb.String()
}
func (e *LineFilterExpr) Filter() (log.Filterer, error) {
acc := make([]log.Filterer, 0)
for curr := e; curr != nil; curr = curr.Left {
var next log.Filterer
var err error
if curr.Or != nil {
next, err = newOrFilter(curr)
if err != nil {
return nil, err
}
acc = append(acc, next)
} else {
switch curr.Op {
case OpFilterIP:
next, err := log.NewIPLineFilter(curr.Match, curr.Ty)
if err != nil {
return nil, err
}
acc = append(acc, next)
default:
next, err = log.NewFilter(curr.Match, curr.Ty)
if err != nil {
return nil, err
}
acc = append(acc, next)
}
}
}
if len(acc) == 1 {
return acc[0], nil
}
// The accumulation is right to left so it needs to be reversed.
for i := len(acc)/2 - 1; i >= 0; i-- {
opp := len(acc) - 1 - i
acc[i], acc[opp] = acc[opp], acc[i]
}
return log.NewAndFilters(acc), nil
}
func newOrFilter(f *LineFilterExpr) (log.Filterer, error) {
orFilter, err := log.NewFilter(f.Match, f.Ty)
if err != nil {
return nil, err
}
for or := f.Or; or != nil; or = or.Or {
filter, err := log.NewFilter(or.Match, or.Ty)
if err != nil {
return nil, err
}
orFilter = log.ChainOrFilter(orFilter, filter)
}
return orFilter, nil
}
func (e *LineFilterExpr) Stage() (log.Stage, error) {
f, err := e.Filter()
if err != nil {
return nil, err
}
return f.ToStage(), nil
}
type LogfmtParserExpr struct {
Strict bool
KeepEmpty bool
implicit
}
func newLogfmtParserExpr(flags []string) *LogfmtParserExpr {
e := LogfmtParserExpr{}
for _, f := range flags {
switch f {
case OpStrict:
e.Strict = true
case OpKeepEmpty:
e.KeepEmpty = true
}
}
return &e
}
func (*LogfmtParserExpr) isStageExpr() {}
func (e *LogfmtParserExpr) Shardable(_ bool) bool { return true }
func (e *LogfmtParserExpr) Walk(f WalkFn) { f(e) }
func (e *LogfmtParserExpr) Accept(v RootVisitor) { v.VisitLogfmtParser(e) }
func (e *LogfmtParserExpr) Stage() (log.Stage, error) {
return log.NewLogfmtParser(e.Strict, e.KeepEmpty), nil
}
func (e *LogfmtParserExpr) String() string {
var sb strings.Builder
sb.WriteString(OpPipe)
sb.WriteString(" ")
sb.WriteString(OpParserTypeLogfmt)
if e.Strict {
sb.WriteString(" ")
sb.WriteString(OpStrict)
}
if e.KeepEmpty {
sb.WriteString(" ")
sb.WriteString(OpKeepEmpty)
}
return sb.String()
}
type LabelParserExpr struct {
Op string
Param string
implicit
}
func newLabelParserExpr(op, param string) *LabelParserExpr {
if op == OpParserTypeRegexp {
_, err := log.NewRegexpParser(param)
if err != nil {
panic(logqlmodel.NewParseError(fmt.Sprintf("invalid regexp parser: %s", err.Error()), 0, 0))
}
}
if op == OpParserTypePattern {
_, err := log.NewPatternParser(param)
if err != nil {
panic(logqlmodel.NewParseError(fmt.Sprintf("invalid pattern parser: %s", err.Error()), 0, 0))
}
}
return &LabelParserExpr{
Op: op,
Param: param,
}
}
func (*LabelParserExpr) isStageExpr() {}
func (e *LabelParserExpr) Shardable(_ bool) bool { return true }
func (e *LabelParserExpr) Walk(f WalkFn) { f(e) }
func (e *LabelParserExpr) Accept(v RootVisitor) { v.VisitLabelParser(e) }
func (e *LabelParserExpr) Stage() (log.Stage, error) {
switch e.Op {
case OpParserTypeJSON:
return log.NewJSONParser(), nil
case OpParserTypeRegexp:
return log.NewRegexpParser(e.Param)
case OpParserTypeUnpack:
return log.NewUnpackParser(), nil
case OpParserTypePattern:
return log.NewPatternParser(e.Param)
default:
return nil, fmt.Errorf("unknown parser operator: %s", e.Op)
}
}
func (e *LabelParserExpr) String() string {
var sb strings.Builder
sb.WriteString(OpPipe)
sb.WriteString(" ")
sb.WriteString(e.Op)
if e.Param != "" {
sb.WriteString(" ")
sb.WriteString(strconv.Quote(e.Param))
}
if (e.Op == OpParserTypeRegexp || e.Op == OpParserTypePattern) && e.Param == "" {
sb.WriteString(" \"\"")
}
return sb.String()
}
type LabelFilterExpr struct {
log.LabelFilterer
implicit
}
func newLabelFilterExpr(filterer log.LabelFilterer) *LabelFilterExpr {
return &LabelFilterExpr{
LabelFilterer: filterer,
}
}
func (*LabelFilterExpr) isStageExpr() {}
func (e *LabelFilterExpr) Shardable(_ bool) bool { return true }
func (e *LabelFilterExpr) Walk(f WalkFn) { f(e) }
func (e *LabelFilterExpr) Accept(v RootVisitor) { v.VisitLabelFilter(e) }
func (e *LabelFilterExpr) Stage() (log.Stage, error) {
switch ip := e.LabelFilterer.(type) {
case *log.IPLabelFilter:
return ip, ip.PatternError()
case *log.NoopLabelFilter:
return log.NoopStage, nil
}
return e.LabelFilterer, nil
}
func (e *LabelFilterExpr) String() string {
return fmt.Sprintf("%s %s", OpPipe, e.LabelFilterer.String())
}
type LineFmtExpr struct {
Value string
implicit
}
func newLineFmtExpr(value string) *LineFmtExpr {
return &LineFmtExpr{
Value: value,
}
}
type DecolorizeExpr struct {
implicit
}
func newDecolorizeExpr() *DecolorizeExpr {
return &DecolorizeExpr{}
}
func (*DecolorizeExpr) isStageExpr() {}
func (e *DecolorizeExpr) Shardable(_ bool) bool { return true }
func (e *DecolorizeExpr) Stage() (log.Stage, error) {
return log.NewDecolorizer()
}
func (e *DecolorizeExpr) String() string {
return fmt.Sprintf("%s %s", OpPipe, OpDecolorize)
}
func (e *DecolorizeExpr) Walk(f WalkFn) { f(e) }
func (e *DecolorizeExpr) Accept(v RootVisitor) { v.VisitDecolorize(e) }
type DropLabelsExpr struct {
dropLabels []log.DropLabel
implicit
}
func newDropLabelsExpr(dropLabels []log.DropLabel) *DropLabelsExpr {
return &DropLabelsExpr{dropLabels: dropLabels}
}
func (*DropLabelsExpr) isStageExpr() {}
func (e *DropLabelsExpr) Shardable(_ bool) bool { return true }
func (e *DropLabelsExpr) Stage() (log.Stage, error) {
return log.NewDropLabels(e.dropLabels), nil
}
func (e *DropLabelsExpr) String() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%s %s ", OpPipe, OpDrop))
for i, dropLabel := range e.dropLabels {
if dropLabel.Matcher != nil {
sb.WriteString(dropLabel.Matcher.String())
if i+1 != len(e.dropLabels) {
sb.WriteString(",")
}
}
if dropLabel.Name != "" {
sb.WriteString(dropLabel.Name)
if i+1 != len(e.dropLabels) {
sb.WriteString(",")
}
}
}
str := sb.String()
return str
}
func (e *DropLabelsExpr) Walk(f WalkFn) { f(e) }
func (e *DropLabelsExpr) Accept(v RootVisitor) { v.VisitDropLabels(e) }
type KeepLabelsExpr struct {
keepLabels []log.KeepLabel
implicit
}
func newKeepLabelsExpr(keepLabels []log.KeepLabel) *KeepLabelsExpr {
return &KeepLabelsExpr{keepLabels: keepLabels}
}
func (*KeepLabelsExpr) isStageExpr() {}
func (e *KeepLabelsExpr) Shardable(_ bool) bool { return true }
func (e *KeepLabelsExpr) Stage() (log.Stage, error) {
return log.NewKeepLabels(e.keepLabels), nil
}
func (e *KeepLabelsExpr) String() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%s %s ", OpPipe, OpKeep))
for i, keepLabel := range e.keepLabels {
if keepLabel.Matcher != nil {
sb.WriteString(keepLabel.Matcher.String())
if i+1 != len(e.keepLabels) {
sb.WriteString(",")
}
}
if keepLabel.Name != "" {
sb.WriteString(keepLabel.Name)
if i+1 != len(e.keepLabels) {
sb.WriteString(",")
}
}
}
str := sb.String()
return str
}
func (e *KeepLabelsExpr) Walk(f WalkFn) { f(e) }
func (e *KeepLabelsExpr) Accept(v RootVisitor) { v.VisitKeepLabel(e) }
func (*LineFmtExpr) isStageExpr() {}
func (e *LineFmtExpr) Shardable(_ bool) bool { return true }
func (e *LineFmtExpr) Walk(f WalkFn) { f(e) }
func (e *LineFmtExpr) Accept(v RootVisitor) { v.VisitLineFmt(e) }
func (e *LineFmtExpr) Stage() (log.Stage, error) {
return log.NewFormatter(e.Value)
}
func (e *LineFmtExpr) String() string {
return fmt.Sprintf("%s %s %s", OpPipe, OpFmtLine, strconv.Quote(e.Value))
}
type LabelFmtExpr struct {
Formats []log.LabelFmt
implicit
}
func newLabelFmtExpr(fmts []log.LabelFmt) *LabelFmtExpr {
return &LabelFmtExpr{
Formats: fmts,
}
}
func (*LabelFmtExpr) isStageExpr() {}
func (e *LabelFmtExpr) Shardable(_ bool) bool {
// While LabelFmt is shardable in certain cases, it is not always,
// but this is left to the shardmapper to determine
return true
}
func (e *LabelFmtExpr) Walk(f WalkFn) { f(e) }
func (e *LabelFmtExpr) Accept(v RootVisitor) { v.VisitLabelFmt(e) }
func (e *LabelFmtExpr) Stage() (log.Stage, error) {
return log.NewLabelsFormatter(e.Formats)
}
func (e *LabelFmtExpr) String() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%s %s ", OpPipe, OpFmtLabel))
for i, f := range e.Formats {
sb.WriteString(f.Name)
sb.WriteString("=")
if f.Rename {
sb.WriteString(f.Value)
} else {
sb.WriteString(strconv.Quote(f.Value))
}
if i+1 != len(e.Formats) {
sb.WriteString(",")
}
}
return sb.String()
}
type JSONExpressionParser struct {
Expressions []log.LabelExtractionExpr
implicit
}
func newJSONExpressionParser(expressions []log.LabelExtractionExpr) *JSONExpressionParser {
return &JSONExpressionParser{
Expressions: expressions,
}
}
func (*JSONExpressionParser) isStageExpr() {}
func (j *JSONExpressionParser) Shardable(_ bool) bool { return true }
func (j *JSONExpressionParser) Walk(f WalkFn) { f(j) }
func (j *JSONExpressionParser) Accept(v RootVisitor) { v.VisitJSONExpressionParser(j) }
func (j *JSONExpressionParser) Stage() (log.Stage, error) {
return log.NewJSONExpressionParser(j.Expressions)
}
func (j *JSONExpressionParser) String() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%s %s ", OpPipe, OpParserTypeJSON))
for i, exp := range j.Expressions {
sb.WriteString(exp.Identifier)
sb.WriteString("=")
sb.WriteString(strconv.Quote(exp.Expression))
if i+1 != len(j.Expressions) {
sb.WriteString(",")
}
}
return sb.String()
}
type internedStringSet map[string]struct {
s string
ok bool
}
type LogfmtExpressionParser struct {
Expressions []log.LabelExtractionExpr
Strict, KeepEmpty bool
implicit
}
func newLogfmtExpressionParser(expressions []log.LabelExtractionExpr, flags []string) *LogfmtExpressionParser {
e := LogfmtExpressionParser{
Expressions: expressions,
}
for _, flag := range flags {
switch flag {
case OpStrict:
e.Strict = true
case OpKeepEmpty:
e.KeepEmpty = true
}
}
return &e
}
func (*LogfmtExpressionParser) isStageExpr() {}
func (l *LogfmtExpressionParser) Shardable(_ bool) bool { return true }
func (l *LogfmtExpressionParser) Walk(f WalkFn) { f(l) }
func (l *LogfmtExpressionParser) Accept(v RootVisitor) { v.VisitLogfmtExpressionParser(l) }
func (l *LogfmtExpressionParser) Stage() (log.Stage, error) {
return log.NewLogfmtExpressionParser(l.Expressions, l.Strict)
}
func (l *LogfmtExpressionParser) String() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%s %s ", OpPipe, OpParserTypeLogfmt))
if l.Strict {
sb.WriteString(OpStrict)
sb.WriteString(" ")
}
if l.KeepEmpty {
sb.WriteString(OpKeepEmpty)
sb.WriteString(" ")
}