Skip to content
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

Improve LogQL format stages requireLabel #4769

Merged
merged 3 commits into from
Nov 26, 2021
Merged
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
41 changes: 23 additions & 18 deletions pkg/logql/log/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ type LineFormatter struct {
func NewFormatter(tmpl string) (*LineFormatter, error) {
t, err := template.New("line").Option("missingkey=zero").Funcs(functionMap).Parse(tmpl)
if err != nil {
return nil, fmt.Errorf("invalid line template: %s", err)
return nil, fmt.Errorf("invalid line template: %w", err)
}
return &LineFormatter{
Template: t,
Expand All @@ -111,6 +111,7 @@ func NewFormatter(tmpl string) (*LineFormatter, error) {

func (lf *LineFormatter) Process(line []byte, lbs *LabelsBuilder) ([]byte, bool) {
lf.buf.Reset()

if err := lf.Template.Execute(lf.buf, lbs.Labels().Map()); err != nil {
lbs.SetErr(errTemplateFormat)
return line, true
Expand All @@ -122,18 +123,25 @@ func (lf *LineFormatter) Process(line []byte, lbs *LabelsBuilder) ([]byte, bool)
}

func (lf *LineFormatter) RequiredLabelNames() []string {
return uniqueString(listNodeFields(lf.Root))
return uniqueString(listNodeFields([]parse.Node{lf.Root}))
}

func listNodeFields(node parse.Node) []string {
func listNodeFields(nodes []parse.Node) []string {
var res []string
if node.Type() == parse.NodeAction {
res = append(res, listNodeFieldsFromPipe(node.(*parse.ActionNode).Pipe)...)
}
res = append(res, listNodeFieldsFromBranch(node)...)
if ln, ok := node.(*parse.ListNode); ok {
for _, n := range ln.Nodes {
res = append(res, listNodeFields(n)...)
for _, node := range nodes {
switch node.Type() {
case parse.NodePipe:
res = append(res, listNodeFieldsFromPipe(node.(*parse.PipeNode))...)
case parse.NodeAction:
res = append(res, listNodeFieldsFromPipe(node.(*parse.ActionNode).Pipe)...)
case parse.NodeList:
res = append(res, listNodeFields(node.(*parse.ListNode).Nodes)...)
case parse.NodeCommand:
res = append(res, listNodeFields(node.(*parse.CommandNode).Args)...)
case parse.NodeIf, parse.NodeWith, parse.NodeRange:
res = append(res, listNodeFieldsFromBranch(node)...)
case parse.NodeField:
res = append(res, node.(*parse.FieldNode).Ident...)
}
}
return res
Expand All @@ -156,22 +164,18 @@ func listNodeFieldsFromBranch(node parse.Node) []string {
res = append(res, listNodeFieldsFromPipe(b.Pipe)...)
}
if b.List != nil {
res = append(res, listNodeFields(b.List)...)
res = append(res, listNodeFields(b.List.Nodes)...)
}
if b.ElseList != nil {
res = append(res, listNodeFields(b.ElseList)...)
res = append(res, listNodeFields(b.ElseList.Nodes)...)
}
return res
}

func listNodeFieldsFromPipe(p *parse.PipeNode) []string {
var res []string
for _, c := range p.Cmds {
for _, a := range c.Args {
if f, ok := a.(*parse.FieldNode); ok {
res = append(res, f.Ident...)
}
}
res = append(res, listNodeFields(c.Args)...)
}
return res
}
Expand Down Expand Up @@ -220,6 +224,7 @@ func NewLabelsFormatter(fmts []LabelFmt) (*LabelsFormatter, error) {
return nil, err
}
formats := make([]labelFormatter, 0, len(fmts))

for _, fm := range fmts {
toAdd := labelFormatter{LabelFmt: fm}
if !fm.Rename {
Expand Down Expand Up @@ -284,7 +289,7 @@ func (lf *LabelsFormatter) RequiredLabelNames() []string {
names = append(names, fm.Value)
continue
}
names = append(names, listNodeFields(fm.tmpl.Root)...)
names = append(names, listNodeFields([]parse.Node{fm.tmpl.Root})...)
}
return uniqueString(names)
}
Expand Down