Skip to content

Commit

Permalink
feat: line_format bypass template execution if possible (#15411)
Browse files Browse the repository at this point in the history
Co-authored-by: Ed Welch <[email protected]>
  • Loading branch information
cyriltovena and slim-bean authored Dec 16, 2024
1 parent b8168a8 commit 2150fbc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
23 changes: 22 additions & 1 deletion pkg/logql/log/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ type LineFormatter struct {

currentLine []byte
currentTs int64
simpleKey string
}

// NewFormatter creates a new log line formatter from a given text template.
Expand All @@ -213,10 +214,30 @@ func NewFormatter(tmpl string) (*LineFormatter, error) {
return nil, fmt.Errorf("invalid line template: %w", err)
}
lf.Template = t

// determine if the template is a simple key substitution, e.g. line_format `{{.message}}`
// if it is, save the key name and we can use it later to directly copy the string
// bytes of the value to avoid copying and allocating a new string.
if len(t.Root.Nodes) == 1 && t.Root.Nodes[0].Type() == parse.NodeAction {
actionNode := t.Root.Nodes[0].(*parse.ActionNode)
if len(actionNode.Pipe.Cmds) == 1 && len(actionNode.Pipe.Cmds[0].Args) == 1 {
if fieldNode, ok := actionNode.Pipe.Cmds[0].Args[0].(*parse.FieldNode); ok && len(fieldNode.Ident) == 1 {
lf.simpleKey = fieldNode.Ident[0]
}
}
}

return lf, nil
}

func (lf *LineFormatter) Process(ts int64, line []byte, lbs *LabelsBuilder) ([]byte, bool) {
if lf.simpleKey != "" {
if val, ok := lbs.Get(lf.simpleKey); ok {
return unsafeGetBytes(val), true
}
return []byte{}, true
}

lf.buf.Reset()
lf.currentLine = line
lf.currentTs = ts
Expand Down Expand Up @@ -387,7 +408,7 @@ func (lf *LabelsFormatter) Process(ts int64, l []byte, lbs *LabelsBuilder) ([]by
lf.currentLine = l
lf.currentTs = ts

var m = smp.Get()
m := smp.Get()
defer smp.Put(m)
for _, f := range lf.formats {
if f.Rename {
Expand Down
32 changes: 29 additions & 3 deletions pkg/logql/log/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,33 @@ func Test_lineFormatter_Format(t *testing.T) {
labels.FromStrings("foo", "hello"),
[]byte("1"),
},
{
"simple key template",
newMustLineFormatter("{{.foo}}"),
labels.FromStrings("foo", "bar"),
0,
[]byte("bar"),
labels.FromStrings("foo", "bar"),
nil,
},
{
"simple key template with space",
newMustLineFormatter("{{.foo}} "),
labels.FromStrings("foo", "bar"),
0,
[]byte("bar "),
labels.FromStrings("foo", "bar"),
nil,
},
{
"simple key template with missing key",
newMustLineFormatter("{{.missing}}"),
labels.FromStrings("foo", "bar"),
0,
[]byte{},
labels.FromStrings("foo", "bar"),
nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -916,7 +943,7 @@ func TestLabelFormatter_RequiredLabelNames(t *testing.T) {
}

func TestDecolorizer(t *testing.T) {
var decolorizer, _ = NewDecolorizer()
decolorizer, _ := NewDecolorizer()
tests := []struct {
name string
src []byte
Expand All @@ -927,7 +954,7 @@ func TestDecolorizer(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var result, _ = decolorizer.Process(0, tt.src, nil)
result, _ := decolorizer.Process(0, tt.src, nil)
require.Equal(t, tt.expected, result)
})
}
Expand Down Expand Up @@ -979,7 +1006,6 @@ func TestMapPoolPanic(_ *testing.T) {
}
smp.Put(m)
wgFinished.Done()

}()
}
wg.Done()
Expand Down

0 comments on commit 2150fbc

Please sign in to comment.