mirror of https://github.com/prometheus/prometheus
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
204 lines
5.6 KiB
204 lines
5.6 KiB
9 years ago
|
// Copyright 2015 The Prometheus Authors
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
|
||
8 years ago
|
package ec2
|
||
9 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
8 years ago
|
"net"
|
||
9 years ago
|
"strings"
|
||
9 years ago
|
"time"
|
||
|
|
||
|
"github.com/aws/aws-sdk-go/aws"
|
||
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
||
8 years ago
|
"github.com/aws/aws-sdk-go/aws/session"
|
||
8 years ago
|
"github.com/prometheus/client_golang/prometheus"
|
||
9 years ago
|
"github.com/prometheus/common/log"
|
||
|
"github.com/prometheus/common/model"
|
||
9 years ago
|
"golang.org/x/net/context"
|
||
9 years ago
|
|
||
|
"github.com/aws/aws-sdk-go/service/ec2"
|
||
|
"github.com/prometheus/prometheus/config"
|
||
|
"github.com/prometheus/prometheus/util/strutil"
|
||
|
)
|
||
|
|
||
|
const (
|
||
8 years ago
|
ec2Label = model.MetaLabelPrefix + "ec2_"
|
||
|
ec2LabelAZ = ec2Label + "availability_zone"
|
||
|
ec2LabelInstanceID = ec2Label + "instance_id"
|
||
|
ec2LabelInstanceState = ec2Label + "instance_state"
|
||
8 years ago
|
ec2LabelInstanceType = ec2Label + "instance_type"
|
||
8 years ago
|
ec2LabelPublicDNS = ec2Label + "public_dns_name"
|
||
|
ec2LabelPublicIP = ec2Label + "public_ip"
|
||
|
ec2LabelPrivateIP = ec2Label + "private_ip"
|
||
|
ec2LabelSubnetID = ec2Label + "subnet_id"
|
||
|
ec2LabelTag = ec2Label + "tag_"
|
||
|
ec2LabelVPCID = ec2Label + "vpc_id"
|
||
|
subnetSeparator = ","
|
||
9 years ago
|
)
|
||
|
|
||
8 years ago
|
var (
|
||
8 years ago
|
ec2SDRefreshFailuresCount = prometheus.NewCounter(
|
||
8 years ago
|
prometheus.CounterOpts{
|
||
8 years ago
|
Name: "prometheus_sd_ec2_refresh_failures_total",
|
||
|
Help: "The number of EC2-SD scrape failures.",
|
||
8 years ago
|
})
|
||
8 years ago
|
ec2SDRefreshDuration = prometheus.NewSummary(
|
||
8 years ago
|
prometheus.SummaryOpts{
|
||
8 years ago
|
Name: "prometheus_sd_ec2_refresh_duration_seconds",
|
||
|
Help: "The duration of a EC2-SD refresh in seconds.",
|
||
8 years ago
|
})
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
8 years ago
|
prometheus.MustRegister(ec2SDRefreshFailuresCount)
|
||
|
prometheus.MustRegister(ec2SDRefreshDuration)
|
||
8 years ago
|
}
|
||
|
|
||
9 years ago
|
// EC2Discovery periodically performs EC2-SD requests. It implements
|
||
|
// the TargetProvider interface.
|
||
|
type EC2Discovery struct {
|
||
|
aws *aws.Config
|
||
|
interval time.Duration
|
||
8 years ago
|
profile string
|
||
9 years ago
|
port int
|
||
|
}
|
||
|
|
||
8 years ago
|
// NewDiscovery returns a new EC2Discovery which periodically refreshes its targets.
|
||
|
func NewDiscovery(conf *config.EC2SDConfig) *EC2Discovery {
|
||
9 years ago
|
creds := credentials.NewStaticCredentials(conf.AccessKey, conf.SecretKey, "")
|
||
|
if conf.AccessKey == "" && conf.SecretKey == "" {
|
||
8 years ago
|
creds = nil
|
||
9 years ago
|
}
|
||
|
return &EC2Discovery{
|
||
|
aws: &aws.Config{
|
||
|
Region: &conf.Region,
|
||
|
Credentials: creds,
|
||
|
},
|
||
8 years ago
|
profile: conf.Profile,
|
||
9 years ago
|
interval: time.Duration(conf.RefreshInterval),
|
||
|
port: conf.Port,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Run implements the TargetProvider interface.
|
||
9 years ago
|
func (ed *EC2Discovery) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
|
||
9 years ago
|
ticker := time.NewTicker(ed.interval)
|
||
|
defer ticker.Stop()
|
||
|
|
||
|
// Get an initial set right away.
|
||
|
tg, err := ed.refresh()
|
||
|
if err != nil {
|
||
|
log.Error(err)
|
||
|
} else {
|
||
8 years ago
|
select {
|
||
|
case ch <- []*config.TargetGroup{tg}:
|
||
|
case <-ctx.Done():
|
||
|
return
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
|
for {
|
||
|
select {
|
||
|
case <-ticker.C:
|
||
|
tg, err := ed.refresh()
|
||
|
if err != nil {
|
||
|
log.Error(err)
|
||
8 years ago
|
continue
|
||
|
}
|
||
|
|
||
|
select {
|
||
|
case ch <- []*config.TargetGroup{tg}:
|
||
|
case <-ctx.Done():
|
||
|
return
|
||
9 years ago
|
}
|
||
9 years ago
|
case <-ctx.Done():
|
||
9 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func (ed *EC2Discovery) refresh() (tg *config.TargetGroup, err error) {
|
||
|
t0 := time.Now()
|
||
|
defer func() {
|
||
8 years ago
|
ec2SDRefreshDuration.Observe(time.Since(t0).Seconds())
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
ec2SDRefreshFailuresCount.Inc()
|
||
8 years ago
|
}
|
||
|
}()
|
||
|
|
||
8 years ago
|
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)
|
||
8 years ago
|
tg = &config.TargetGroup{
|
||
9 years ago
|
Source: *ed.aws.Region,
|
||
|
}
|
||
8 years ago
|
if err = ec2s.DescribeInstancesPages(nil, func(p *ec2.DescribeInstancesOutput, lastPage bool) bool {
|
||
9 years ago
|
for _, r := range p.Reservations {
|
||
|
for _, inst := range r.Instances {
|
||
|
if inst.PrivateIpAddress == nil {
|
||
|
continue
|
||
|
}
|
||
|
labels := model.LabelSet{
|
||
|
ec2LabelInstanceID: model.LabelValue(*inst.InstanceId),
|
||
|
}
|
||
|
labels[ec2LabelPrivateIP] = model.LabelValue(*inst.PrivateIpAddress)
|
||
8 years ago
|
addr := net.JoinHostPort(*inst.PrivateIpAddress, fmt.Sprintf("%d", ed.port))
|
||
9 years ago
|
labels[model.AddressLabel] = model.LabelValue(addr)
|
||
9 years ago
|
|
||
|
if inst.PublicIpAddress != nil {
|
||
|
labels[ec2LabelPublicIP] = model.LabelValue(*inst.PublicIpAddress)
|
||
|
labels[ec2LabelPublicDNS] = model.LabelValue(*inst.PublicDnsName)
|
||
|
}
|
||
|
|
||
|
labels[ec2LabelAZ] = model.LabelValue(*inst.Placement.AvailabilityZone)
|
||
8 years ago
|
labels[ec2LabelInstanceState] = model.LabelValue(*inst.State.Name)
|
||
8 years ago
|
labels[ec2LabelInstanceType] = model.LabelValue(*inst.InstanceType)
|
||
9 years ago
|
|
||
|
if inst.VpcId != nil {
|
||
|
labels[ec2LabelVPCID] = model.LabelValue(*inst.VpcId)
|
||
|
|
||
|
subnetsMap := make(map[string]struct{})
|
||
|
for _, eni := range inst.NetworkInterfaces {
|
||
|
subnetsMap[*eni.SubnetId] = struct{}{}
|
||
|
}
|
||
|
subnets := []string{}
|
||
|
for k := range subnetsMap {
|
||
|
subnets = append(subnets, k)
|
||
|
}
|
||
|
labels[ec2LabelSubnetID] = model.LabelValue(
|
||
|
subnetSeparator +
|
||
|
strings.Join(subnets, subnetSeparator) +
|
||
|
subnetSeparator)
|
||
|
}
|
||
|
|
||
9 years ago
|
for _, t := range inst.Tags {
|
||
|
name := strutil.SanitizeLabelName(*t.Key)
|
||
|
labels[ec2LabelTag+model.LabelName(name)] = model.LabelValue(*t.Value)
|
||
|
}
|
||
|
tg.Targets = append(tg.Targets, labels)
|
||
|
}
|
||
|
}
|
||
|
return true
|
||
|
}); err != nil {
|
||
|
return nil, fmt.Errorf("could not describe instances: %s", err)
|
||
|
}
|
||
|
return tg, nil
|
||
|
}
|