Skip to content

Commit

Permalink
feat: Add option to log AWS Health events on console
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreZiviani committed Apr 4, 2024
1 parent 25aff89 commit 3b8c482
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
25 changes: 25 additions & 0 deletions exporter/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions exporter/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 2 additions & 0 deletions exporter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type Metrics struct {
ignoreResourceEvent []string

accountNames map[string]string

logEvents bool
}

type HealthEvent struct {
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: <event name>:<resource identifier>)"},
&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},
}
Expand All @@ -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()

Expand Down

0 comments on commit 3b8c482

Please sign in to comment.