Skip to content

Commit

Permalink
Merge pull request #2 from leprechaun/region-filtering
Browse files Browse the repository at this point in the history
feature(regions): Adding support for limiting the regions we query
  • Loading branch information
martonsereg authored Dec 10, 2018
2 parents 6f7f361 + 219c645 commit ae7e9b5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Usage of ./spot-price-exporter:
Comma separated list of AWS partitions. Accepted values: aws, aws-cn, aws-us-gov (default "aws")
-product-descriptions string
Comma separated list of product descriptions. Accepted values: Linux/UNIX, SUSE Linux, Windows, Linux/UNIX (Amazon VPC), SUSE Linux (Amazon VPC), Windows (Amazon VPC) (default "Linux/UNIX")
-regions string
Comma separated list of AWS regions to get pricing for (defaults to *all*)
```

### Example metrics
Expand Down
25 changes: 22 additions & 3 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Exporter struct {
session *session.Session
partitions []string
productDescriptions []string
regions []string
duration prometheus.Gauge
scrapeErrors prometheus.Gauge
totalScrapes prometheus.Counter
Expand All @@ -36,8 +37,8 @@ type scrapeResult struct {
ProductDescription string
}

// NewExporter returns a new exporter of AWS Autoscaling group metrics.
func NewExporter(p []string, pds []string) (*Exporter, error) {
// NewExporter returns a new exporter of AWS Spot Price metrics.
func NewExporter(p []string, pds []string, regions []string) (*Exporter, error) {

session, err := session.NewSession()
if err != nil {
Expand All @@ -49,6 +50,7 @@ func NewExporter(p []string, pds []string) (*Exporter, error) {
session: session,
partitions: p,
productDescriptions: pds,
regions: regions,
duration: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "aws_spot",
Name: "scrape_duration_seconds",
Expand Down Expand Up @@ -123,9 +125,17 @@ func (e *Exporter) scrape(scrapes chan<- scrapeResult) {
if !e.inPartitions(p.ID()) {
continue
}
log.Infof("querying spot prices in all regions [partition=%s]", p.ID())
log.Infof("querying spot prices in each region [partition=%s]", p.ID())
var wg sync.WaitGroup
for _, r := range p.Regions() {
// Skip regions we don't care about, if we specified any we do
if len(e.regions) > 0 {
if !e.inRegions(r.ID()) {
log.Debugf("Skipping region %s", r.ID())
continue
}
}

log.Debugf("querying spot prices [region=%s]", r.ID())
wg.Add(1)
go func(r string) {
Expand Down Expand Up @@ -203,3 +213,12 @@ func (e *Exporter) inPartitions(p string) bool {
}
return false
}

func (e *Exporter) inRegions(r string) bool {
for _, region := range e.regions {
if r == region {
return true
}
}
return false
}
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
rawLevel = flag.String("log-level", "info", "log level")
partitions = flag.String("partitions", "aws", "Comma separated list of AWS partitions. Accepted values: aws, aws-cn, aws-us-gov")
productDescriptions = flag.String("product-descriptions", "Linux/UNIX", "Comma separated list of product descriptions. Accepted values: Linux/UNIX, SUSE Linux, Windows, Linux/UNIX (Amazon VPC), SUSE Linux (Amazon VPC), Windows (Amazon VPC)")
regions = flag.String("regions", "", "Comma separated list of AWS regions to get pricing for (defaults to *all*)")
)

func init() {
Expand All @@ -34,9 +35,10 @@ func main() {
log.Infof("Starting AWS Spot Price exporter. [log-level=%s, partitions=%s, product-descriptions=%s]", *rawLevel, *partitions, *productDescriptions)
parts := splitAndTrim(*partitions)
pds := splitAndTrim(*productDescriptions)
regions := splitAndTrim(*regions)
validatePartitions(parts)
validateProductDesc(pds)
exporter, err := exporter.NewExporter(parts, pds)
exporter, err := exporter.NewExporter(parts, pds, regions)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit ae7e9b5

Please sign in to comment.