@@ -57,9 +57,10 @@ type Node struct {
5757 SubGroks yaml.MapSlice `yaml:"pattern_syntax,omitempty"`
5858
5959 // Holds a grok pattern
60- Grok GrokPattern `yaml:"grok,omitempty"`
60+ Grok GrokPattern `yaml:"grok,omitempty"`
61+ RuntimeGrok RuntimeGrokPattern `yaml:"-"`
6162 // Statics can be present in any type of node and is executed last
62- Statics []Static `yaml:"statics,omitempty"`
63+ Statics []Static `yaml:"statics,omitempty"`
6364 RuntimeStatics []RuntimeStatic `yaml:"-"`
6465 // Stash allows to capture data from the log line and store it in an accessible cache
6566 Stash []DataCapture `yaml:"stash,omitempty"`
@@ -83,7 +84,7 @@ func (n *Node) validate(ectx EnricherCtx) error {
8384 return fmt .Errorf ("non-empty filter %q was not compiled" , n .Filter )
8485 }
8586
86- if n .Grok .RunTimeRegexp != nil || n .Grok .TargetField != "" {
87+ if n .RuntimeGrok .RunTimeRegexp != nil || n .Grok .TargetField != "" {
8788 if n .Grok .TargetField == "" && n .Grok .ExpValue == "" {
8889 return errors .New ("grok requires 'expression' or 'apply_on'" )
8990 }
@@ -202,12 +203,12 @@ func (n *Node) processGrok(p *types.Event, cachedExprEnv map[string]any) (bool,
202203 clog := n .Logger
203204 gstr := ""
204205
205- if n .Grok .RunTimeRegexp == nil {
206- clog .Tracef ("! No grok pattern: %p" , n .Grok .RunTimeRegexp )
206+ if n .RuntimeGrok .RunTimeRegexp == nil {
207+ clog .Tracef ("! No grok pattern: %p" , n .RuntimeGrok .RunTimeRegexp )
207208 return true , false , nil
208209 }
209210
210- clog .Tracef ("Processing grok pattern: %s: %p" , n .Grok .RegexpName , n .Grok .RunTimeRegexp )
211+ clog .Tracef ("Processing grok pattern: %s: %p" , n .Grok .RegexpName , n .RuntimeGrok .RunTimeRegexp )
211212 // for unparsed, parsed etc. set sensible defaults to reduce user hassle
212213 if n .Grok .TargetField != "" {
213214 // it's a hack to avoid using real reflect
@@ -219,8 +220,8 @@ func (n *Node) processGrok(p *types.Event, cachedExprEnv map[string]any) (bool,
219220 clog .Debugf ("(%s) target field %q doesn't exist in %v" , n .rn , n .Grok .TargetField , p .Parsed )
220221 return false , false , nil
221222 }
222- } else if n .Grok .RunTimeValue != nil {
223- output , err := exprhelpers .Run (n .Grok .RunTimeValue , cachedExprEnv , clog , n .Debug )
223+ } else if n .RuntimeGrok .RunTimeValue != nil {
224+ output , err := exprhelpers .Run (n .RuntimeGrok .RunTimeValue , cachedExprEnv , clog , n .Debug )
224225 if err != nil {
225226 clog .Warningf ("failed to run RunTimeValue: %v" , err )
226227 return false , false , nil
@@ -245,7 +246,7 @@ func (n *Node) processGrok(p *types.Event, cachedExprEnv map[string]any) (bool,
245246 groklabel = n .Grok .RegexpName
246247 }
247248
248- grok := n .Grok .RunTimeRegexp .Parse (gstr )
249+ grok := n .RuntimeGrok .RunTimeRegexp .Parse (gstr )
249250
250251 if len (grok ) == 0 {
251252 // grok failed, node failed
@@ -263,7 +264,7 @@ func (n *Node) processGrok(p *types.Event, cachedExprEnv map[string]any) (bool,
263264 p .Parsed [k ] = v
264265 }
265266 // if the grok succeed, process associated statics
266- err := n .ProcessStatics (n . Grok . RuntimeStatics , p )
267+ err := n .RuntimeGrok . ProcessStatics (p , n . EnrichFunctions , clog , n . Debug )
267268 if err != nil {
268269 clog .Errorf ("(%s) Failed to process statics: %v" , n .rn , err )
269270 return false , false , err
@@ -340,6 +341,7 @@ func (n *Node) processLeaves(
340341 if err != nil {
341342 n .Logger .Tracef ("\t Node (%s) failed: %v" , child .rn , err )
342343 n .Logger .Debugf ("Event leaving node: ko" )
344+
343345 return false , err
344346 }
345347
@@ -395,7 +397,7 @@ func (n *Node) process(p *types.Event, ctx UnixParserCtx, expressionEnv map[stri
395397 }
396398
397399 // Process the stash (data collection) if: a grok was present and succeeded, or if there is no grok
398- if nodeHasOKGrok || n .Grok .RunTimeRegexp == nil {
400+ if nodeHasOKGrok || n .RuntimeGrok .RunTimeRegexp == nil {
399401 if err := n .processStash (p , cachedExprEnv , clog ); err != nil {
400402 return false , err
401403 }
@@ -435,7 +437,7 @@ func (n *Node) process(p *types.Event, ctx UnixParserCtx, expressionEnv map[stri
435437 if len (n .Statics ) > 0 && (isWhitelisted || ! n .ContainsWLs ()) {
436438 clog .Debugf ("+ Processing %d statics" , len (n .Statics ))
437439 // if all else is good in whitelist, process node's statics
438- err := n .ProcessStatics (n . RuntimeStatics , p )
440+ err := n .ProcessStatics (p )
439441 if err != nil {
440442 clog .Errorf ("Failed to process statics: %v" , err )
441443 return false , err
@@ -523,61 +525,13 @@ func (n *Node) compile(pctx *UnixParserCtx, ectx EnricherCtx) error {
523525 }
524526 }
525527
526- /* load grok by name or compile in-place */
527- if n .Grok .RegexpName != "" {
528- n .Logger .Tracef ("+ Regexp Compilation %q" , n .Grok .RegexpName )
529-
530- n .Grok .RunTimeRegexp , err = pctx .Grok .Get (n .Grok .RegexpName )
531- if err != nil {
532- return fmt .Errorf ("unable to find grok %q: %v" , n .Grok .RegexpName , err )
533- }
534-
535- if n .Grok .RunTimeRegexp == nil {
536- return fmt .Errorf ("empty grok %q" , n .Grok .RegexpName )
537- }
538-
539- n .Logger .Tracef ("%s regexp: %s" , n .Grok .RegexpName , n .Grok .RunTimeRegexp .String ())
540-
541- valid = true
542- } else if n .Grok .RegexpValue != "" {
543- if strings .HasSuffix (n .Grok .RegexpValue , "\n " ) {
544- n .Logger .Debugf ("Beware, pattern ends with \\ n: %q" , n .Grok .RegexpValue )
545- }
546-
547- n .Grok .RunTimeRegexp , err = pctx .Grok .Compile (n .Grok .RegexpValue )
548- if err != nil {
549- return fmt .Errorf ("failed to compile grok %q: %v" , n .Grok .RegexpValue , err )
550- }
551-
552- if n .Grok .RunTimeRegexp == nil {
553- // We shouldn't be here because compilation succeeded, so regexp shouldn't be nil
554- return fmt .Errorf ("grok compilation failure: %s" , n .Grok .RegexpValue )
555- }
556-
557- n .Logger .Tracef ("%s regexp: %s" , n .Grok .RegexpValue , n .Grok .RunTimeRegexp .String ())
558-
559- valid = true
560- }
561-
562- // if grok source is an expression
563- if n .Grok .ExpValue != "" {
564- n .Grok .RunTimeValue , err = expr .Compile (n .Grok .ExpValue ,
565- exprhelpers .GetExprOptions (map [string ]any {"evt" : & types.Event {}})... )
566- if err != nil {
567- return fmt .Errorf ("while compiling grok's expression: %w" , err )
568- }
569- }
570-
571- /* load grok statics */
572- // compile expr statics if present
573- for _ , static := range n .Grok .Statics {
574- compiled , err := static .Compile ()
528+ if n .Grok .RegexpName != "" || n .Grok .RegexpValue != "" || n .Grok .ExpValue != "" {
529+ rg , err := n .Grok .Compile (pctx , n .Logger )
575530 if err != nil {
576531 return err
577532 }
578533
579- n .Grok .RuntimeStatics = append (n .Grok .RuntimeStatics , * compiled )
580-
534+ n .RuntimeGrok = * rg
581535 valid = true
582536 }
583537
0 commit comments