Skip to content

Commit

Permalink
fix: Timezone initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreZiviani committed Apr 4, 2023
1 parent ac8cbf6 commit 2ec5613
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
11 changes: 2 additions & 9 deletions exporter/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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},
Expand All @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions exporter/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions exporter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Metrics struct {
slackApi *slack.Client
slackToken string
slackChannel string

tz *time.Location
}

type HealthEvent struct {
Expand Down

0 comments on commit 2ec5613

Please sign in to comment.