Skip to content

Commit

Permalink
Seal label filterers
Browse files Browse the repository at this point in the history
  • Loading branch information
jeschkies committed Nov 13, 2023
1 parent 0923606 commit 75b73bb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pkg/logql/log/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ func (f *IPLabelFilter) Process(_ int64, line []byte, lbs *LabelsBuilder) ([]byt
return line, f.filterTy(line, f.ty, lbs)
}

func (f *IPLabelFilter) isLabelFilterer() {}

// `RequiredLabelNames` implements `Stage` interface
func (f *IPLabelFilter) RequiredLabelNames() []string {
return []string{f.label}
Expand Down
18 changes: 18 additions & 0 deletions pkg/logql/log/label_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func (f LabelFilterType) String() string {
type LabelFilterer interface {
Stage
fmt.Stringer

// Seal trait
isLabelFilterer()
}

type BinaryLabelFilter struct {
Expand Down Expand Up @@ -94,6 +97,8 @@ func (b *BinaryLabelFilter) Process(ts int64, line []byte, lbs *LabelsBuilder) (
return line, lok && rok
}

func (b *BinaryLabelFilter) isLabelFilterer() {}

func (b *BinaryLabelFilter) RequiredLabelNames() []string {
var names []string
names = append(names, b.Left.RequiredLabelNames()...)
Expand Down Expand Up @@ -122,6 +127,9 @@ type NoopLabelFilter struct {
func (NoopLabelFilter) Process(_ int64, line []byte, _ *LabelsBuilder) ([]byte, bool) {
return line, true
}

func (NoopLabelFilter) isLabelFilterer() {}

func (NoopLabelFilter) RequiredLabelNames() []string { return []string{} }

func (f NoopLabelFilter) String() string {
Expand Down Expand Up @@ -197,6 +205,8 @@ func (d *BytesLabelFilter) Process(_ int64, line []byte, lbs *LabelsBuilder) ([]
}
}

func (b *BytesLabelFilter) isLabelFilterer() {}

func (d *BytesLabelFilter) RequiredLabelNames() []string {
return []string{d.Name}
}
Expand Down Expand Up @@ -262,6 +272,8 @@ func (d *DurationLabelFilter) Process(_ int64, line []byte, lbs *LabelsBuilder)
}
}

func (b *DurationLabelFilter) isLabelFilterer() {}

func (d *DurationLabelFilter) RequiredLabelNames() []string {
return []string{d.Name}
}
Expand Down Expand Up @@ -323,6 +335,8 @@ func (n *NumericLabelFilter) Process(_ int64, line []byte, lbs *LabelsBuilder) (

}

func (n *NumericLabelFilter) isLabelFilterer() {}

func (n *NumericLabelFilter) RequiredLabelNames() []string {
return []string{n.Name}
}
Expand Down Expand Up @@ -358,6 +372,8 @@ func (s *StringLabelFilter) Process(_ int64, line []byte, lbs *LabelsBuilder) ([
return line, s.Matches(labelValue(s.Name, lbs))
}

func (s *StringLabelFilter) isLabelFilterer() {}

func (s *StringLabelFilter) RequiredLabelNames() []string {
return []string{s.Name}
}
Expand All @@ -383,6 +399,8 @@ func (s *lineFilterLabelFilter) Process(_ int64, line []byte, lbs *LabelsBuilder
return line, s.filter.Filter(unsafeGetBytes(v))
}

func (s *lineFilterLabelFilter) isLabelFilterer() {}

func (s *lineFilterLabelFilter) RequiredLabelNames() []string {
return []string{s.Name}
}
Expand Down
20 changes: 18 additions & 2 deletions pkg/logql/syntax/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"time"

jsoniter "github.com/json-iterator/go"

"github.com/grafana/loki/pkg/logql/log"
)

type JSONSerializer struct {
Expand Down Expand Up @@ -312,11 +314,11 @@ func encodeUnwrap(s *jsoniter.Stream, u *UnwrapExpr) {
s.WriteMore()
s.WriteObjectField("post_filterers")
s.WriteArrayStart()
for i, group := range u.PostFilters{
for i, filter := range u.PostFilters {
if i > 0 {
s.WriteMore()
}
s.WriteString(group)
encodePostFilter(s, filter)
}
s.WriteArrayEnd()

Expand All @@ -339,6 +341,20 @@ func decodeUnwrap(iter *jsoniter.Iterator) *UnwrapExpr {
return e
}

func encodePostFilter(s *jsoniter.Stream, filter log.LabelFilterer) {
switch concrete := filter.(type) {
case *log.BinaryLabelFilter:
s.WriteObjectStart()
s.WriteObjectField("left")
encodePostFilter(s, concrete.Left)

s.WriteMore()
s.WriteObjectField("right")
encodePostFilter(s, concrete.Right)
s.WriteObjectEnd()
}
}

func encodeLogSelector(s *jsoniter.Stream, e LogSelectorExpr) {
s.WriteObjectStart()
s.WriteObjectField("raw")
Expand Down

0 comments on commit 75b73bb

Please sign in to comment.