Skip to content

Commit

Permalink
MEDIUM: Add support for the traces section
Browse files Browse the repository at this point in the history
  • Loading branch information
oliwer authored and mjuraga committed Dec 23, 2024
1 parent cd0563e commit ca2e234
Show file tree
Hide file tree
Showing 36 changed files with 2,360 additions and 3 deletions.
2 changes: 1 addition & 1 deletion config-parser/generate/config-file.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func (c *ConfigFile) StringFiles(baseFolder string) {
}
usedNiceNames[niceName] = struct{}{}
sectionName := " test"
if sectionType == "global" {
if sectionType == "global" || sectionType == "traces" {
sectionName = ""
}
oneTest := "const " + niceName + " = `\n" + sectionType + sectionName + "\n" + " " + line + "\n`" + "\n"
Expand Down
2 changes: 2 additions & 0 deletions config-parser/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type ConfiguredParsers struct {
LogForward *Parsers
FCGIApp *Parsers
CrtStore *Parsers
Traces *Parsers
// spoe parsers
SPOEAgent *Parsers
SPOEGroup *Parsers
Expand Down Expand Up @@ -95,4 +96,5 @@ func (p *configParser) initParserMaps() {
p.Parsers[LogForward] = map[string]*Parsers{}
p.Parsers[FCGIApp] = map[string]*Parsers{}
p.Parsers[CrtStore] = map[string]*Parsers{}
p.Parsers[Traces] = map[string]*Parsers{}
}
2 changes: 2 additions & 0 deletions config-parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const (
LogForward Section = "log-forward"
FCGIApp Section = "fcgi-app"
CrtStore Section = "crt-store"
Traces Section = "traces"
// spoe sections
SPOEAgent Section = "spoe-agent"
SPOEGroup Section = "spoe-group"
Expand All @@ -56,6 +57,7 @@ const (
const (
CommentsSectionName = "data"
GlobalSectionName = "data"
TracesSectionName = ""
)

var DefaultSectionName = "" //nolint:gochecknoglobals
Expand Down
58 changes: 58 additions & 0 deletions config-parser/parsers/trace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2024 HAProxy Technologies
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package parsers

import (
"github.com/haproxytech/client-native/v6/config-parser/common"
"github.com/haproxytech/client-native/v6/config-parser/errors"
"github.com/haproxytech/client-native/v6/config-parser/types"
)

type Trace struct {
data []types.Trace
preComments []string // comments that appear before the actual line
}

func (t *Trace) parse(line string, parts []string, comment string) (*types.Trace, error) {
if parts[0] != t.GetParserName() {
return nil, &errors.ParseError{Parser: t.GetParserName(), Line: line}
}
if len(parts) < 2 {
return nil, &errors.ParseError{Parser: t.GetParserName(), Line: line, Message: "Parse error: not enough arguments"}
}

return &types.Trace{
Params: parts[1:],
Comment: comment,
}, nil
}

func (t *Trace) Result() ([]common.ReturnResultLine, error) {
if len(t.data) == 0 {
return nil, errors.ErrFetch
}

result := make([]common.ReturnResultLine, len(t.data))

for i, trace := range t.data {
result[i] = common.ReturnResultLine{
Data: t.GetParserName() + " " + common.SmartJoin(trace.Params...),
Comment: trace.Comment,
}
}
return result, nil
}
157 changes: 157 additions & 0 deletions config-parser/parsers/trace_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions config-parser/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,15 @@ func (p *configParser) ProcessLine(line string, parts []string, comment string,
if p.Options.Log {
p.Options.Logger.Tracef("%scrt-store section %s active", p.Options.LogPrefix, data.Name)
}
case "traces":
if config.Traces == nil {
config.Traces = p.getTracesParser()
p.Parsers[Traces][TracesSectionName] = config.Traces
if p.Options.Log {
p.Options.Logger.Tracef("%straces section active", p.Options.LogPrefix)
}
}
config.Active = config.Traces
case "snippet_beg":
config.Previous = config.Active
config.Active = &Parsers{
Expand Down
8 changes: 8 additions & 0 deletions config-parser/section-parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (p *configParser) createParsers(parser map[string]ParserInterface, sequence
addParser(parser, &sequence, &extra.Section{Name: "log-forward"})
addParser(parser, &sequence, &extra.Section{Name: "fcgi-app"})
addParser(parser, &sequence, &extra.Section{Name: "crt-store"})
addParser(parser, &sequence, &extra.Section{Name: "traces"})
if !p.Options.DisableUnProcessed {
addParser(parser, &sequence, &extra.UnProcessed{})
}
Expand Down Expand Up @@ -978,3 +979,10 @@ func (p *configParser) getCrtStoreParser() *Parsers {
addParser(parser, &sequence, &parsers.LoadCert{})
return p.createParsers(parser, sequence)
}

func (p *configParser) getTracesParser() *Parsers {
parser := map[string]ParserInterface{}
sequence := []Section{}
addParser(parser, &sequence, &parsers.Trace{})
return p.createParsers(parser, sequence)
}
4 changes: 4 additions & 0 deletions config-parser/tests/configs/haproxy.cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ cache foobar
total-max-size 4
max-age 240
traces
trace h1 sink buf1 level developer verbosity complete start now
trace h2 sink buf2 level developer verbosity complete start now
crt-store tpm2
crt-base /c
key-base /k
Expand Down
5 changes: 5 additions & 0 deletions config-parser/tests/configs/haproxy_generated.cfg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions config-parser/tests/integration/traces_data_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ca2e234

Please sign in to comment.