Skip to content

Commit

Permalink
serialize filters
Browse files Browse the repository at this point in the history
  • Loading branch information
jeschkies committed Nov 14, 2023
1 parent f74a465 commit 3d79f2d
Show file tree
Hide file tree
Showing 14 changed files with 280 additions and 45 deletions.
28 changes: 14 additions & 14 deletions pkg/logql/log/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,41 +78,41 @@ func (f *IPLineFilter) filterTy(line []byte, ty labels.MatchType) bool {

type IPLabelFilter struct {
ip *ipFilter
ty LabelFilterType
Ty LabelFilterType

// if used as label matcher, this holds the identifier label name.
// if used as Label matcher, this holds the identifier Label name.
// e.g: (|remote_addr = ip("xxx")). Here labelName is `remote_addr`
label string
Label string

// patError records if given pattern is invalid.
patError error

// local copy of pattern to display it in errors, even though pattern matcher fails because of invalid pattern.
pattern string
// local copy of Pattern to display it in errors, even though Pattern matcher fails because of invalid Pattern.
Pattern string
}

// NewIPLabelFilter is used to construct ip filter as label filter for the given `label`.
func NewIPLabelFilter(pattern string, label string, ty LabelFilterType) *IPLabelFilter {
func NewIPLabelFilter(pattern, label string, ty LabelFilterType) *IPLabelFilter {
ip, err := newIPFilter(pattern)
return &IPLabelFilter{
ip: ip,
label: label,
ty: ty,
Label: label,
Ty: ty,
patError: err,
pattern: pattern,
Pattern: pattern,
}
}

// `Process` implements `Stage` interface
func (f *IPLabelFilter) Process(_ int64, line []byte, lbs *LabelsBuilder) ([]byte, bool) {
return line, f.filterTy(line, f.ty, lbs)
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}
return []string{f.Label}
}

// PatternError will be used `labelFilter.Stage()` method so that, if the given pattern is wrong
Expand All @@ -126,7 +126,7 @@ func (f *IPLabelFilter) filterTy(_ []byte, ty LabelFilterType, lbs *LabelsBuilde
// why `true`?. if there's an error only the string matchers can filter out.
return true
}
input, ok := lbs.Get(f.label)
input, ok := lbs.Get(f.Label)
if !ok {
// we have not found the label.
return false
Expand All @@ -148,11 +148,11 @@ func (f *IPLabelFilter) filterTy(_ []byte, ty LabelFilterType, lbs *LabelsBuilde
// `String` implements fmt.Stringer inteface, by which also implements `LabelFilterer` inteface.
func (f *IPLabelFilter) String() string {
eq := "=" // LabelFilterEqual -> "==", we don't want in string representation of ip label filter.
if f.ty == LabelFilterNotEqual {
if f.Ty == LabelFilterNotEqual {
eq = LabelFilterNotEqual.String()
}

return fmt.Sprintf("%s%sip(%q)", f.label, eq, f.pattern) // label filter
return fmt.Sprintf("%s%sip(%q)", f.Label, eq, f.Pattern) // label filter
}

// ipFilter search for IP addresses of given `pattern` in the given `line`.
Expand Down
26 changes: 13 additions & 13 deletions pkg/logql/log/label_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ type LabelFilterer interface {
type BinaryLabelFilter struct {
Left LabelFilterer
Right LabelFilterer
and bool
And bool
}

// NewAndLabelFilter creates a new LabelFilterer from a and binary operation of two LabelFilterer.
func NewAndLabelFilter(left LabelFilterer, right LabelFilterer) *BinaryLabelFilter {
return &BinaryLabelFilter{
Left: left,
Right: right,
and: true,
And: true,
}
}

Expand All @@ -89,11 +89,11 @@ func NewOrLabelFilter(left LabelFilterer, right LabelFilterer) *BinaryLabelFilte

func (b *BinaryLabelFilter) Process(ts int64, line []byte, lbs *LabelsBuilder) ([]byte, bool) {
line, lok := b.Left.Process(ts, line, lbs)
if !b.and && lok {
if !b.And && lok {
return line, true
}
line, rok := b.Right.Process(ts, line, lbs)
if !b.and {
if !b.And {
return line, lok || rok
}
return line, lok && rok
Expand All @@ -112,7 +112,7 @@ func (b *BinaryLabelFilter) String() string {
var sb strings.Builder
sb.WriteString("( ")
sb.WriteString(b.Left.String())
if b.and {
if b.And {
sb.WriteString(" , ")
} else {
sb.WriteString(" or ")
Expand Down Expand Up @@ -219,7 +219,7 @@ func (d *BytesLabelFilter) String() string {
return -1
}
return r
}, humanize.IBytes(d.Value)) // TODO: discuss whether this should just be bytes, B, to be more accurate.
}, humanize.Bytes(d.Value)) // TODO: discuss whether this should just be bytes, B, to be more accurate.
return fmt.Sprintf("%s%s%s", d.Name, d.Type, b)
}

Expand Down Expand Up @@ -364,7 +364,7 @@ func NewStringLabelFilter(m *labels.Matcher) LabelFilterer {
return &NoopLabelFilter{m}
}

return &lineFilterLabelFilter{
return &LineFilterLabelFilter{
Matcher: m,
filter: f,
}
Expand All @@ -380,14 +380,14 @@ func (s *StringLabelFilter) RequiredLabelNames() []string {
return []string{s.Name}
}

// lineFilterLabelFilter filters the desired label using an optimized line filter
type lineFilterLabelFilter struct {
// LineFilterLabelFilter filters the desired label using an optimized line filter
type LineFilterLabelFilter struct {
*labels.Matcher
filter Filterer
}

// overrides the matcher.String() function in case there is a regexpFilter
func (s *lineFilterLabelFilter) String() string {
func (s *LineFilterLabelFilter) String() string {
if unwrappedFilter, ok := s.filter.(regexpFilter); ok {
rStr := unwrappedFilter.String()
str := fmt.Sprintf("%s%s`%s`", s.Matcher.Name, s.Matcher.Type, rStr)
Expand All @@ -396,14 +396,14 @@ func (s *lineFilterLabelFilter) String() string {
return s.Matcher.String()
}

func (s *lineFilterLabelFilter) Process(_ int64, line []byte, lbs *LabelsBuilder) ([]byte, bool) {
func (s *LineFilterLabelFilter) Process(_ int64, line []byte, lbs *LabelsBuilder) ([]byte, bool) {
v := labelValue(s.Name, lbs)
return line, s.filter.Filter(unsafeGetBytes(v))
}

func (s *lineFilterLabelFilter) isLabelFilterer() {}
func (s *LineFilterLabelFilter) isLabelFilterer() {}

func (s *lineFilterLabelFilter) RequiredLabelNames() []string {
func (s *LineFilterLabelFilter) RequiredLabelNames() []string {
return []string{s.Name}
}

Expand Down
Loading

0 comments on commit 3d79f2d

Please sign in to comment.