Skip to content

Commit

Permalink
isolate logging logic
Browse files Browse the repository at this point in the history
  • Loading branch information
83bytes committed Jun 23, 2024
1 parent db2ef97 commit cd81045
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 11 deletions.
37 changes: 37 additions & 0 deletions basicWebhookPayload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"version": "4",
"groupKey": "",
"truncatedAlerts": 0,
"status": "firing",
"receiver": "<string>",
"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"
}
]
}
7 changes: 3 additions & 4 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package cmd

import (
"alertmanager/config"
"alertmanager/logging"
"alertmanager/server"
"fmt"
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -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
}
Expand Down Expand Up @@ -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")
}
19 changes: 14 additions & 5 deletions cmd/logging.go → logging/logging.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package logging

import (
"fmt"
Expand All @@ -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)
Expand All @@ -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}
}
8 changes: 6 additions & 2 deletions server/handler.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 5 additions & 0 deletions webhook/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package webhook

// type struct WebhookData {

// }

0 comments on commit cd81045

Please sign in to comment.