From 3b8c48231d4145203fa129e9ac60abca010136ac Mon Sep 17 00:00:00 2001 From: Andre Ziviani Date: Thu, 4 Apr 2024 16:04:21 -0300 Subject: [PATCH] feat: Add option to log AWS Health events on console --- exporter/health.go | 25 +++++++++++++++++++++++++ exporter/metrics.go | 3 +++ exporter/types.go | 2 ++ main.go | 3 ++- 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/exporter/health.go b/exporter/health.go index 191d4c2..ec10c70 100644 --- a/exporter/health.go +++ b/exporter/health.go @@ -2,11 +2,13 @@ package exporter import ( "context" + "encoding/json" "fmt" "strings" "github.com/aws/aws-sdk-go-v2/service/health" healthTypes "github.com/aws/aws-sdk-go-v2/service/health/types" + log "github.com/sirupsen/logrus" "github.com/slack-go/slack" ) @@ -45,11 +47,34 @@ func (m *Metrics) GetHealthEvents() []HealthEvent { events = append(events, e) m.SendSlackNotification(e) + m.LogEvent(e) } return events } +func (m Metrics) LogEvent(e HealthEvent) { + if !m.logEvents { + return + } + msg := map[string]string{ + "resources": m.extractResources(e.AffectedResources), + "accounts": m.extractAccounts(e.AffectedAccounts), + "service": *e.Event.Service, + "region": *e.Event.Region, + "status": string(e.Event.StatusCode), + "Start Time": e.Event.StartTime.In(m.tz).String(), + "Event ARN": fmt.Sprintf("`%s`", *e.Event.Arn), + "Updates": *e.EventDescription.LatestDescription, + } + + j, _ := json.Marshal(msg) + + log.WithFields(log.Fields{ + "event": string(j), + }).Info() + +} func (m Metrics) SendSlackNotification(e HealthEvent) { if m.slackApi == nil { return diff --git a/exporter/metrics.go b/exporter/metrics.go index 71b83c4..6e2d40a 100644 --- a/exporter/metrics.go +++ b/exporter/metrics.go @@ -109,4 +109,7 @@ func (m *Metrics) init(ctx context.Context, c *cli.Context) { sort.Strings(m.ignoreResourceEvent) } + if c.Bool("log-events") { + m.logEvents = true + } } diff --git a/exporter/types.go b/exporter/types.go index 3820da5..75e1aa6 100644 --- a/exporter/types.go +++ b/exporter/types.go @@ -28,6 +28,8 @@ type Metrics struct { ignoreResourceEvent []string accountNames map[string]string + + logEvents bool } type HealthEvent struct { diff --git a/main.go b/main.go index e912301..ac86247 100644 --- a/main.go +++ b/main.go @@ -31,6 +31,7 @@ func main() { &cli.StringFlag{Name: "ignore-events", Usage: "Comma separated list of events to be ignored on all resources"}, &cli.StringFlag{Name: "ignore-resources", Usage: "Comma separated list of resources to be ignored on all events, format is dependant on resource type (some are ARN others are Name, check AWS docs)"}, &cli.StringFlag{Name: "ignore-resource-event", Usage: "Comma separated list of events to be ignored on a specific resource (format: :)"}, + &cli.BoolFlag{Name: "log-events", Usage: "Log AWS Health events as JSON", Value: false}, &cli.DurationFlag{Name: "time-shift", Usage: "[INTERNAL] Apply a time delta to event filter instead of looking at previous scrape", Hidden: true, Value: 0 * time.Second}, } @@ -47,7 +48,7 @@ func main() { log.Debugf("Set log level to %s", parsedLevel) } - log.Infof("Starting AWS Health Exporter. [log-level=%s]", c.String("log-level")) + log.Infof("Starting AWS Health Exporter. [log-level=%s,log-events=%t]", c.String("log-level"), c.Bool("log-events")) ctx := context.Background()