Skip to content

Commit

Permalink
fix missing metrics for notifications
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Labarussias <[email protected]>
  • Loading branch information
Issif authored and poiana committed Dec 9, 2024
1 parent 0efb770 commit 1903720
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 82 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
116 changes: 58 additions & 58 deletions actionners/actionners.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,35 +269,35 @@ 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,
}

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
}
Expand All @@ -306,75 +306,75 @@ 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
}

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(),
Expand All @@ -385,67 +385,67 @@ 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))
span.SetAttributes(attribute.String("output.target", target))

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
}
Expand Down
2 changes: 1 addition & 1 deletion notifiers/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
47 changes: 24 additions & 23 deletions rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -160,23 +160,24 @@
additional_contexts:
- k8snode
parameters:
command: echo "${NODE_HOSTNAME}"
shell: /bin/sh
command: uname -a
- action: Test log
actionner: kubernetes:log
output:
target: aws:s3
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:
Expand Down

0 comments on commit 1903720

Please sign in to comment.