From 2ec56139d6828e1283b1ea215394dfc79b060efb Mon Sep 17 00:00:00 2001 From: Andre Ziviani Date: Tue, 4 Apr 2023 11:08:06 -0300 Subject: [PATCH] fix: Timezone initialization --- exporter/health.go | 11 ++--------- exporter/metrics.go | 8 ++++++++ exporter/types.go | 2 ++ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/exporter/health.go b/exporter/health.go index a67f03b..a0d87ec 100644 --- a/exporter/health.go +++ b/exporter/health.go @@ -3,9 +3,7 @@ package exporter import ( "context" "fmt" - "os" "strings" - "time" "github.com/aws/aws-sdk-go-v2/service/health" healthTypes "github.com/aws/aws-sdk-go-v2/service/health/types" @@ -23,11 +21,6 @@ func (m *Metrics) HealthOrganizationEnabled(ctx context.Context) bool { } func (m Metrics) SendSlackNotification(events []HealthEvent) { - tz, err := time.LoadLocation(os.Getenv("TZ")) - if err != nil { - panic(err.Error()) - } - for _, e := range events { resources := m.extractResources(e.AffectedResources) @@ -43,7 +36,7 @@ func (m Metrics) SendSlackNotification(events []HealthEvent) { {Title: "Resource(s)", Value: resources, Short: true}, {Title: "Service", Value: service, Short: true}, {Title: "Region", Value: region, Short: true}, - {Title: "Start Time", Value: e.Event.StartTime.In(tz).String(), Short: true}, + {Title: "Start Time", Value: e.Event.StartTime.In(m.tz).String(), Short: true}, {Title: "Status", Value: string(status), Short: true}, {Title: "Event ARN", Value: fmt.Sprintf("`%s`", *e.Event.Arn), Short: false}, {Title: "Updates", Value: *e.EventDescription.LatestDescription, Short: false}, @@ -53,7 +46,7 @@ func (m Metrics) SendSlackNotification(events []HealthEvent) { text = fmt.Sprintf(":heavy_check_mark:*[RESOLVED] The AWS Health issue with the %s service in the %s region is now resolved.*", service, region) color = "18be52" attachmentFields = append(attachmentFields[:6], attachmentFields[5:]...) - attachmentFields[5] = slack.AttachmentField{Title: "End Time", Value: e.Event.EndTime.In(tz).String(), Short: true} + attachmentFields[5] = slack.AttachmentField{Title: "End Time", Value: e.Event.EndTime.In(m.tz).String(), Short: true} } else { text = fmt.Sprintf(":rotating_light:*[NEW] AWS Health reported an issue with the %s service in the %s region.*", service, region) color = "danger" diff --git a/exporter/metrics.go b/exporter/metrics.go index 7de4256..cbe0663 100644 --- a/exporter/metrics.go +++ b/exporter/metrics.go @@ -2,8 +2,10 @@ package exporter import ( "context" + "os" "strings" "time" + _ "time/tzdata" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/credentials/stscreds" @@ -55,6 +57,12 @@ func (m *Metrics) init(ctx context.Context, c *cli.Context) { m.slackApi = slack.New(m.slackToken) m.organizationEnabled = m.HealthOrganizationEnabled(ctx) + + m.tz, err = time.LoadLocation(os.Getenv("TZ")) + if err != nil { + panic(err.Error()) + } + } func (m *Metrics) Describe(ch chan<- *prometheus.Desc) { diff --git a/exporter/types.go b/exporter/types.go index 772d630..462a012 100644 --- a/exporter/types.go +++ b/exporter/types.go @@ -18,6 +18,8 @@ type Metrics struct { slackApi *slack.Client slackToken string slackChannel string + + tz *time.Location } type HealthEvent struct {