From 13070e8766920dd777cb144dd7c6204e787c58c8 Mon Sep 17 00:00:00 2001 From: Sohom Bhattacharjee Date: Sat, 29 Jun 2024 21:23:46 +0530 Subject: [PATCH] move alert to types --- alert/processAlert.go | 11 ++++++++++- server/handler.go | 3 ++- {alert => types}/alert.go | 20 +++++--------------- 3 files changed, 17 insertions(+), 17 deletions(-) rename {alert => types}/alert.go (77%) diff --git a/alert/processAlert.go b/alert/processAlert.go index c3e143b..962ae85 100644 --- a/alert/processAlert.go +++ b/alert/processAlert.go @@ -5,9 +5,18 @@ import ( "alertmanager/config" "alertmanager/enrichment" "alertmanager/logging" + "alertmanager/types" "fmt" ) +func LoadAlertFromPayload(a *types.Alert) error { + if an, ok := a.Labels["alertname"]; ok { + a.AlertName = an + return nil + } + return fmt.Errorf("alertname not present in alert payload") +} + // Processes an entire alert-pipeline end-to-end // If an alert-pipeline is configured for given alert // this function executes the enrichments one-by-one @@ -15,7 +24,7 @@ import ( // The body of the Action 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 -func ProcessAlert(a Alert) { +func ProcessAlert(a types.Alert) { logr := logging.GetLogger() an := a.GetAlertName() diff --git a/server/handler.go b/server/handler.go index a15ce89..6c06734 100644 --- a/server/handler.go +++ b/server/handler.go @@ -3,6 +3,7 @@ package server import ( "alertmanager/alert" "alertmanager/logging" + "alertmanager/types" "alertmanager/utils" "github.com/gofiber/fiber/v2" @@ -11,7 +12,7 @@ import ( func alertWebhookHandler(c *fiber.Ctx) error { logr := logging.GetLogger() logr.Debug("in webhook handler") - ag := new(alert.AlertGroup) + ag := new(types.AlertGroup) b := c.BodyRaw() diff --git a/alert/alert.go b/types/alert.go similarity index 77% rename from alert/alert.go rename to types/alert.go index 246c1ea..89c92d3 100644 --- a/alert/alert.go +++ b/types/alert.go @@ -1,17 +1,15 @@ -package alert +package types import ( - "alertmanager/types" "encoding/json" - "fmt" "time" ) type Alert struct { // fields we use to process stuff - alertName string `json:"-"` - enrichments []types.Enrichment `json:"-"` - actions []types.Action `json:"-"` + AlertName string `json:"-"` + Enrichments []Enrichment `json:"-"` + Actions []Action `json:"-"` // fields we get from outside Annotations map[string]string `json:"annotations"` @@ -35,7 +33,7 @@ type AlertGroup struct { // getter and setter for internal fields func (a *Alert) GetAlertName() string { - return a.alertName + return a.AlertName } func (c AlertGroup) String() string { @@ -45,11 +43,3 @@ func (c AlertGroup) String() string { // handled at the ValidateAndLoad level return string(s) } - -func LoadAlertFromPayload(a *Alert) error { - if an, ok := a.Labels["alertname"]; ok { - a.alertName = an - return nil - } - return fmt.Errorf("alertname not present in alert payload") -}