Skip to content

Commit

Permalink
added new metric path, spot price metrics with zones
Browse files Browse the repository at this point in the history
  • Loading branch information
Attila Prekopcsák authored and lpuskas committed Dec 4, 2018
1 parent 39f38b8 commit ae60288
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 27 deletions.
1 change: 0 additions & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions cmd/cloudinfo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,14 @@ func main() {
// add prometheus metric endpoint
if viper.GetBool(metricsEnabledFlag) {
reg := prometheus.NewRegistry()
reg.MustRegister(cloudinfo.OnDemandPriceGauge, cloudinfo.SpotPriceGauge)
reg.MustRegister(cloudinfo.OnDemandPriceGauge, google.SpotPriceGauge, azure.SpotPriceGauge)
spotReg := prometheus.NewRegistry()
spotReg.MustRegister(amazon.SpotPriceGauge, alibaba.SpotPriceGauge)
p := ginprometheus.NewPrometheus("http", []string{"provider", "service", "region"})
p.SetListenAddress(viper.GetString(metricsAddressFlag))
p.Use(router)
p.MetricsPath = "/metrics/price"
p.UseWithCustomMetrics(router, prometheus.Gatherers{reg})
p.Use(router, "metrics")
p.UseWithCustomMetrics(router, prometheus.Gatherers{reg}, "/metrics/price")
p.UseWithCustomMetrics(router, prometheus.Gatherers{spotReg}, "/metrics/spotprice")
}

logger.Extract(ctx).Info("Initialized gin router")
Expand Down
17 changes: 15 additions & 2 deletions pkg/cloudinfo/alibaba/cloudinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"net/http"
"strconv"
"strings"
Expand All @@ -30,6 +31,15 @@ import (
"github.com/spf13/viper"
)

// SpotPriceGauge collects metrics for the prometheus
var SpotPriceGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "cloudinfo",
Name: "alibaba_spot_price",
Help: "spot price for each instance type",
},
[]string{"region", "zone", "instanceType"},
)

// OnDemandPrice contains price data from json
type OnDemandPrice struct {
Currency string `json:"currency"`
Expand Down Expand Up @@ -355,11 +365,14 @@ func (e *AlibabaInfoer) GetCurrentPrices(ctx context.Context, region string) (ma
}

prices := make(map[string]cloudinfo.Price)
for region, sp := range spotPrices {
prices[region] = cloudinfo.Price{
for instanceType, sp := range spotPrices {
prices[instanceType] = cloudinfo.Price{
SpotPrice: sp,
OnDemandPrice: -1,
}
for zone, price := range sp {
SpotPriceGauge.WithLabelValues(region, zone, instanceType).Set(price)
}
}

return prices, nil
Expand Down
17 changes: 15 additions & 2 deletions pkg/cloudinfo/amazon/cloudinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"strconv"
"strings"
"time"
Expand All @@ -39,6 +40,15 @@ const (
Cpu = "vcpu"
)

// SpotPriceGauge collects metrics for the prometheus
var SpotPriceGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "cloudinfo",
Name: "amazon_spot_price",
Help: "spot price for each instance type",
},
[]string{"region", "zone", "instanceType"},
)

var (
eksRegionIds = []string{"us-west-2", "us-east-1", "eu-west-1", "us-east-2"}
)
Expand Down Expand Up @@ -470,11 +480,14 @@ func (e *Ec2Infoer) GetCurrentPrices(ctx context.Context, region string) (map[st
}

prices := make(map[string]cloudinfo.Price)
for region, sp := range spotPrices {
prices[region] = cloudinfo.Price{
for instanceType, sp := range spotPrices {
prices[instanceType] = cloudinfo.Price{
SpotPrice: sp,
OnDemandPrice: -1,
}
for zone, price := range sp {
SpotPriceGauge.WithLabelValues(region, zone, instanceType).Set(price)
}
}
return prices, nil
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/cloudinfo/azure/cloudinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"io/ioutil"
"os"
"regexp"
Expand All @@ -39,6 +40,15 @@ import (
"github.com/banzaicloud/cloudinfo/pkg/cloudinfo"
)

// SpotPriceGauge collects metrics for the prometheus
var SpotPriceGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "cloudinfo",
Name: "azure_spot_price",
Help: "spot price for each instance type",
},
[]string{"region", "instanceType"},
)

var (
regionCodeMappings = map[string]string{
"ap": "asia",
Expand Down Expand Up @@ -245,6 +255,7 @@ func (a *AzureInfoer) Initialize(ctx context.Context) (map[string]map[string]clo
spotPrice := make(cloudinfo.SpotPriceInfo)
spotPrice[region] = priceInUsd
price.SpotPrice = spotPrice
SpotPriceGauge.WithLabelValues(region, instanceType).Set(priceInUsd)
}

allPrices[region][instanceType] = price
Expand Down
16 changes: 0 additions & 16 deletions pkg/cloudinfo/cloudinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,6 @@ var (
},
[]string{"provider", "region", "instanceType"},
)
// SpotPriceGauge collects metrics for the prometheus
SpotPriceGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "cloudinfo",
Name: "spot_price",
Help: "spot price for each instance type",
},
[]string{"provider", "region", "instanceType"},
)
)

// IsBurst returns true if the EC2 instance vCPU is burst type
Expand Down Expand Up @@ -411,10 +403,6 @@ func (cpi *CachingCloudInfo) Initialize(ctx context.Context, provider string) (m
for instType, p := range ap {
cpi.vmAttrStore.Set(cpi.getPriceKey(provider, region, instType), p, cpi.renewalInterval)
OnDemandPriceGauge.WithLabelValues(provider, region, instType).Set(p.OnDemandPrice)
for _, spotPrice := range p.SpotPrice {
SpotPriceGauge.WithLabelValues(provider, region, instType).Set(spotPrice)
break
}
}
}
log.Info("finished to initialize product information")
Expand Down Expand Up @@ -514,10 +502,6 @@ func (cpi *CachingCloudInfo) renewShortLivedInfo(ctx context.Context, provider s
}
for instType, p := range prices {
cpi.vmAttrStore.Set(cpi.getPriceKey(provider, region, instType), p, 8*time.Minute)
for _, spotPrice := range p.SpotPrice {
SpotPriceGauge.WithLabelValues(provider, region, instType).Set(spotPrice)
break
}
}
return prices, nil
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/cloudinfo/google/cloudinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package google
import (
"context"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"net/http"
"os"
"regexp"
Expand All @@ -32,6 +33,15 @@ import (
"google.golang.org/api/googleapi/transport"
)

// SpotPriceGauge collects metrics for the prometheus
var SpotPriceGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "cloudinfo",
Name: "google_spot_price",
Help: "spot price for each instance type",
},
[]string{"region", "zone", "instanceType"},
)

var regionNames = map[string]string{
"asia-east1": "Asia Pacific (Taiwan)",
"asia-east2": "Asia Pacific (Hong Kong)",
Expand Down Expand Up @@ -162,8 +172,10 @@ func (g *GceInfoer) Initialize(ctx context.Context) (map[string]map[string]cloud
for _, z := range zonesInRegions[region] {
if mt.Name == "f1-micro" || mt.Name == "g1-small" {
spotPrice[z] = price[mt.Name]["Preemptible"]
SpotPriceGauge.WithLabelValues(region, z, mt.Name).Set(spotPrice[z])
} else {
spotPrice[z] = price[cloudinfo.Cpu]["Preemptible"]*float64(mt.GuestCpus) + price[cloudinfo.Memory]["Preemptible"]*float64(mt.MemoryMb)/1024
SpotPriceGauge.WithLabelValues(region, z, mt.Name).Set(spotPrice[z])
}
}
prices.SpotPrice = spotPrice
Expand Down

0 comments on commit ae60288

Please sign in to comment.