From cd810452ee1b8221fef354070d9bde3599412d25 Mon Sep 17 00:00:00 2001 From: Sohom Bhattacharjee Date: Sun, 23 Jun 2024 20:24:43 +0530 Subject: [PATCH] isolate logging logic --- basicWebhookPayload.json | 37 +++++++++++++++++++++++++++++++++++++ cmd/server.go | 7 +++---- {cmd => logging}/logging.go | 19 ++++++++++++++----- server/handler.go | 8 ++++++-- webhook/data.go | 5 +++++ 5 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 basicWebhookPayload.json rename {cmd => logging}/logging.go (52%) create mode 100644 webhook/data.go diff --git a/basicWebhookPayload.json b/basicWebhookPayload.json new file mode 100644 index 0000000..fddc967 --- /dev/null +++ b/basicWebhookPayload.json @@ -0,0 +1,37 @@ +{ + "version": "4", + "groupKey": "", + "truncatedAlerts": 0, + "status": "firing", + "receiver": "", + "groupLabels": {}, + "commonLabels": {}, + "commonAnnotations": {}, + "externalURL": "https://something", + "alerts": [ + { + "annotations": { + "description": "Pod customer/customer-test-service-9b75b488c-cpfd7 (test-service) is restarting 2.11 times / 10 minutes.", + "runbook_url": "https://github.com/kubernetes-monitoring/kubernetes-mixin/tree/master/runbook.md#alert-name-kubepodcrashlooping", + "summary": "Pod is crash looping." + }, + "labels": { + "alertname": "KubePodCrashLooping", + "cluster": "cluster-main", + "container": "test-service", + "endpoint": "http", + "job": "kube-state-metrics", + "namespace": "customer", + "pod": "customer-test-service-9b75b488c-cpfd7", + "priority": "P0", + "prometheus": "monitoring/kube-prometheus-stack-prometheus", + "region": "us-west-1", + "replica": "0", + "service": "kube-prometheus-stack-kube-state-metrics", + "severity": "CRITICAL" + }, + "startsAt": "2022-03-02T07:31:57.339Z", + "status": "firing" + } + ] +} diff --git a/cmd/server.go b/cmd/server.go index 881a0b4..561cbf8 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -2,11 +2,11 @@ package cmd import ( "alertmanager/config" + "alertmanager/logging" "alertmanager/server" "fmt" "os" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -23,8 +23,7 @@ func serverCommandRunE(cmd *cobra.Command, args []string) error { mgmtPort, _ := cmd.Flags().GetInt("meanagement-port") cFile, _ := cmd.Flags().GetString("config-file") - log := logrus.New() - err := setLogLevelE(log, ll) + log, err := logging.NewLogger(ll) if err != nil { return err } @@ -58,5 +57,5 @@ func init() { // serverCmd.Flags().Int("metric-port", 8082, "metrics port to listen on") // serverCmd.Flags().Int("management-port", 8083, "management port to listen on") serverCmd.Flags().String("config-file", "./alert-manager-config.yml", "Path to alert config") - serverCmd.Flags().String("log-level", DEFAULT_LOG_LEVEL, "log-level for alertmanager; options INFO|DEBUG|ERROR") + serverCmd.Flags().String("log-level", logging.DEFAULT_LOG_LEVEL, "log-level for alertmanager; options INFO|DEBUG|ERROR") } diff --git a/cmd/logging.go b/logging/logging.go similarity index 52% rename from cmd/logging.go rename to logging/logging.go index 4de5da5..419399d 100644 --- a/cmd/logging.go +++ b/logging/logging.go @@ -1,4 +1,4 @@ -package cmd +package logging import ( "fmt" @@ -7,11 +7,16 @@ import ( "github.com/sirupsen/logrus" ) +type Logger struct { + *logrus.Logger +} + var DEFAULT_LOG_LEVEL = "INFO" +var log = logrus.New() -func setLogLevelE(log *logrus.Logger, ll string) error { +func NewLogger(logLevel string) (*Logger, error) { - switch strings.ToUpper(ll) { + switch strings.ToUpper(logLevel) { case "INFO": log.Info("info logs enabled") log.SetLevel(logrus.InfoLevel) @@ -22,8 +27,12 @@ func setLogLevelE(log *logrus.Logger, ll string) error { log.Info("error logs enabled") log.SetLevel(logrus.ErrorLevel) default: - return fmt.Errorf("unsupported log-level %s; refer docs", ll) + return nil, fmt.Errorf("unsupported log-level %s; refer docs", logLevel) } - return nil + return &Logger{log}, nil +} + +func GetLogger() *Logger { + return &Logger{log} } diff --git a/server/handler.go b/server/handler.go index b0857d8..0eee301 100644 --- a/server/handler.go +++ b/server/handler.go @@ -1,7 +1,11 @@ package server -import "github.com/gofiber/fiber/v2" +import ( + "github.com/gofiber/fiber/v2" +) func alertWebhookHandler(c *fiber.Ctx) error { - return c.SendString("pong") + c.Context().Logger() + b := c.BodyRaw() + return c.Status(fiber.StatusBadRequest).Send(b) } diff --git a/webhook/data.go b/webhook/data.go new file mode 100644 index 0000000..9e35905 --- /dev/null +++ b/webhook/data.go @@ -0,0 +1,5 @@ +package webhook + +// type struct WebhookData { + +// }