Skip to content

Commit

Permalink
Replace KeepLabel and DropLabel with NamedLabelMatcher
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Haudum <[email protected]>
  • Loading branch information
chaudum committed Feb 1, 2025
1 parent 0f0f218 commit df5fb62
Show file tree
Hide file tree
Showing 9 changed files with 309 additions and 359 deletions.
24 changes: 12 additions & 12 deletions pkg/logql/log/drop_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ import (
"github.com/grafana/loki/v3/pkg/logqlmodel"
)

type DropLabels struct {
dropLabels []DropLabel
}

type DropLabel struct {
type NamedLabelMatcher struct {
Matcher *labels.Matcher
Name string
}

func NewDropLabel(matcher *labels.Matcher, name string) DropLabel {
return DropLabel{
Matcher: matcher,
Name: name,
func NewNamedLabelMatcher(m *labels.Matcher, n string) NamedLabelMatcher {
return NamedLabelMatcher{
Matcher: m,
Name: n,
}
}

func NewDropLabels(dl []DropLabel) *DropLabels {
return &DropLabels{dropLabels: dl}
type DropLabels struct {
labels []NamedLabelMatcher
}

func NewDropLabels(labels []NamedLabelMatcher) *DropLabels {
return &DropLabels{labels: labels}
}

func (dl *DropLabels) Process(_ int64, line []byte, lbls *LabelsBuilder) ([]byte, bool) {
for _, dropLabel := range dl.dropLabels {
for _, dropLabel := range dl.labels {
if dropLabel.Matcher != nil {
dropLabelMatches(dropLabel.Matcher, lbls)
continue
Expand Down
12 changes: 6 additions & 6 deletions pkg/logql/log/drop_labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
func Test_DropLabels(t *testing.T) {
tests := []struct {
Name string
dropLabels []DropLabel
dropLabels []NamedLabelMatcher
err string
errDetails string
lbs labels.Labels
want labels.Labels
}{
{
"drop by name",
[]DropLabel{
[]NamedLabelMatcher{
{
nil,
"app",
Expand All @@ -40,7 +40,7 @@ func Test_DropLabels(t *testing.T) {
},
{
"drop by __error__",
[]DropLabel{
[]NamedLabelMatcher{
{
labels.MustNewMatcher(labels.MatchEqual, logqlmodel.ErrorLabel, errJSON),
"",
Expand All @@ -63,7 +63,7 @@ func Test_DropLabels(t *testing.T) {
},
{
"drop with wrong __error__ value",
[]DropLabel{
[]NamedLabelMatcher{
{
labels.MustNewMatcher(labels.MatchEqual, logqlmodel.ErrorLabel, errLogfmt),
"",
Expand All @@ -84,7 +84,7 @@ func Test_DropLabels(t *testing.T) {
},
{
"drop by __error_details__",
[]DropLabel{
[]NamedLabelMatcher{
{
labels.MustNewMatcher(labels.MatchRegexp, logqlmodel.ErrorDetailsLabel, "expecting json.*"),
"",
Expand All @@ -107,7 +107,7 @@ func Test_DropLabels(t *testing.T) {
},
{
"drop labels with names and matcher",
[]DropLabel{
[]NamedLabelMatcher{
{
labels.MustNewMatcher(labels.MatchEqual, logqlmodel.ErrorLabel, errJSON),
"",
Expand Down
24 changes: 5 additions & 19 deletions pkg/logql/log/keep_labels.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
package log

import (
"github.com/prometheus/prometheus/model/labels"

"github.com/grafana/loki/v3/pkg/logqlmodel"
)

type KeepLabels struct {
keepLabels []KeepLabel
}

type KeepLabel struct {
Matcher *labels.Matcher
Name string
}

func NewKeepLabel(matcher *labels.Matcher, name string) KeepLabel {
return KeepLabel{
Matcher: matcher,
Name: name,
}
labels []NamedLabelMatcher
}

func NewKeepLabels(kl []KeepLabel) *KeepLabels {
return &KeepLabels{keepLabels: kl}
func NewKeepLabels(labels []NamedLabelMatcher) *KeepLabels {
return &KeepLabels{labels: labels}
}

func (kl *KeepLabels) Process(_ int64, line []byte, lbls *LabelsBuilder) ([]byte, bool) {
if len(kl.keepLabels) == 0 {
if len(kl.labels) == 0 {
return line, true
}

Expand All @@ -38,7 +24,7 @@ func (kl *KeepLabels) Process(_ int64, line []byte, lbls *LabelsBuilder) ([]byte
}

var keep bool
for _, keepLabel := range kl.keepLabels {
for _, keepLabel := range kl.labels {
if keepLabel.Matcher != nil && keepLabel.Matcher.Name == lb.Name && keepLabel.Matcher.Matches(lb.Value) {
keep = true
break
Expand Down
10 changes: 5 additions & 5 deletions pkg/logql/log/keep_labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
func Test_KeepLabels(t *testing.T) {
for _, tc := range []struct {
Name string
keepLabels []KeepLabel
keepLabels []NamedLabelMatcher
lbs labels.Labels

want labels.Labels
}{
{
"keep all",
[]KeepLabel{},
[]NamedLabelMatcher{},
labels.FromStrings(
"app", "foo",
"namespace", "prod",
Expand All @@ -35,7 +35,7 @@ func Test_KeepLabels(t *testing.T) {
},
{
"keep by name",
[]KeepLabel{
[]NamedLabelMatcher{
{
nil,
"app",
Expand All @@ -58,7 +58,7 @@ func Test_KeepLabels(t *testing.T) {
},
{
"keep labels with names and matcher",
[]KeepLabel{
[]NamedLabelMatcher{
{
labels.MustNewMatcher(labels.MatchEqual, "namespace", "prod"),
"",
Expand All @@ -85,7 +85,7 @@ func Test_KeepLabels(t *testing.T) {
},
{
"preserve special labels",
[]KeepLabel{
[]NamedLabelMatcher{
{
labels.MustNewMatcher(labels.MatchEqual, "namespace", "prod"),
"",
Expand Down
10 changes: 5 additions & 5 deletions pkg/logql/log/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func TestDropLabelsPipeline(t *testing.T) {
[]Stage{
NewLogfmtParser(true, false),
NewJSONParser(),
NewDropLabels([]DropLabel{
NewDropLabels([]NamedLabelMatcher{
{
nil,
"__error__",
Expand Down Expand Up @@ -365,7 +365,7 @@ func TestDropLabelsPipeline(t *testing.T) {
[]Stage{
NewLogfmtParser(true, false),
NewJSONParser(),
NewDropLabels([]DropLabel{
NewDropLabels([]NamedLabelMatcher{
{
labels.MustNewMatcher(labels.MatchEqual, logqlmodel.ErrorLabel, errLogfmt),
"",
Expand Down Expand Up @@ -431,7 +431,7 @@ func TestKeepLabelsPipeline(t *testing.T) {
name: "keep all",
stages: []Stage{
NewLogfmtParser(false, false),
NewKeepLabels([]KeepLabel{}),
NewKeepLabels([]NamedLabelMatcher{}),
},
lines: [][]byte{
[]byte(`level=info ts=2020-10-18T18:04:22.147378997Z caller=metrics.go:81 status=200`),
Expand Down Expand Up @@ -467,7 +467,7 @@ func TestKeepLabelsPipeline(t *testing.T) {
name: "keep by name",
stages: []Stage{
NewLogfmtParser(false, false),
NewKeepLabels([]KeepLabel{
NewKeepLabels([]NamedLabelMatcher{
{
nil,
"level",
Expand Down Expand Up @@ -498,7 +498,7 @@ func TestKeepLabelsPipeline(t *testing.T) {
name: "keep by matcher",
stages: []Stage{
NewLogfmtParser(false, false),
NewKeepLabels([]KeepLabel{
NewKeepLabels([]NamedLabelMatcher{
{
labels.MustNewMatcher(labels.MatchEqual, "level", "info"),
"",
Expand Down
8 changes: 4 additions & 4 deletions pkg/logql/syntax/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,10 +829,10 @@ func (e *DecolorizeExpr) Walk(f WalkFn) { f(e) }
func (e *DecolorizeExpr) Accept(v RootVisitor) { v.VisitDecolorize(e) }

type DropLabelsExpr struct {
dropLabels []log.DropLabel
dropLabels []log.NamedLabelMatcher
}

func newDropLabelsExpr(dropLabels []log.DropLabel) *DropLabelsExpr {
func newDropLabelsExpr(dropLabels []log.NamedLabelMatcher) *DropLabelsExpr {
return &DropLabelsExpr{dropLabels: dropLabels}
}

Expand Down Expand Up @@ -869,10 +869,10 @@ func (e *DropLabelsExpr) Walk(f WalkFn) { f(e) }
func (e *DropLabelsExpr) Accept(v RootVisitor) { v.VisitDropLabels(e) }

type KeepLabelsExpr struct {
keepLabels []log.KeepLabel
keepLabels []log.NamedLabelMatcher
}

func newKeepLabelsExpr(keepLabels []log.KeepLabel) *KeepLabelsExpr {
func newKeepLabelsExpr(keepLabels []log.NamedLabelMatcher) *KeepLabelsExpr {
return &KeepLabelsExpr{keepLabels: keepLabels}
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/logql/syntax/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ func (v *cloneVisitor) VisitDecolorize(*DecolorizeExpr) {

func (v *cloneVisitor) VisitDropLabels(e *DropLabelsExpr) {
copied := &DropLabelsExpr{
dropLabels: make([]log.DropLabel, len(e.dropLabels)),
dropLabels: make([]log.NamedLabelMatcher, len(e.dropLabels)),
}
for i, l := range e.dropLabels {
var matcher *labels.Matcher
if l.Matcher != nil {
matcher = labels.MustNewMatcher(l.Matcher.Type, l.Matcher.Name, l.Matcher.Value)
}
copied.dropLabels[i] = log.NewDropLabel(matcher, l.Name)
copied.dropLabels[i] = log.NewNamedLabelMatcher(matcher, l.Name)
}

v.cloned = copied
Expand All @@ -170,10 +170,10 @@ func (v *cloneVisitor) VisitJSONExpressionParser(e *JSONExpressionParserExpr) {

func (v *cloneVisitor) VisitKeepLabel(e *KeepLabelsExpr) {
copied := &KeepLabelsExpr{
keepLabels: make([]log.KeepLabel, len(e.keepLabels)),
keepLabels: make([]log.NamedLabelMatcher, len(e.keepLabels)),
}
for i, k := range e.keepLabels {
copied.keepLabels[i] = log.KeepLabel{
copied.keepLabels[i] = log.NamedLabelMatcher{
Name: k.Name,
}
if k.Matcher != nil {
Expand Down
37 changes: 12 additions & 25 deletions pkg/logql/syntax/syntax.y
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ import (
filter log.LineMatchType
lineFilterExpr *LineFilterExpr
binOpts *BinOpOptions
dropLabel log.DropLabel
dropLabels []log.DropLabel
keepLabel log.KeepLabel
keepLabels []log.KeepLabel
namedMatcher log.NamedLabelMatcher
namedMatchers []log.NamedLabelMatcher
labelFormat log.LabelFmt
labelsFormat []log.LabelFmt
grouping *Grouping
Expand Down Expand Up @@ -61,10 +59,8 @@ import (
%type <str> vector
%type <strs> labels parserFlags
%type <binOpts> binOpModifier boolModifier onOrIgnoringModifier
%type <dropLabels> dropLabels
%type <dropLabel> dropLabel
%type <keepLabels> keepLabels
%type <keepLabel> keepLabel
%type <namedMatcher> namedMatcher
%type <namedMatchers> namedMatchers
%type <labelFormat> labelFormat
%type <labelsFormat> labelsFormat
%type <grouping> grouping
Expand Down Expand Up @@ -354,27 +350,18 @@ numberFilter:
| IDENTIFIER CMP_EQ literalExpr { $$ = log.NewNumericLabelFilter(log.LabelFilterEqual, $1, $3.Val)}
;

dropLabel:
IDENTIFIER { $$ = log.NewDropLabel(nil, $1) }
| matcher { $$ = log.NewDropLabel($1, "") }
namedMatcher:
IDENTIFIER { $$ = log.NewNamedLabelMatcher(nil, $1) }
| matcher { $$ = log.NewNamedLabelMatcher($1, "") }

dropLabels:
dropLabel { $$ = []log.DropLabel{$1}}
| dropLabels COMMA dropLabel { $$ = append($1, $3) }
namedMatchers:
namedMatcher { $$ = []log.NamedLabelMatcher{$1} }
| namedMatchers COMMA namedMatcher { $$ = append($1, $3) }
;

dropLabelsExpr: DROP dropLabels { $$ = newDropLabelsExpr($2) }
dropLabelsExpr: DROP namedMatchers { $$ = newDropLabelsExpr($2) }

keepLabel:
IDENTIFIER { $$ = log.NewKeepLabel(nil, $1) }
| matcher { $$ = log.NewKeepLabel($1, "") }

keepLabels:
keepLabel { $$ = []log.KeepLabel{$1}}
| keepLabels COMMA keepLabel { $$ = append($1, $3) }
;

keepLabelsExpr: KEEP keepLabels { $$ = newKeepLabelsExpr($2) }
keepLabelsExpr: KEEP namedMatchers { $$ = newKeepLabelsExpr($2) }

// Operator precedence only works if each of these is listed separately.
binOpExpr:
Expand Down
Loading

0 comments on commit df5fb62

Please sign in to comment.