Skip to content

Commit 4e08b88

Browse files
authored
refact pkg/parser: extract method processLeaves (#3886)
* refact pkg/parser: extract method processLeaves * lint
1 parent 075447c commit 4e08b88

File tree

1 file changed

+46
-27
lines changed

1 file changed

+46
-27
lines changed

pkg/parser/node.go

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (n *Node) validate(ectx EnricherCtx) error {
112112
}
113113
}
114114

115-
for idx:= range n.Stash {
115+
for idx := range n.Stash {
116116
// pointer not value, to avoid throwing away the defaults
117117
stash := &n.Stash[idx]
118118

@@ -337,6 +337,46 @@ func (n *Node) processStash(p *types.Event, cachedExprEnv map[string]any, logger
337337
return nil
338338
}
339339

340+
func (n *Node) processLeaves(
341+
p *types.Event,
342+
ctx UnixParserCtx,
343+
cachedExprEnv map[string]any,
344+
initialState bool,
345+
nodeHasOKGrok bool,
346+
) (bool, error) {
347+
nodeState := initialState
348+
349+
for idx := range n.LeavesNodes {
350+
child := &n.LeavesNodes[idx]
351+
352+
ret, err := child.process(p, ctx, cachedExprEnv)
353+
if err != nil {
354+
n.Logger.Tracef("\tNode (%s) failed: %v", child.rn, err)
355+
n.Logger.Debugf("Event leaving node: ko")
356+
return false, err
357+
}
358+
359+
n.Logger.Tracef("\tsub-node (%s) ret: %v (strategy:%s)", child.rn, ret, n.OnSuccess)
360+
361+
if ret {
362+
nodeState = true
363+
/* if child is successful, stop processing */
364+
if n.OnSuccess == "next_stage" {
365+
n.Logger.Debugf("child is success, OnSuccess=next_stage, skip")
366+
break
367+
}
368+
} else if !nodeHasOKGrok {
369+
/*
370+
If the parent node has a successful grok pattern, its state will stay successful even if one or more childs fail.
371+
If the parent node is a skeleton node (no grok pattern), then at least one child must be successful for it to be a success.
372+
*/
373+
nodeState = false
374+
}
375+
}
376+
377+
return nodeState, nil
378+
}
379+
340380
func (n *Node) process(p *types.Event, ctx UnixParserCtx, expressionEnv map[string]any) (bool, error) {
341381
clog := n.Logger
342382

@@ -374,34 +414,13 @@ func (n *Node) process(p *types.Event, ctx UnixParserCtx, expressionEnv map[stri
374414
}
375415
}
376416

377-
// Iterate on leafs
378-
leaves := n.LeavesNodes
379-
for idx := range leaves {
380-
ret, err := leaves[idx].process(p, ctx, cachedExprEnv)
381-
if err != nil {
382-
clog.Tracef("\tNode (%s) failed : %v", leaves[idx].rn, err)
383-
clog.Debugf("Event leaving node : ko")
384-
385-
return false, err
386-
}
417+
leafState, err := n.processLeaves(p, ctx, cachedExprEnv, nodeState, nodeHasOKGrok)
418+
if err != nil {
419+
return false, err
420+
}
387421

388-
clog.Tracef("\tsub-node (%s) ret : %v (strategy:%s)", leaves[idx].rn, ret, n.OnSuccess)
422+
nodeState = leafState
389423

390-
if ret {
391-
nodeState = true
392-
/* if child is successful, stop processing */
393-
if n.OnSuccess == "next_stage" {
394-
clog.Debugf("child is success, OnSuccess=next_stage, skip")
395-
break
396-
}
397-
} else if !nodeHasOKGrok {
398-
/*
399-
If the parent node has a successful grok pattern, it's state will stay successful even if one or more chil fails.
400-
If the parent node is a skeleton node (no grok pattern), then at least one child must be successful for it to be a success.
401-
*/
402-
nodeState = false
403-
}
404-
}
405424
/*todo : check if a node made the state change ?*/
406425
/* should the childs inherit the on_success behavior */
407426

0 commit comments

Comments
 (0)