Skip to content

Commit

Permalink
noop alert and noop enrichment working fully
Browse files Browse the repository at this point in the history
  • Loading branch information
83bytes committed Jun 30, 2024
1 parent 13070e8 commit 519f315
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 19 deletions.
6 changes: 4 additions & 2 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ func GetActionMap() *ActionLut {
return &actionMap
}

type ActionLut map[string]func(types.Action, map[string]interface{}) error
type ActionFunc func(types.Alert, types.Action, map[string]interface{}) error

func (flut ActionLut) Add(fname string, f func(types.Action, map[string]interface{}) error) {
type ActionLut map[string]ActionFunc

func (flut ActionLut) Add(fname string, f ActionFunc) {
flut[fname] = f
}
9 changes: 4 additions & 5 deletions action/noopAction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import (
"fmt"
)

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

rs := fmt.Sprintf("noop action called \nalert: %s \naction: %s \n", alert.AlertName, action.ActionName)

for k, v := range resultMap {
if s, ok := v.(string); ok {
rs += fmt.Sprintf("result of %s enrichment; is -> %s \n", k, s)
rs += fmt.Sprintf("result of %s enrichment(s): %s \n", k, s)
}

}

logr.Debug(rs)
Expand Down
16 changes: 12 additions & 4 deletions alert/processAlert.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@ func LoadAlertFromPayload(a *types.Alert) error {
// If an alert-pipeline is configured for given alert
// this function executes the enrichments one-by-one
// then it executes the actions one-by-one
// The body of the Action is passed to all the enrichment and action
// The body of the Alert is passed to all the enrichment and action
// so that they have the complete context regarding what is going on
// The actions additionally will also contain the output of all the enrichments

// todo:
// actions and enrichments are not able to pick "specific information" from the Alert.
// want to add the ability to pick x.y.z field from the alert json.
// this will further power things like "lookup ip from alert and grab x,y,z metrics from grafana"

// todo:
// process each enrichment concurrently using goroutines and channels etc

func ProcessAlert(a types.Alert) {
logr := logging.GetLogger()
an := a.GetAlertName()
Expand All @@ -50,7 +59,7 @@ func ProcessAlert(a types.Alert) {
logr.Info("processing enrichment : ", v.EnrichmentName)

if f, ok := (*enrichmentMap)[v.EnrichmentName]; ok {
resMap[v.EnrichmentName], err = f(v)
resMap[v.EnrichmentName], err = f(a, v)
if err != nil {
fmt.Println(err)
}
Expand All @@ -62,11 +71,10 @@ func ProcessAlert(a types.Alert) {
logr.Info("processing action : ", v.ActionName)

if f, ok := (*actionMap)[v.ActionName]; ok {
err := f(v, resMap)
err := f(a, v, resMap)
if err != nil {
fmt.Println(err)
}
}
}

}
8 changes: 5 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ func GetAmConfig() *types.AlertManagerConfig {
func ValidateAndLoad(b []byte) (*types.AlertManagerConfig, error) {
amConfig := GetAmConfig()

// todo protect this by a Mutex
// todo: protect this by a Mutex
// a write mutex is enough
// todo
// we will need this once we start hotloading of the config.
// for now this is fine
// todo:
// try to use a strict unmarshalling like in json
err := yaml.Unmarshal(b, &amConfig)
if err != nil {
Expand All @@ -36,7 +38,7 @@ func ValidateAndLoad(b []byte) (*types.AlertManagerConfig, error) {
// do better validation
// right now it accepts a stray key in the list of alert_pipelines
// and ingects an empty alert-config
// Filter out the empty entr for now.
// filter out the empty entry for now.
// maybe check if json-schema etc can help here
return amConfig, nil
}
5 changes: 3 additions & 2 deletions enrichment/enrichment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ func GetEnrichmentMap() *EnrichmentLut {
return &enrichmentMap
}

type EnrichmentLut map[string]func(types.Enrichment) (interface{}, error)
type EnrichmentFunc func(types.Alert, types.Enrichment) (interface{}, error)
type EnrichmentLut map[string]EnrichmentFunc

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

func NoopEnrichment(e types.Enrichment) (interface{}, error) {
func NoopEnrichment(alert types.Alert, e types.Enrichment) (interface{}, error) {
logr := logging.GetLogger()

rs := fmt.Sprint("noop enrichment called with : ", e.EnrichmentArgs)
logr.Info(rs)
rs := fmt.Sprintf("noop enrichment called: \nalert: %s\nenrichment: %s\nwith args: %s", alert.AlertName, e.EnrichmentName, e.EnrichmentArgs)

logr.Debug(rs)

return rs, nil
}

0 comments on commit 519f315

Please sign in to comment.