From 519f31543801cc8d5853ded2192c96f2050712da Mon Sep 17 00:00:00 2001 From: Sohom Bhattacharjee Date: Sun, 30 Jun 2024 18:43:38 +0530 Subject: [PATCH] noop alert and noop enrichment working fully --- action/action.go | 6 ++++-- action/noopAction.go | 9 ++++----- alert/processAlert.go | 16 ++++++++++++---- config/config.go | 8 +++++--- enrichment/enrichment.go | 5 +++-- enrichment/noopEnrichment.go | 7 ++++--- 6 files changed, 32 insertions(+), 19 deletions(-) diff --git a/action/action.go b/action/action.go index 26177f5..8beaff3 100644 --- a/action/action.go +++ b/action/action.go @@ -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 } diff --git a/action/noopAction.go b/action/noopAction.go index 3d350e4..afc1383 100644 --- a/action/noopAction.go +++ b/action/noopAction.go @@ -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) diff --git a/alert/processAlert.go b/alert/processAlert.go index 962ae85..32de604 100644 --- a/alert/processAlert.go +++ b/alert/processAlert.go @@ -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() @@ -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) } @@ -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) } } } - } diff --git a/config/config.go b/config/config.go index e0136a9..369b8cc 100644 --- a/config/config.go +++ b/config/config.go @@ -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 { @@ -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 } diff --git a/enrichment/enrichment.go b/enrichment/enrichment.go index 9b23c5e..4e51e86 100644 --- a/enrichment/enrichment.go +++ b/enrichment/enrichment.go @@ -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 } diff --git a/enrichment/noopEnrichment.go b/enrichment/noopEnrichment.go index a04a56d..6311a8c 100644 --- a/enrichment/noopEnrichment.go +++ b/enrichment/noopEnrichment.go @@ -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 }