ec2_sd_configs: Support profiles for configuring the ec2 service

This commit is contained in:
Kraig Amador 2016-11-03 07:54:27 -07:00
parent ba3fcceaa5
commit bec6870ed4
5 changed files with 17 additions and 3 deletions

View file

@ -29,6 +29,7 @@ The following individuals have contributed code to this repository
* Joonas Bergius <joonas@digitalocean.com>
* Joseph Wilk <joe@josephwilk.net>
* Julius Volz <julius.volz@gmail.com>
* Kraig Amador <kraig@bigkraig.com>
* Laurie Malau <laurie.malau@gmail.com>
* Marko Mikulicic <mkm@cesanta.com>
* Matt T. Proud <matt.proud@gmail.com>

View file

@ -907,6 +907,7 @@ type EC2SDConfig struct {
Region string `yaml:"region"`
AccessKey string `yaml:"access_key,omitempty"`
SecretKey string `yaml:"secret_key,omitempty"`
Profile string `yaml:"profile,omitempty"`
RefreshInterval model.Duration `yaml:"refresh_interval,omitempty"`
Port int `yaml:"port"`

View file

@ -292,6 +292,7 @@ var expectedConf = &Config{
Region: "us-east-1",
AccessKey: "access",
SecretKey: "secret",
Profile: "profile",
RefreshInterval: model.Duration(60 * time.Second),
Port: 80,
},

View file

@ -143,6 +143,7 @@ scrape_configs:
- region: us-east-1
access_key: access
secret_key: secret
profile: profile
- job_name: service-azure
azure_sd_configs:

View file

@ -21,7 +21,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/prometheus/common/model"
@ -72,6 +72,7 @@ func init() {
type EC2Discovery struct {
aws *aws.Config
interval time.Duration
profile string
port int
}
@ -79,13 +80,14 @@ type EC2Discovery struct {
func NewEC2Discovery(conf *config.EC2SDConfig) *EC2Discovery {
creds := credentials.NewStaticCredentials(conf.AccessKey, conf.SecretKey, "")
if conf.AccessKey == "" && conf.SecretKey == "" {
creds = defaults.DefaultChainCredentials
creds = nil
}
return &EC2Discovery{
aws: &aws.Config{
Region: &conf.Region,
Credentials: creds,
},
profile: conf.Profile,
interval: time.Duration(conf.RefreshInterval),
port: conf.Port,
}
@ -130,7 +132,15 @@ func (ed *EC2Discovery) refresh() (tg *config.TargetGroup, err error) {
}
}()
ec2s := ec2.New(ed.aws)
sess, err := session.NewSessionWithOptions(session.Options{
Config: *ed.aws,
Profile: ed.profile,
})
if err != nil {
return nil, fmt.Errorf("could not create aws session: %s", err)
}
ec2s := ec2.New(sess)
tg = &config.TargetGroup{
Source: *ed.aws.Region,
}