Skip to content

Commit

Permalink
fix: AWS Health service region
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreZiviani committed Jun 13, 2023
1 parent 4561b01 commit 0e678eb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
35 changes: 31 additions & 4 deletions exporter/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ package exporter
import (
"context"
"fmt"
"net"
"os"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/health"
)

const (
TermOnDemand string = "JRTCKXETXF"
TermPerHour string = "6YS6EN2CT7"
TermOnDemand string = "JRTCKXETXF"
TermPerHour string = "6YS6EN2CT7"
HealthEndpoint string = "global.health.amazonaws.com"
)

type Pricing struct {
Expand Down Expand Up @@ -48,13 +52,36 @@ type Details struct {
PricePerUnit map[string]string
}

func newAWSConfig(ctx context.Context) (aws.Config, error) {
func (m *Metrics) NewHealthClient(ctx context.Context) {
// AWS Health is a global service with two regions:
// Active: us-east-1
// Passive: us-east-2
// When theres an incident in us-east-1 AWS can change the endpoint to us-east-2 but, AFAIK you have to manage this yourself
// AWS Health Aware implementation also do something like this...
cname, err := net.LookupCNAME(HealthEndpoint)
if err != nil {
panic(err.Error())
}

cname = strings.TrimSuffix(cname, ".")
region := strings.Split(cname, ".")[1]

cfg, err := newAWSConfig(ctx, config.WithRegion(region))

if err != nil {
panic(err.Error())
}

m.health = health.NewFromConfig(cfg, health.WithEndpointResolver(health.EndpointResolverFromURL(fmt.Sprintf("https://%s", cname))))
}

func newAWSConfig(ctx context.Context, optFns ...func(*config.LoadOptions) error) (aws.Config, error) {
region := os.Getenv("AWS_REGION")
if region == "" {
return aws.Config{}, fmt.Errorf("Please configure the AWS_REGION environment variable")
}

cfg, err := config.LoadDefaultConfig(ctx)
cfg, err := config.LoadDefaultConfig(ctx, optFns...)
if err != nil {
return aws.Config{}, err
}
Expand Down
11 changes: 5 additions & 6 deletions exporter/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/health"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
Expand All @@ -36,20 +35,20 @@ func NewMetrics(ctx context.Context, registry *prometheus.Registry, c *cli.Conte

func (m *Metrics) init(ctx context.Context, c *cli.Context) {
cfg, err := newAWSConfig(ctx)

if err != nil {
panic(err.Error())
}

cfg.Region = "us-east-1"
m.awsconfig = cfg

if len(c.String("assume-role")) > 0 {
stsclient := sts.NewFromConfig(cfg)
stsclient := sts.NewFromConfig(m.awsconfig)
creds := stscreds.NewAssumeRoleProvider(stsclient, c.String("assume-role"))
cfg.Credentials = aws.NewCredentialsCache(creds)
m.awsconfig.Credentials = aws.NewCredentialsCache(creds)
}

m.health = health.NewFromConfig(cfg)
m.awsconfig = cfg
m.NewHealthClient(ctx)

m.lastScrape = time.Now()

Expand Down

0 comments on commit 0e678eb

Please sign in to comment.