Skip to content

Commit

Permalink
got enrichments to work
Browse files Browse the repository at this point in the history
  • Loading branch information
83bytes committed Jun 27, 2024
1 parent 696ccdb commit fd65974
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 3 deletions.
21 changes: 19 additions & 2 deletions alert/processAlert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package alert

import (
"alertmanager/config"
"alertmanager/enrichment"
"alertmanager/logging"
"fmt"
)

// Once I have an alertName
Expand All @@ -18,10 +20,25 @@ func ProcessAlert(a Alert) {

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

logr.Debug("alert pipeline", p)
enrichmentMap := enrichment.GetEnrichmentMap()

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

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

}

}
2 changes: 1 addition & 1 deletion basicWebhookPayload.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"summary": "Pod is crash looping."
},
"labels": {
"alertname": "KubePodCrashLooping2",
"alertname": "NOOP_ALERT",
"cluster": "cluster-main",
"container": "test-service",
"endpoint": "http",
Expand Down
6 changes: 6 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"alertmanager/config"
"alertmanager/enrichment"
"alertmanager/logging"
"alertmanager/server"
"fmt"
Expand All @@ -28,6 +29,11 @@ func serverCommandRunE(cmd *cobra.Command, args []string) error {
return err
}

// Initialize the Enrichments
log.Info("Adding NooP Enrichment")
enr := enrichment.GetEnrichmentMap()
enr.Add("NOOP_ENRICHMENT", enrichment.Noop_Enrichment)

b, err := os.ReadFile(cFile)
if err != nil {
return fmt.Errorf("cannot read config file; %s", err)
Expand Down
8 changes: 8 additions & 0 deletions enrichment/enrichment.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package enrichment

import "alertmanager/utils"

type Enrichment struct {
EnrichmentName string `yaml:"enrichment_name"`
EnrichmentArgs string `yaml:"enrichment_args"`
Expand All @@ -8,3 +10,9 @@ type Enrichment struct {
func GetDefaultEnrichment() Enrichment {
return Enrichment{EnrichmentName: "NOOP_ENRICHMENT", EnrichmentArgs: "ARG1,ARG2"}
}

var enrichmentMap = make(utils.FunctionLut)

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

import (
"alertmanager/logging"
)

func Noop_Enrichment(args string) (string, error) {
logr := logging.GetLogger()

logr.Info("Noop enrichment Called")

return "Noop called", nil
}
17 changes: 17 additions & 0 deletions utils/fucntionLut.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package utils

// A lookup table for name -> function
// Functions are pretty generic.
// Accepts a string of args (todo: make this more generic)
// there are no timeouts yet (todo)
// each function is supposed to handle its own arg structure and how to parse it.
// todo
// there has to be a better way to do this with interfaces and gorotines and channels

type FunctionLut map[string]func(string) (string, error)

func (flut FunctionLut) Add(fname string, f func(string) (string, error)) FunctionLut {
flut[fname] = f

return flut
}

0 comments on commit fd65974

Please sign in to comment.