Skip to content

Commit 075447c

Browse files
authored
refact pkg/parser: review formatting verbs, logs (#3887)
1 parent 6578f09 commit 075447c

File tree

1 file changed

+49
-49
lines changed

1 file changed

+49
-49
lines changed

pkg/parser/node.go

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ func (n *Node) validate(ectx EnricherCtx) error {
7575

7676
/* "" behaves like continue */
7777
if n.OnSuccess != "continue" && n.OnSuccess != "next_stage" && n.OnSuccess != "" {
78-
return fmt.Errorf("onsuccess '%s' not continue,next_stage", n.OnSuccess)
78+
return fmt.Errorf("onsuccess %q not continue,next_stage", n.OnSuccess)
7979
}
8080

8181
if n.Filter != "" && n.RunTimeFilter == nil {
82-
return fmt.Errorf("non-empty filter '%s' was not compiled", n.Filter)
82+
return fmt.Errorf("non-empty filter %q was not compiled", n.Filter)
8383
}
8484

8585
if n.Grok.RunTimeRegexp != nil || n.Grok.TargetField != "" {
@@ -95,15 +95,15 @@ func (n *Node) validate(ectx EnricherCtx) error {
9595
for idx, static := range n.Statics {
9696
if static.Method != "" {
9797
if static.ExpValue == "" {
98-
return fmt.Errorf("static %d : when method is set, expression must be present", idx)
98+
return fmt.Errorf("static %d: when method is set, expression must be present", idx)
9999
}
100100

101101
if _, ok := ectx.Registered[static.Method]; !ok {
102-
log.Warningf("the method '%s' doesn't exist or the plugin has not been initialized", static.Method)
102+
log.Warningf("the method %q doesn't exist or the plugin has not been initialized", static.Method)
103103
}
104104
} else {
105105
if static.Meta == "" && static.Parsed == "" && static.TargetByName == "" {
106-
return fmt.Errorf("static %d : at least one of meta/event/target must be set", idx)
106+
return fmt.Errorf("static %d: at least one of meta/event/target must be set", idx)
107107
}
108108

109109
if static.Value == "" && static.RunTimeValue == nil {
@@ -117,19 +117,19 @@ func (n *Node) validate(ectx EnricherCtx) error {
117117
stash := &n.Stash[idx]
118118

119119
if stash.Name == "" {
120-
return fmt.Errorf("stash %d : name must be set", idx)
120+
return fmt.Errorf("stash %d: name must be set", idx)
121121
}
122122

123123
if stash.Value == "" {
124-
return fmt.Errorf("stash %s : value expression must be set", stash.Name)
124+
return fmt.Errorf("stash %s: value expression must be set", stash.Name)
125125
}
126126

127127
if stash.Key == "" {
128-
return fmt.Errorf("stash %s : key expression must be set", stash.Name)
128+
return fmt.Errorf("stash %s: key expression must be set", stash.Name)
129129
}
130130

131131
if stash.TTL == "" {
132-
return fmt.Errorf("stash %s : ttl must be set", stash.Name)
132+
return fmt.Errorf("stash %s: ttl must be set", stash.Name)
133133
}
134134

135135
if stash.Strategy == "" {
@@ -147,28 +147,28 @@ func (n *Node) validate(ectx EnricherCtx) error {
147147
func (n *Node) processFilter(cachedExprEnv map[string]any) (bool, error) {
148148
clog := n.Logger
149149
if n.RunTimeFilter == nil {
150-
clog.Tracef("Node has not filter, enter")
150+
clog.Trace("Node has not filter, enter")
151151
return true, nil
152152
}
153153

154154
// Evaluate node's filter
155155
output, err := exprhelpers.Run(n.RunTimeFilter, cachedExprEnv, clog, n.Debug)
156156
if err != nil {
157-
clog.Warningf("failed to run filter : %v", err)
158-
clog.Debugf("Event leaving node : ko")
157+
clog.Warningf("failed to run filter: %v", err)
158+
clog.Debug("Event leaving node: ko")
159159

160160
return false, nil
161161
}
162162

163163
switch out := output.(type) {
164164
case bool:
165165
if !out {
166-
clog.Debugf("Event leaving node : ko (failed filter)")
166+
clog.Debug("Event leaving node: ko (failed filter)")
167167
return false, nil
168168
}
169169
default:
170-
clog.Warningf("Expr '%s' returned non-bool, abort : %T", n.Filter, output)
171-
clog.Debugf("Event leaving node : ko")
170+
clog.Warningf("Expr %q returned non-bool, abort: %T", n.Filter, output)
171+
clog.Debug("Event leaving node: ko")
172172

173173
return false, nil
174174
}
@@ -216,11 +216,11 @@ func (n *Node) processGrok(p *types.Event, cachedExprEnv map[string]any) (bool,
216216
gstr := ""
217217

218218
if n.Grok.RunTimeRegexp == nil {
219-
clog.Tracef("! No grok pattern : %p", n.Grok.RunTimeRegexp)
219+
clog.Tracef("! No grok pattern: %p", n.Grok.RunTimeRegexp)
220220
return true, false, nil
221221
}
222222

223-
clog.Tracef("Processing grok pattern : %s : %p", n.Grok.RegexpName, n.Grok.RunTimeRegexp)
223+
clog.Tracef("Processing grok pattern: %s: %p", n.Grok.RegexpName, n.Grok.RunTimeRegexp)
224224
// for unparsed, parsed etc. set sensible defaults to reduce user hassle
225225
if n.Grok.TargetField != "" {
226226
// it's a hack to avoid using real reflect
@@ -229,13 +229,13 @@ func (n *Node) processGrok(p *types.Event, cachedExprEnv map[string]any) (bool,
229229
} else if val, ok := p.Parsed[n.Grok.TargetField]; ok {
230230
gstr = val
231231
} else {
232-
clog.Debugf("(%s) target field '%s' doesn't exist in %v", n.rn, n.Grok.TargetField, p.Parsed)
232+
clog.Debugf("(%s) target field %q doesn't exist in %v", n.rn, n.Grok.TargetField, p.Parsed)
233233
return false, false, nil
234234
}
235235
} else if n.Grok.RunTimeValue != nil {
236236
output, err := exprhelpers.Run(n.Grok.RunTimeValue, cachedExprEnv, clog, n.Debug)
237237
if err != nil {
238-
clog.Warningf("failed to run RunTimeValue : %v", err)
238+
clog.Warningf("failed to run RunTimeValue: %v", err)
239239
return false, false, nil
240240
}
241241

@@ -247,7 +247,7 @@ func (n *Node) processGrok(p *types.Event, cachedExprEnv map[string]any) (bool,
247247
case float64, float32:
248248
gstr = fmt.Sprintf("%f", out)
249249
default:
250-
clog.Errorf("unexpected return type for RunTimeValue : %T", output)
250+
clog.Errorf("unexpected return type for RunTimeValue: %T", output)
251251
}
252252
}
253253

@@ -262,23 +262,23 @@ func (n *Node) processGrok(p *types.Event, cachedExprEnv map[string]any) (bool,
262262

263263
if len(grok) == 0 {
264264
// grok failed, node failed
265-
clog.Debugf("+ Grok '%s' didn't return data on '%s'", groklabel, gstr)
265+
clog.Debugf("+ Grok %q didn't return data on %q", groklabel, gstr)
266266
return false, false, nil
267267
}
268268

269269
/*tag explicitly that the *current* node had a successful grok pattern. it's important to know success state*/
270270
nodeHasOKGrok = true
271271

272-
clog.Debugf("+ Grok '%s' returned %d entries to merge in Parsed", groklabel, len(grok))
272+
clog.Debugf("+ Grok %q returned %d entries to merge in Parsed", groklabel, len(grok))
273273
// We managed to grok stuff, merged into parse
274274
for k, v := range grok {
275-
clog.Debugf("\t.Parsed['%s'] = '%s'", k, v)
275+
clog.Debugf("\t.Parsed[%q] = %q", k, v)
276276
p.Parsed[k] = v
277277
}
278278
// if the grok succeed, process associated statics
279279
err := n.ProcessStatics(n.Grok.Statics, p)
280280
if err != nil {
281-
clog.Errorf("(%s) Failed to process statics : %v", n.rn, err)
281+
clog.Errorf("(%s) Failed to process statics: %v", n.rn, err)
282282
return false, false, err
283283
}
284284

@@ -304,28 +304,28 @@ func (n *Node) processStash(p *types.Event, cachedExprEnv map[string]any, logger
304304
// collect the data
305305
output, err := exprhelpers.Run(stash.ValueExpression, cachedExprEnv, logger, n.Debug)
306306
if err != nil {
307-
logger.Warningf("Error while running stash val expression : %v", err)
307+
logger.Warningf("Error while running stash val expression: %v", err)
308308
}
309309
// can we expect anything else than a string ?
310310
switch output := output.(type) {
311311
case string:
312312
value = output
313313
default:
314-
logger.Warningf("unexpected type %t (%v) while running '%s'", output, output, stash.Value)
314+
logger.Warningf("unexpected type %T (%v) while running %q", output, output, stash.Value)
315315
continue
316316
}
317317

318318
// collect the key
319319
output, err = exprhelpers.Run(stash.KeyExpression, cachedExprEnv, logger, n.Debug)
320320
if err != nil {
321-
logger.Warningf("Error while running stash key expression : %v", err)
321+
logger.Warningf("Error while running stash key expression: %v", err)
322322
}
323323
// can we expect anything else than a string ?
324324
switch output := output.(type) {
325325
case string:
326326
key = output
327327
default:
328-
logger.Warningf("unexpected type %t (%v) while running '%s'", output, output, stash.Key)
328+
logger.Warningf("unexpected type %T (%v) while running %q", output, output, stash.Key)
329329
continue
330330
}
331331

@@ -342,7 +342,7 @@ func (n *Node) process(p *types.Event, ctx UnixParserCtx, expressionEnv map[stri
342342

343343
cachedExprEnv := expressionEnv
344344

345-
clog.Tracef("Event entering node")
345+
clog.Trace("Event entering node")
346346

347347
nodeState, err := n.processFilter(cachedExprEnv)
348348
if err != nil {
@@ -405,15 +405,15 @@ func (n *Node) process(p *types.Event, ctx UnixParserCtx, expressionEnv map[stri
405405
/*todo : check if a node made the state change ?*/
406406
/* should the childs inherit the on_success behavior */
407407

408-
clog.Tracef("State after nodes : %v", nodeState)
408+
clog.Tracef("State after nodes: %v", nodeState)
409409

410410
// grok or leafs failed, don't process statics
411411
if !nodeState {
412412
if n.Name != "" {
413413
metrics.NodesHitsKo.With(prometheus.Labels{"source": p.Line.Src, "type": p.Line.Module, "name": n.Name, "stage": p.Stage, "acquis_type": p.Line.Labels["type"]}).Inc()
414414
}
415415

416-
clog.Debugf("Event leaving node : ko")
416+
clog.Debug("Event leaving node: ko")
417417

418418
return nodeState, nil
419419
}
@@ -431,34 +431,34 @@ func (n *Node) process(p *types.Event, ctx UnixParserCtx, expressionEnv map[stri
431431
// if all else is good in whitelist, process node's statics
432432
err := n.ProcessStatics(n.Statics, p)
433433
if err != nil {
434-
clog.Errorf("Failed to process statics : %v", err)
434+
clog.Errorf("Failed to process statics: %v", err)
435435
return false, err
436436
}
437437
} else {
438-
clog.Tracef("! No node statics")
438+
clog.Trace("! No node statics")
439439
}
440440

441441
if nodeState {
442-
clog.Debugf("Event leaving node : ok")
443-
log.Tracef("node is successful, check strategy")
442+
clog.Debug("Event leaving node : ok")
443+
log.Trace("node is successful, check strategy")
444444

445445
if n.OnSuccess == "next_stage" {
446446
idx := stageidx(p.Stage, ctx.Stages)
447447
// we're at the last stage
448448
if idx+1 == len(ctx.Stages) {
449-
clog.Debugf("node reached the last stage : %s", p.Stage)
449+
clog.Debugf("node reached the last stage: %s", p.Stage)
450450
} else {
451451
clog.Debugf("move Event from stage %s to %s", p.Stage, ctx.Stages[idx+1])
452452
p.Stage = ctx.Stages[idx+1]
453453
}
454454
} else {
455-
clog.Tracef("no strategy on success (%s), continue !", n.OnSuccess)
455+
clog.Tracef("no strategy on success (%s), continue!", n.OnSuccess)
456456
}
457457
} else {
458-
clog.Debugf("Event leaving node : ko")
458+
clog.Debug("Event leaving node: ko")
459459
}
460460

461-
clog.Tracef("Node successful, continue")
461+
clog.Trace("Node successful, continue")
462462

463463
return nodeState, nil
464464
}
@@ -491,64 +491,64 @@ func (n *Node) compile(pctx *UnixParserCtx, ectx EnricherCtx) error {
491491
/* display info about top-level nodes, they should be the only one with explicit stage name ?*/
492492
n.Logger = n.Logger.WithFields(log.Fields{"stage": n.Stage, "name": n.Name})
493493

494-
n.Logger.Tracef("Compiling : %s", dumpr.Sdump(n))
494+
n.Logger.Tracef("Compiling: %s", dumpr.Sdump(n))
495495

496496
// compile filter if present
497497
if n.Filter != "" {
498498
n.RunTimeFilter, err = expr.Compile(n.Filter, exprhelpers.GetExprOptions(map[string]any{"evt": &types.Event{}})...)
499499
if err != nil {
500-
return fmt.Errorf("compilation of '%s' failed: %v", n.Filter, err)
500+
return fmt.Errorf("compilation of %q failed: %v", n.Filter, err)
501501
}
502502
}
503503

504504
/* handle pattern_syntax and groks */
505505
for _, pattern := range n.SubGroks {
506-
n.Logger.Tracef("Adding subpattern '%s' : '%s'", pattern.Key, pattern.Value)
506+
n.Logger.Tracef("Adding subpattern '%s': '%s'", pattern.Key, pattern.Value)
507507

508508
if err = pctx.Grok.Add(pattern.Key.(string), pattern.Value.(string)); err != nil {
509509
if errors.Is(err, grokky.ErrAlreadyExist) {
510510
n.Logger.Warningf("grok '%s' already registred", pattern.Key)
511511
continue
512512
}
513513

514-
n.Logger.Errorf("Unable to compile subpattern %s : %v", pattern.Key, err)
514+
n.Logger.Errorf("Unable to compile subpattern %s: %v", pattern.Key, err)
515515

516516
return err
517517
}
518518
}
519519

520520
/* load grok by name or compile in-place */
521521
if n.Grok.RegexpName != "" {
522-
n.Logger.Tracef("+ Regexp Compilation '%s'", n.Grok.RegexpName)
522+
n.Logger.Tracef("+ Regexp Compilation %q", n.Grok.RegexpName)
523523

524524
n.Grok.RunTimeRegexp, err = pctx.Grok.Get(n.Grok.RegexpName)
525525
if err != nil {
526-
return fmt.Errorf("unable to find grok '%s': %v", n.Grok.RegexpName, err)
526+
return fmt.Errorf("unable to find grok %q: %v", n.Grok.RegexpName, err)
527527
}
528528

529529
if n.Grok.RunTimeRegexp == nil {
530-
return fmt.Errorf("empty grok '%s'", n.Grok.RegexpName)
530+
return fmt.Errorf("empty grok %q", n.Grok.RegexpName)
531531
}
532532

533533
n.Logger.Tracef("%s regexp: %s", n.Grok.RegexpName, n.Grok.RunTimeRegexp.String())
534534

535535
valid = true
536536
} else if n.Grok.RegexpValue != "" {
537537
if strings.HasSuffix(n.Grok.RegexpValue, "\n") {
538-
n.Logger.Debugf("Beware, pattern ends with \\n : '%s'", n.Grok.RegexpValue)
538+
n.Logger.Debugf("Beware, pattern ends with \\n: %q", n.Grok.RegexpValue)
539539
}
540540

541541
n.Grok.RunTimeRegexp, err = pctx.Grok.Compile(n.Grok.RegexpValue)
542542
if err != nil {
543-
return fmt.Errorf("failed to compile grok '%s': %v", n.Grok.RegexpValue, err)
543+
return fmt.Errorf("failed to compile grok %q: %v", n.Grok.RegexpValue, err)
544544
}
545545

546546
if n.Grok.RunTimeRegexp == nil {
547547
// We shouldn't be here because compilation succeeded, so regexp shouldn't be nil
548548
return fmt.Errorf("grok compilation failure: %s", n.Grok.RegexpValue)
549549
}
550550

551-
n.Logger.Tracef("%s regexp : %s", n.Grok.RegexpValue, n.Grok.RunTimeRegexp.String())
551+
n.Logger.Tracef("%s regexp: %s", n.Grok.RegexpValue, n.Grok.RunTimeRegexp.String())
552552

553553
valid = true
554554
}

0 commit comments

Comments
 (0)