Skip to content

Commit 3b91304

Browse files
authored
refact pkg/parser: extract method, avoid calling defer in loop (#3564)
1 parent 15dcbde commit 3b91304

File tree

1 file changed

+105
-76
lines changed

1 file changed

+105
-76
lines changed

pkg/parser/stage.go

Lines changed: 105 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -37,103 +37,132 @@ type Stagefile struct {
3737
}
3838

3939
func LoadStages(stageFiles []Stagefile, pctx *UnixParserCtx, ectx EnricherCtx) ([]Node, error) {
40-
var nodes []Node
41-
tmpstages := make(map[string]bool)
40+
var allNodes []Node
41+
42+
tmpStages := make(map[string]bool)
4243
pctx.Stages = []string{}
4344

44-
for _, stageFile := range stageFiles {
45-
if !strings.HasSuffix(stageFile.Filename, ".yaml") && !strings.HasSuffix(stageFile.Filename, ".yml") {
46-
log.Warningf("skip non yaml : %s", stageFile.Filename)
45+
for _, sf := range stageFiles {
46+
nodes, err := processStageFile(sf, pctx, ectx)
47+
if err != nil {
48+
return nil, err
49+
}
50+
51+
for _, n := range nodes { //nolint:gocritic // rangeValCopy
52+
allNodes = append(allNodes, n)
53+
tmpStages[n.Stage] = true
54+
}
55+
}
56+
57+
for k := range tmpStages {
58+
pctx.Stages = append(pctx.Stages, k)
59+
}
60+
61+
sort.Strings(pctx.Stages)
62+
log.Infof("Loaded %d nodes from %d stages", len(allNodes), len(pctx.Stages))
63+
64+
return allNodes, nil
65+
}
66+
67+
func processStageFile(stageFile Stagefile, pctx *UnixParserCtx, ectx EnricherCtx) ([]Node, error) {
68+
if !strings.HasSuffix(stageFile.Filename, ".yaml") && !strings.HasSuffix(stageFile.Filename, ".yml") {
69+
log.Warningf("skip non yaml : %s", stageFile.Filename)
70+
return nil, nil
71+
}
72+
73+
log.Debugf("loading parser file '%s'", stageFile)
74+
75+
st, err := os.Stat(stageFile.Filename)
76+
if err != nil {
77+
return nil, fmt.Errorf("failed to stat %s : %v", stageFile, err)
78+
}
79+
80+
if st.IsDir() {
81+
return nil, nil
82+
}
83+
84+
yamlFile, err := os.Open(stageFile.Filename)
85+
if err != nil {
86+
return nil, fmt.Errorf("can't access parsing configuration file %s : %s", stageFile.Filename, err)
87+
}
88+
defer yamlFile.Close()
89+
// process the yaml
90+
dec := yaml.NewDecoder(yamlFile)
91+
dec.SetStrict(true)
92+
93+
var nodes []Node
94+
95+
nodesCount := 0
96+
97+
for {
98+
node := Node{}
99+
node.OnSuccess = "continue" // default behavior is to continue
100+
101+
if err = dec.Decode(&node); err != nil {
102+
if errors.Is(err, io.EOF) {
103+
log.Tracef("End of yaml file")
104+
break
105+
}
106+
107+
return nil, fmt.Errorf("error decoding parsing configuration file '%s': %v", stageFile.Filename, err)
108+
}
109+
110+
// check for empty bucket
111+
if node.Name == "" && node.Description == "" && node.Author == "" {
112+
log.Infof("Node in %s has no name, author or description. Skipping.", stageFile.Filename)
47113
continue
48114
}
49-
log.Debugf("loading parser file '%s'", stageFile)
50-
st, err := os.Stat(stageFile.Filename)
115+
116+
// check compat
117+
if node.FormatVersion == "" {
118+
log.Tracef("no version in %s, assuming '1.0'", node.Name)
119+
node.FormatVersion = "1.0"
120+
}
121+
122+
ok, err := constraint.Satisfies(node.FormatVersion, constraint.Parser)
51123
if err != nil {
52-
return nil, fmt.Errorf("failed to stat %s : %v", stageFile, err)
124+
return nil, fmt.Errorf("failed to check version : %s", err)
53125
}
54-
if st.IsDir() {
126+
127+
if !ok {
128+
log.Errorf("%s : %s doesn't satisfy parser format %s, skip", node.Name, node.FormatVersion, constraint.Parser)
55129
continue
56130
}
57-
yamlFile, err := os.Open(stageFile.Filename)
131+
132+
node.Stage = stageFile.Stage
133+
// compile the node : grok pattern and expression
134+
135+
err = node.compile(pctx, ectx)
58136
if err != nil {
59-
return nil, fmt.Errorf("can't access parsing configuration file %s : %s", stageFile.Filename, err)
60-
}
61-
defer yamlFile.Close()
62-
//process the yaml
63-
dec := yaml.NewDecoder(yamlFile)
64-
dec.SetStrict(true)
65-
nodesCount := 0
66-
for {
67-
node := Node{}
68-
node.OnSuccess = "continue" //default behavior is to continue
69-
err = dec.Decode(&node)
70-
if err != nil {
71-
if errors.Is(err, io.EOF) {
72-
log.Tracef("End of yaml file")
73-
break
74-
}
75-
return nil, fmt.Errorf("error decoding parsing configuration file '%s': %v", stageFile.Filename, err)
137+
if node.Name != "" {
138+
return nil, fmt.Errorf("failed to compile node '%s' in '%s' : %s", node.Name, stageFile.Filename, err)
76139
}
77140

78-
//check for empty bucket
79-
if node.Name == "" && node.Description == "" && node.Author == "" {
80-
log.Infof("Node in %s has no name, author or description. Skipping.", stageFile.Filename)
81-
continue
82-
}
83-
//check compat
84-
if node.FormatVersion == "" {
85-
log.Tracef("no version in %s, assuming '1.0'", node.Name)
86-
node.FormatVersion = "1.0"
87-
}
88-
ok, err := constraint.Satisfies(node.FormatVersion, constraint.Parser)
89-
if err != nil {
90-
return nil, fmt.Errorf("failed to check version : %s", err)
91-
}
92-
if !ok {
93-
log.Errorf("%s : %s doesn't satisfy parser format %s, skip", node.Name, node.FormatVersion, constraint.Parser)
94-
continue
95-
}
141+
return nil, fmt.Errorf("failed to compile node in '%s' : %s", stageFile.Filename, err)
142+
}
143+
/* if the stage is empty, the node is empty, it's a trailing entry in users yaml file */
144+
if node.Stage == "" {
145+
continue
146+
}
96147

97-
node.Stage = stageFile.Stage
98-
if _, ok := tmpstages[stageFile.Stage]; !ok {
99-
tmpstages[stageFile.Stage] = true
100-
}
101-
//compile the node : grok pattern and expression
102-
err = node.compile(pctx, ectx)
148+
for _, data := range node.Data {
149+
err = exprhelpers.FileInit(pctx.DataFolder, data.DestPath, data.Type)
103150
if err != nil {
104-
if node.Name != "" {
105-
return nil, fmt.Errorf("failed to compile node '%s' in '%s' : %s", node.Name, stageFile.Filename, err)
106-
}
107-
return nil, fmt.Errorf("failed to compile node in '%s' : %s", stageFile.Filename, err)
108-
}
109-
/* if the stage is empty, the node is empty, it's a trailing entry in users yaml file */
110-
if node.Stage == "" {
111-
continue
151+
log.Error(err.Error())
112152
}
113153

114-
for _, data := range node.Data {
115-
err = exprhelpers.FileInit(pctx.DataFolder, data.DestPath, data.Type)
116-
if err != nil {
154+
if data.Type == "regexp" { // cache only makes sense for regexp
155+
if err = exprhelpers.RegexpCacheInit(data.DestPath, *data); err != nil {
117156
log.Error(err.Error())
118157
}
119-
if data.Type == "regexp" { //cache only makes sense for regexp
120-
if err = exprhelpers.RegexpCacheInit(data.DestPath, *data); err != nil {
121-
log.Error(err.Error())
122-
}
123-
}
124158
}
125-
126-
nodes = append(nodes, node)
127-
nodesCount++
128159
}
129-
log.WithFields(log.Fields{"file": stageFile.Filename, "stage": stageFile.Stage}).Infof("Loaded %d parser nodes", nodesCount)
130-
}
131160

132-
for k := range tmpstages {
133-
pctx.Stages = append(pctx.Stages, k)
161+
nodes = append(nodes, node)
162+
nodesCount++
134163
}
135-
sort.Strings(pctx.Stages)
136-
log.Infof("Loaded %d nodes from %d stages", len(nodes), len(pctx.Stages))
164+
165+
log.WithFields(log.Fields{"file": stageFile.Filename, "stage": stageFile.Stage}).Infof("Loaded %d parser nodes", nodesCount)
137166

138167
return nodes, nil
139168
}

0 commit comments

Comments
 (0)