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 1 commit
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
101 changes: 77 additions & 24 deletions pkg/logql/log/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,35 @@ func init() {
type LineFormatter struct {
*template.Template
buf *bytes.Buffer

requiredLabelNamesMap map[string]struct{}
requiredLabelNames []string
}

// NewFormatter creates a new log line formatter from a given text template.
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)
}
names := uniqueString(listNodeFields([]parse.Node{t.Root}))
namesMap := make(map[string]struct{}, len(names))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just a hashset? We don't seem to use the values.

Copy link
Contributor Author

@cyriltovena cyriltovena Nov 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is. I need more time to think about this PR. It does improve but I think we can do better at source I might just split this PR into 2.

for _, s := range names {
namesMap[s] = struct{}{}
}
return &LineFormatter{
Template: t,
buf: bytes.NewBuffer(make([]byte, 4096)),
Template: t,
buf: bytes.NewBuffer(make([]byte, 4096)),
requiredLabelNamesMap: namesMap,
requiredLabelNames: names,
}, nil
}

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 {

model := lf.buildModel(lbs)
if err := lf.Template.Execute(lf.buf, model); err != nil {
lbs.SetErr(errTemplateFormat)
return line, true
}
Expand All @@ -121,19 +133,39 @@ func (lf *LineFormatter) Process(line []byte, lbs *LabelsBuilder) ([]byte, bool)
return res, true
}

func (lf *LineFormatter) buildModel(lbs *LabelsBuilder) interface{} {
labels := lbs.Labels()
model := make(map[string]string, len(lf.requiredLabelNamesMap))
if len(lf.requiredLabelNames) > 0 {
for _, l := range labels {
if _, ok := lf.requiredLabelNamesMap[l.Name]; ok {
model[l.Name] = l.Value
}
}
}
return model
}

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

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 +188,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 @@ -210,6 +238,9 @@ type labelFormatter struct {
type LabelsFormatter struct {
formats []labelFormatter
buf *bytes.Buffer

requiredLabelNamesMap map[string]struct{}
requiredLabelNames []string
}

// NewLabelsFormatter creates a new formatter that can format multiple labels at once.
Expand All @@ -220,6 +251,8 @@ func NewLabelsFormatter(fmts []LabelFmt) (*LabelsFormatter, error) {
return nil, err
}
formats := make([]labelFormatter, 0, len(fmts))
var names []string

for _, fm := range fmts {
toAdd := labelFormatter{LabelFmt: fm}
if !fm.Rename {
Expand All @@ -228,12 +261,19 @@ func NewLabelsFormatter(fmts []LabelFmt) (*LabelsFormatter, error) {
return nil, fmt.Errorf("invalid template for label '%s': %s", fm.Name, err)
}
toAdd.tmpl = t
names = uniqueString(append(names, listNodeFields([]parse.Node{t.Root})...))
}
formats = append(formats, toAdd)
}
namesMap := make(map[string]struct{}, len(names))
for _, n := range names {
namesMap[n] = struct{}{}
}
return &LabelsFormatter{
formats: formats,
buf: bytes.NewBuffer(make([]byte, 1024)),
formats: formats,
buf: bytes.NewBuffer(make([]byte, 1024)),
requiredLabelNamesMap: namesMap,
requiredLabelNames: names,
}, nil
}

Expand All @@ -253,6 +293,19 @@ func validate(fmts []LabelFmt) error {
return nil
}

func (lf *LabelsFormatter) buildModel(lbs *LabelsBuilder) interface{} {
labels := lbs.Labels()
model := make(map[string]string, len(lf.requiredLabelNamesMap))
if len(lf.requiredLabelNames) > 0 {
for _, l := range labels {
if _, ok := lf.requiredLabelNamesMap[l.Name]; ok {
model[l.Name] = l.Value
}
}
}
return model
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be some code duplication.


func (lf *LabelsFormatter) Process(l []byte, lbs *LabelsBuilder) ([]byte, bool) {
var data interface{}
for _, f := range lf.formats {
Expand All @@ -266,7 +319,7 @@ func (lf *LabelsFormatter) Process(l []byte, lbs *LabelsBuilder) ([]byte, bool)
}
lf.buf.Reset()
if data == nil {
data = lbs.Labels().Map()
data = lf.buildModel(lbs)
}
if err := f.tmpl.Execute(lf.buf, data); err != nil {
lbs.SetErr(errTemplateFormat)
Expand All @@ -284,8 +337,8 @@ func (lf *LabelsFormatter) RequiredLabelNames() []string {
names = append(names, fm.Value)
continue
}
names = append(names, listNodeFields(fm.tmpl.Root)...)
}
names = append(names, lf.requiredLabelNames...)
return uniqueString(names)
}

Expand Down