Skip to content

fix: LineFilterLabelFilter.String() regexp correct delimiters #17129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/logql/log/label_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,12 @@ type LineFilterLabelFilter struct {
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)
if strings.Contains(rStr, "`") {
rStr = strconv.Quote(rStr)
} else {
rStr = fmt.Sprintf("`%s`", rStr)
}
str := fmt.Sprintf("%s%s%s", s.Matcher.Name, s.Matcher.Type, rStr)
return str
}
return s.Matcher.String()
Expand Down
48 changes: 48 additions & 0 deletions pkg/logql/log/label_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,51 @@ func BenchmarkLineLabelFilters(b *testing.B) {
})
}
}

func TestLineFilterLabelFilter_String(t *testing.T) {
type fields struct {
Matcher *labels.Matcher
Filter Filterer
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "it correctly surrounds regex containing backticks with double quotes",
fields: fields{
Matcher: labels.MustNewMatcher(labels.MatchRegexp, "msg", "`.`"),
Filter: mustFilter(newRegexpFilter("`.`", "`.`", true)),
},
want: "msg=~\"`.`\"",
},
{
name: "it correctly surrounds regex containing double quote with backticks",
fields: fields{
Matcher: labels.MustNewMatcher(labels.MatchRegexp, "msg", `"`),
Filter: mustFilter(newRegexpFilter(`"`, `"`, true)),
},
want: "msg=~`\"`",
},
{
name: "it correctly surrounds regex containing both backticks and double quotes with double quotes",
fields: fields{
Matcher: labels.MustNewMatcher(labels.MatchRegexp, "msg", "`\""),
Filter: mustFilter(newRegexpFilter("`\"", "`\"", true)),
},
want: "msg=~\"`\\\"\"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &LineFilterLabelFilter{
Matcher: tt.fields.Matcher,
Filter: tt.fields.Filter,
}
if got := s.String(); got != tt.want {
t.Errorf("LineFilterLabelFilter.String() = %v, want %v", got, tt.want)
}
})
}
}
17 changes: 17 additions & 0 deletions pkg/logql/syntax/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,16 @@ var ParseTestCases = []struct {
Without: false,
}, nil),
},
{
in: "{ foo = \"bar\" } | logfmt | msg =~ \"`.`\" ",
exp: newPipelineExpr(
newMatcherExpr([]*labels.Matcher{mustNewMatcher(labels.MatchEqual, "foo", "bar")}),
MultiStageExpr{
newLogfmtParserExpr(nil),
newLabelFilterExpr(log.NewStringLabelFilter(mustNewMatcher(labels.MatchRegexp, "msg", "`.`"))),
},
),
},
{
in: `unk({ foo = "bar" }[5m])`,
err: logqlmodel.NewParseError("syntax error: unexpected IDENTIFIER", 1, 1),
Expand Down Expand Up @@ -3714,4 +3724,11 @@ func TestParseSampleExpr_String(t *testing.T) {
// escaping is hard: the result is {cluster="beep", namespace="boop"} | msg=~`\w.*` which is equivalent to the original
require.Equal(t, "{cluster=\"beep\", namespace=\"boop\"} | msg=~`\\w.*`", expr.String())
})

t.Run("it correctly surrounds regex containing backticks with double quotes", func(t *testing.T) {
query := "{foo=\"bar\"} | logfmt | msg=~\"`.\\\"`\""
expr, err := ParseExpr(query)
require.NoError(t, err)
require.Equal(t, query, expr.String())
})
}
Loading