From 1903720e1b1e1b56e8bedb6399d061278a9bd86c Mon Sep 17 00:00:00 2001 From: Thomas Labarussias Date: Wed, 4 Dec 2024 16:32:19 +0100 Subject: [PATCH] fix missing metrics for notifications Signed-off-by: Thomas Labarussias --- CHANGELOG.md | 4 ++ actionners/actionners.go | 116 +++++++++++++++++++-------------------- notifiers/notifiers.go | 2 +- rules.yaml | 47 ++++++++-------- 4 files changed, 87 insertions(+), 82 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1593491f..df60c2b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.2.1 - 2024-12-09 + +- Fix missing metrics for the `notifications` + ## 0.2.0 - 2024-11-26 - Add `gcp:function` actionner diff --git a/actionners/actionners.go b/actionners/actionners.go index ef1cf936..05757eb7 100644 --- a/actionners/actionners.go +++ b/actionners/actionners.go @@ -269,7 +269,7 @@ func runAction(mctx context.Context, rule *rules.Rule, action *rules.Action, eve if actionner.Information().RequireOutput { octx, span := tracer.Start(actx, "output") - log = utils.LogLine{ + logO := utils.LogLine{ Message: "output", Action: action.GetName(), TraceID: event.TraceID, @@ -277,27 +277,27 @@ func runAction(mctx context.Context, rule *rules.Rule, action *rules.Action, eve if output == nil { err = fmt.Errorf("an output is required") - log.Status = utils.FailureStr - log.Error = err.Error() - log.OutputTarget = "n/a" - utils.PrintLog("error", log) - metrics.IncreaseCounter(log) + logO.Status = utils.FailureStr + logO.Error = err.Error() + logO.OutputTarget = "n/a" + utils.PrintLog("error", logO) + metrics.IncreaseCounter(logO) span.SetStatus(codes.Error, err.Error()) span.RecordError(err) - go notifiers.Notify(octx, rule, action, event, log) + go notifiers.Notify(octx, rule, action, event, logO) span.End() return err } if data == nil || len(data.Bytes) == 0 { err = fmt.Errorf("empty output") - log.Status = utils.FailureStr - log.Error = err.Error() - utils.PrintLog("error", log) - metrics.IncreaseCounter(log) + logO.Status = utils.FailureStr + logO.Error = err.Error() + utils.PrintLog("error", logO) + metrics.IncreaseCounter(logO) span.SetStatus(codes.Error, err.Error()) span.RecordError(err) - go notifiers.Notify(octx, rule, action, event, log) + go notifiers.Notify(octx, rule, action, event, logO) span.End() return err } @@ -306,67 +306,67 @@ func runAction(mctx context.Context, rule *rules.Rule, action *rules.Action, eve o := outputs.ListDefaultOutputs().FindOutput(target) if o == nil { err = fmt.Errorf("unknown output target '%v'", target) - log.Status = utils.FailureStr - log.OutputTarget = target - log.Error = err.Error() - utils.PrintLog("error", log) - metrics.IncreaseCounter(log) + logO.Status = utils.FailureStr + logO.OutputTarget = target + logO.Error = err.Error() + utils.PrintLog("error", logO) + metrics.IncreaseCounter(logO) span.SetAttributes(attribute.String("output.target", target)) span.SetStatus(codes.Error, err.Error()) span.RecordError(err) - go notifiers.Notify(octx, rule, action, event, log) + go notifiers.Notify(octx, rule, action, event, logO) span.End() return err } - log.Category = o.Information().Category - log.OutputTarget = target + logO.Category = o.Information().Category + logO.OutputTarget = target span.SetAttributes(attribute.String("output.name", o.Information().Name)) span.SetAttributes(attribute.String("output.category", o.Information().Category)) span.SetAttributes(attribute.String("output.target", target)) if err2 := o.Checks(output); err2 != nil { - log.Status = utils.FailureStr - log.Error = err2.Error() - utils.PrintLog("error", log) - metrics.IncreaseCounter(log) + logO.Status = utils.FailureStr + logO.Error = err2.Error() + utils.PrintLog("error", logO) + metrics.IncreaseCounter(logO) span.SetStatus(codes.Error, err2.Error()) span.RecordError(err2) - go notifiers.Notify(octx, rule, action, event, log) + go notifiers.Notify(octx, rule, action, event, logO) span.End() return err } result, err = o.Run(output, data) - log.Status = result.Status - log.Objects = result.Objects + logO.Status = result.Status + logO.Objects = result.Objects if result.Output != "" { - log.Output = result.Output + logO.Output = result.Output } if result.Error != "" { - log.Error = result.Error + logO.Error = result.Error } span.SetAttributes(attribute.String("output.status", result.Status)) span.SetAttributes(attribute.String("output.message", result.Output)) - metrics.IncreaseCounter(log) + metrics.IncreaseCounter(logO) if err != nil { - log.Error = err.Error() - utils.PrintLog("error", log) + logO.Error = err.Error() + utils.PrintLog("error", logO) span.SetStatus(codes.Error, err.Error()) span.RecordError(err) - go notifiers.Notify(octx, rule, action, event, log) + go notifiers.Notify(octx, rule, action, event, logO) span.End() return err } span.SetStatus(codes.Ok, "output successfully completed") span.AddEvent(result.Output) - utils.PrintLog("info", log) - go notifiers.Notify(octx, rule, action, event, log) + utils.PrintLog("info", logO) + go notifiers.Notify(octx, rule, action, event, logO) span.End() return nil } @@ -374,7 +374,7 @@ func runAction(mctx context.Context, rule *rules.Rule, action *rules.Action, eve if actionner.Information().AllowOutput && output != nil && data != nil { octx, span := tracer.Start(actx, "output") - log = utils.LogLine{ + logO := utils.LogLine{ Message: "output", Rule: rule.GetName(), Action: action.GetName(), @@ -385,20 +385,20 @@ func runAction(mctx context.Context, rule *rules.Rule, action *rules.Action, eve o := outputs.GetOutputs().FindOutput(target) if o == nil { err = fmt.Errorf("unknown target '%v'", target) - log.OutputTarget = target - log.Status = utils.FailureStr - log.Error = err.Error() - utils.PrintLog("error", log) + logO.OutputTarget = target + logO.Status = utils.FailureStr + logO.Error = err.Error() + utils.PrintLog("error", logO) span.SetAttributes(attribute.String("output.target", target)) span.SetStatus(codes.Error, err.Error()) span.RecordError(err) - go notifiers.Notify(octx, rule, action, event, log) + go notifiers.Notify(octx, rule, action, event, logO) span.End() return err } - log.OutputTarget = target - log.Category = o.Information().Category + logO.OutputTarget = target + logO.Category = o.Information().Category span.SetAttributes(attribute.String("output.name", o.Information().Name)) span.SetAttributes(attribute.String("output.category", o.Information().Category)) @@ -406,46 +406,46 @@ func runAction(mctx context.Context, rule *rules.Rule, action *rules.Action, eve if len(data.Bytes) == 0 { err = fmt.Errorf("empty output") - log.Status = utils.FailureStr - log.Error = err.Error() - utils.PrintLog("error", log) - metrics.IncreaseCounter(log) + logO.Status = utils.FailureStr + logO.Error = err.Error() + utils.PrintLog("error", logO) + metrics.IncreaseCounter(logO) span.SetStatus(codes.Error, err.Error()) span.RecordError(err) - go notifiers.Notify(octx, rule, action, event, log) + go notifiers.Notify(octx, rule, action, event, logO) span.End() return err } result, err = o.Run(output, data) - log.Status = result.Status - log.Objects = result.Objects + logO.Status = result.Status + logO.Objects = result.Objects if result.Output != "" { - log.Output = result.Output + logO.Output = result.Output } if result.Error != "" { - log.Error = result.Error + logO.Error = result.Error } span.SetAttributes(attribute.String("output.status", result.Status)) span.SetAttributes(attribute.String("output.message", result.Output)) - metrics.IncreaseCounter(log) + metrics.IncreaseCounter(logO) if err != nil { - log.Error = err.Error() - utils.PrintLog("error", log) + logO.Error = err.Error() + utils.PrintLog("error", logO) span.SetStatus(codes.Error, err.Error()) span.RecordError(err) - go notifiers.Notify(octx, rule, action, event, log) + go notifiers.Notify(octx, rule, action, event, logO) span.End() return err } span.SetStatus(codes.Ok, "output successfully completed") span.AddEvent(result.Output) - utils.PrintLog("info", log) - go notifiers.Notify(octx, rule, action, event, log) + utils.PrintLog("info", logO) + go notifiers.Notify(octx, rule, action, event, logO) span.End() return nil } diff --git a/notifiers/notifiers.go b/notifiers/notifiers.go index 2d9cdf1e..460e9dc0 100644 --- a/notifiers/notifiers.go +++ b/notifiers/notifiers.go @@ -163,7 +163,7 @@ func Notify(actx context.Context, rule *rules.Rule, action *rules.Action, event span.SetStatus(codes.Ok, "notification successfully sent") logN.Status = utils.SuccessStr utils.PrintLog("info", logN) - metrics.IncreaseCounter(log) + metrics.IncreaseCounter(logN) } span.End() } diff --git a/rules.yaml b/rules.yaml index 12637290..dd962063 100644 --- a/rules.yaml +++ b/rules.yaml @@ -63,19 +63,19 @@ actions: - action: Terminate Pod -- rule: Test invoke lambda - match: - rules: - - Test invoke lambda - actions: - - action: Invoke Lambda function - -- rule: Test invoke GCP function - match: - rules: - - Test invoke GCP function - actions: - - action: Invoke GCP function +# - rule: Test invoke lambda +# match: +# rules: +# - Test invoke lambda +# actions: +# - action: Invoke Lambda function + +# - rule: Test invoke GCP function +# match: +# rules: +# - Test invoke GCP function +# actions: +# - action: Invoke GCP function - rule: Delete unknown namespace match: @@ -160,7 +160,8 @@ additional_contexts: - k8snode parameters: - command: echo "${NODE_HOSTNAME}" + shell: /bin/sh + command: uname -a - action: Test log actionner: kubernetes:log output: @@ -168,15 +169,15 @@ parameters: bucket: falcosidekick-tests prefix: logs/ -# - action: Test download -# actionner: kubernetes:download -# parameters: -# file: "${FD_NAME}" -# output: -# target: minio:s3 -# parameters: -# bucket: falco-talon -# prefix: /files/ + - action: Test download + actionner: kubernetes:download + parameters: + file: "${FD_NAME}" + output: + target: minio:s3 + parameters: + bucket: falco-talon + prefix: /files/ - rule: Test tcpdump match: