Skip to content

Commit

Permalink
use diffiernt LUT for actions and enrichments
Browse files Browse the repository at this point in the history
support different fuction args
pass enrichment resutls to actions
  • Loading branch information
83bytes committed Jun 29, 2024
1 parent 9985672 commit 591774f
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 43 deletions.
6 changes: 2 additions & 4 deletions action/action.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package action

import "alertmanager/utils"

type Action struct {
ActionName string `yaml:"action_name"`
ActionArgs string `yaml:"action_args"`
Expand All @@ -11,8 +9,8 @@ func GetDefaultAction() Action {
return Action{ActionName: "NOOP_ACTION", ActionArgs: "ARG1,ARG2"}
}

var actionMap = make(utils.FunctionLut)
var actionMap = make(ActionLut)

func GetActionMap() *utils.FunctionLut {
func GetActionMap() *ActionLut {
return &actionMap
}
7 changes: 7 additions & 0 deletions action/actionLUT.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package action

type ActionLut map[string]func(Action, map[string]interface{}) error

func (flut ActionLut) Add(fname string, f func(Action, map[string]interface{}) error) {
flut[fname] = f
}
16 changes: 12 additions & 4 deletions action/noopAction.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ import (
"fmt"
)

func NoopAction(args string) (string, error) {
func NoopAction(a Action, resultMap map[string]interface{}) error {
logr := logging.GetLogger()
logr.Debug("noop action called")
rs := fmt.Sprintf("noop action called \n")

rs := fmt.Sprint("Noop Action Called; with output of enrichment ", args)
logr.Info(rs)
return rs, nil
for k, v := range resultMap {
if s, ok := v.(string); ok {
rs += fmt.Sprintf("result of %s enrichment; is -> %s \n", k, s)
}

}

logr.Debug(rs)
return nil
}
16 changes: 7 additions & 9 deletions alert/processAlert.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,44 @@ func ProcessAlert(a Alert) {
logr := logging.GetLogger()
an := a.GetAlertName()

logr.Debug("alert processor", an)
logr.Info("alert being processed : ", an)

amc := config.GetAmConfig()
p := amc.GetPipelineForAlert(an)
if p == nil {
logr.Infof("no alert-pipeline configured for %s", an)
return
}
logr.Debug("alert pipeline name", (*p))
logr.Debug("alert pipeline found for ", (*p))

enrichmentMap := enrichment.GetEnrichmentMap()
actionMap := action.GetActionMap()

// these are used to capture the result of the enrichments
enrichmentRes := ""
var err error
resMap := make(map[string]interface{}, len(p.Enrichments))

// process enrichments
for _, v := range (*p).Enrichments {
logr.Info("processing enrichment : ", v.EnrichmentName)

if f, ok := (*enrichmentMap)[v.EnrichmentName]; ok {
enrichmentRes, err = f(v.EnrichmentArgs)
resMap[v.EnrichmentName], err = f(v)
if err != nil {
fmt.Println(err)
}
fmt.Print("********** ", enrichmentRes, "*****")
}
}

fmt.Print("ENRICHMENT RESP ************* ", enrichmentRes)

// process actions
for _, v := range (*p).Actions {
logr.Info("processing action : ", v.ActionName)

if f, ok := (*actionMap)[v.ActionName]; ok {
x, err := f(v.ActionArgs + enrichmentRes)
err := f(v, resMap)
if err != nil {
fmt.Println(err)
}
fmt.Print("################## ", x)
}
}

Expand Down
11 changes: 4 additions & 7 deletions enrichment/enrichment.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package enrichment

import "alertmanager/utils"

type Enrichment struct {
EnrichmentName string `yaml:"enrichment_name"`
EnrichmentArgs string `yaml:"enrichment_args"`
EnrichmentResult string `yaml:"-"`
EnrichmentName string `yaml:"enrichment_name"`
EnrichmentArgs string `yaml:"enrichment_args"`
}

func GetDefaultEnrichment() Enrichment {
return Enrichment{EnrichmentName: "NOOP_ENRICHMENT", EnrichmentArgs: "ARG1,ARG2"}
}

var enrichmentMap = make(utils.FunctionLut)
var enrichmentMap = make(EnrichmentLut)

func GetEnrichmentMap() *utils.FunctionLut {
func GetEnrichmentMap() *EnrichmentLut {
return &enrichmentMap
}
7 changes: 7 additions & 0 deletions enrichment/enrichmentLUT.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package enrichment

type EnrichmentLut map[string]func(Enrichment) (interface{}, error)

func (flut EnrichmentLut) Add(fname string, f func(Enrichment) (interface{}, error)) {
flut[fname] = f
}
4 changes: 2 additions & 2 deletions enrichment/noopEnrichment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"
)

func NoopEnrichment(args string) (string, error) {
func NoopEnrichment(e Enrichment) (interface{}, error) {
logr := logging.GetLogger()

rs := fmt.Sprint("noop enrichment called with : ", args)
rs := fmt.Sprint("noop enrichment called with : ", e.EnrichmentArgs)
logr.Info(rs)

return rs, nil
Expand Down
17 changes: 0 additions & 17 deletions utils/fucntionLut.go

This file was deleted.

0 comments on commit 591774f

Please sign in to comment.