From a1ddab463e440801e4e3d0a28cff37d4e41377d1 Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Wed, 31 May 2017 23:19:31 +0900 Subject: [PATCH 1/2] config: set default region for EC2 SD --- config/config.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index ee088fe99..9cde12170 100644 --- a/config/config.go +++ b/config/config.go @@ -23,6 +23,8 @@ import ( "strings" "time" + "github.com/aws/aws-sdk-go/aws/ec2metadata" + "github.com/aws/aws-sdk-go/aws/session" "github.com/prometheus/common/model" "gopkg.in/yaml.v2" ) @@ -1126,7 +1128,13 @@ func (c *EC2SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { return err } if c.Region == "" { - return fmt.Errorf("EC2 SD configuration requires a region") + sess := session.Must(session.NewSession()) + metadata := ec2metadata.New(sess) + region, err := metadata.Region() + if err != nil { + return fmt.Errorf("EC2 SD configuration requires a region") + } + c.Region = region } return nil } From 64cef5cd050fdf23b7cfc843f25a2386221f732c Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Tue, 6 Jun 2017 22:02:23 +0900 Subject: [PATCH 2/2] handle NewSession() error --- config/config.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 9cde12170..8a711f925 100644 --- a/config/config.go +++ b/config/config.go @@ -1128,7 +1128,10 @@ func (c *EC2SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { return err } if c.Region == "" { - sess := session.Must(session.NewSession()) + sess, err := session.NewSession() + if err != nil { + return err + } metadata := ec2metadata.New(sess) region, err := metadata.Region() if err != nil {